using Microsoft.EntityFrameworkCore;
using Register_office.Model;
using Register_office.ViewModel;

namespace Register_office.View.PatientV
{
	public partial class DrugDataForm : Form
	{
		private PrescriptionViewModel _viewModel;
		private Prescription? _prescription;

		public DrugDataForm()
		{
			Init();
		}

		public DrugDataForm(Prescription p)
		{
			Init();
			SetData(p);
		}

		private void Init()
		{
			InitializeComponent();
			TopMost = true;
			_viewModel = new();
			SetDataSourceToComboBoxes();
		}

		private void SetData(Prescription prescription)
		{
			_prescription = prescription;
			cbPatient.SelectedValue = prescription.PatientId;
			cbDrug.SelectedValue = prescription.DrugTypePrescription.DrugTypeId;
			cbUnit.SelectedValue = prescription.DrugTypePrescription.DoseUnitId;
			nudDose.Value = prescription.DrugTypePrescription.Dose;
			cbIntakeMethod.SelectedValue = prescription.DrugTypePrescription.IntakeMethodId;
			nudDurationDays.Value = prescription.DrugTypePrescription.DurationDays;

			Text = "Просмотр процедуры";
			btnOK.Visible = cbPatient.Enabled = 
				cbIntakeMethod.Enabled = cbUnit.Enabled =
				nudDose.Enabled = nudDurationDays.Enabled = 
				cbDrug.Enabled = false;
		}

		private void SetDataSourceToComboBoxes()
		{
			cbDrug.DataSource = _viewModel.General.Context.DrugTypes
				.Select(x => new ComboBoxItem { Name = x.InternationalNameRu, Id = x.DrugTypeId })
				.OrderBy(cbi => cbi.Name).ToList();
			cbDrug.DisplayMember = "Name";
			cbDrug.ValueMember = "Id";

			cbPatient.DataSource = _viewModel.General.Context.Patients
				.Include(x => x.PatientNavigation)
				.Select(x => new ComboBoxItem
				{
					Name = x.PatientNavigation.FirstName + " " +
							x.PatientNavigation.FirstName + " " +
							x.PatientNavigation.LastName,
					Id = x.PatientId
				})
				.OrderBy(cbi => cbi.Name).ToList();
			cbPatient.DisplayMember = "Name";
			cbPatient.ValueMember = "Id";

			cbIntakeMethod.DataSource = _viewModel.General.Context.DrugIntakeMethods
				.Select(x => new ComboBoxItem { Name = x.Name, Id = x.DrugIntakeMethodId })
				.OrderBy(cbi => cbi.Name).ToList();
			cbIntakeMethod.DisplayMember = "Name";
			cbIntakeMethod.ValueMember = "Id";

			cbUnit.DataSource = _viewModel.General.Context.Units
				.Select(x => new ComboBoxItem { Name = x.Name, Id = x.UnitId })
				.OrderBy(cbi => cbi.Name).ToList();
			cbUnit.DisplayMember = "Name";
			cbUnit.ValueMember = "Id";
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			Prescription p = new()
			{
				DoctorId = _viewModel.General.DoctorId,
				PatientId = int.Parse(cbPatient.SelectedValue?.ToString() ?? "-1")
			};

			Prescription? prInDb = _viewModel.General.Context.Prescriptions
				.FirstOrDefault(x => x.PatientId == p.PatientId && x.DoctorId == p.DoctorId);
			int id = prInDb?.PrescriptionId ?? 0;
			if (prInDb == null)
			{
				id = _viewModel.AddPrescription(p);
				if (id == -1)
				{
					DialogResult = DialogResult.None;
					return;
				}
			}

			DrugTypePrescription pr = new()
			{
				PrescriptionId = id,
				DrugTypeId = int.Parse(cbDrug.SelectedValue?.ToString() ?? "0"),
				DoseUnitId = int.Parse(cbUnit.SelectedValue?.ToString() ?? "0"),
				IntakeMethodId = int.Parse(cbIntakeMethod.SelectedValue?.ToString() ?? "0"),
				Dose = nudDose.Value,
				DurationDays = (int)nudDurationDays.Value
			};
			if (_viewModel.AddPrescription(pr) == -1)
			{
				DialogResult = DialogResult.None;
				return;
			}

			MessageBox.Show("Лекарство успешно назначено", "Назначение пользователю",
								MessageBoxButtons.OK, MessageBoxIcon.Information);
			Close();

		}
	}
}
