using ProjectPatientAccounting.Entities;
using ProjectPatientAccounting.Repositories;

namespace ProjectPatientAccounting.Forms
{
    public partial class FormReception : Form
    {
        private readonly IReceptionRepository _receptionRepository;

        public FormReception(IReceptionRepository receptionRepository, IPatientDiagnosisRepository patientDiagnosisRepository,
            IDoctorRepository doctorRepository, IPatientRepository patientRepository, IMedicamentRepository medicamentRepository)
        {
            InitializeComponent();
            _receptionRepository = receptionRepository ??
                throw new ArgumentNullException(nameof(receptionRepository));

            comboBoxPatient.DataSource = patientRepository.ReadPatients();
            comboBoxPatient.DisplayMember = "Surname";
            comboBoxPatient.ValueMember = "Id";

            comboBoxDoctor.DataSource = doctorRepository.ReadDoctors();
            comboBoxDoctor.DisplayMember = "Surname";
            comboBoxDoctor.ValueMember = "Id";

            comboBoxDiagnosis.DataSource = patientDiagnosisRepository.ReadDiagnosises();
            comboBoxDiagnosis.DisplayMember = "Name";
            comboBoxDiagnosis.ValueMember = "Id";

            ColumnType.DataSource = medicamentRepository.ReadMedicaments();
            ColumnType.DisplayMember = "Name";
            ColumnType.ValueMember = "Id";
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxPatient.SelectedIndex < 0 ||
                comboBoxDoctor.SelectedIndex < 0 ||
                comboBoxDiagnosis.SelectedIndex < 0 ||
                 dataGridViewMedicaments.RowCount < 1)

                {
                    throw new Exception("Имеются незаполненные поля");
                }
                _receptionRepository.CreateReception(Reception.CreateOperation(0,
                    (int)comboBoxPatient.SelectedValue!,
                    (int)comboBoxDoctor.SelectedValue!,
                    (int)comboBoxDiagnosis.SelectedValue!,
                    Convert.ToInt32(numericUpDownNumTicket.Value),
                    CreateListMedicamentReceptionsFromDataGrid()));
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при сохранении",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private List<MedicamentReception> CreateListMedicamentReceptionsFromDataGrid()
        {
            var list = new List<MedicamentReception>();
            foreach (DataGridViewRow row in dataGridViewMedicaments.Rows)
            {
                if (row.Cells["ColumnType"].Value == null ||
                row.Cells["ColumnDosage"].Value == null)
                {
                    continue;
                }
                list.Add(MedicamentReception.CreateElement(0,
                Convert.ToInt32(row.Cells["ColumnType"].Value),
                Convert.ToInt32(row.Cells["ColumnDosage"].Value)));
            }
            return list;

        }

        private void ButtonCancel_Click(object sender, EventArgs e) => Close();
    }
}

