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

namespace Register_office.View.PatientV
{
	public partial class ExaminationDataForm : Form
	{
		private PrescriptionViewModel _viewModel;
		private Prescription? _prescription;
		bool _isResult;
		public ExaminationDataForm()
		{
			Init();
			btnResult.Visible = false;
		}

		public ExaminationDataForm(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;
			cbExamination.SelectedValue = prescription.ExaminationTypePrescription.ExaminationTypeId;

			Text = "Просмотр обследования";
			btnOK.Visible = cbPatient.Enabled =
				cbExamination.Enabled = false;

			_isResult = prescription.ExaminationTypePrescription.ResultId != null;
			btnResult.Text = _isResult
				? "Посмотреть результаты"
				: "Добавить результаты";
			btnResult.Visible = true;
		}

		private void SetDataSourceToComboBoxes()
		{
			cbExamination.DataSource = _viewModel.General.Context.ExaminationTypes
				.Select(x => new ComboBoxItem { Name = x.Name, Id = x.ExaminationTypeId })
				.OrderBy(cbi => cbi.Name).ToList();
			cbExamination.DisplayMember = "Name";
			cbExamination.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";
		}

		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;
				}
			}

			ExaminationTypePrescription pr = new()
			{
				PrescriptionId = id,
				ExaminationTypeId = int.Parse(cbExamination.SelectedValue?.ToString() ?? "0")
			};
			if (_viewModel.AddPrescription(pr) == -1)
			{
				DialogResult = DialogResult.None;
				return;
			}

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

		}

		private void btnResult_Click(object sender, EventArgs e)
		{
			ResultDataForm rf;
			if (!_isResult)
			{
				rf = new(_prescription.ExaminationTypePrescription);
				if (rf.ShowDialog() == DialogResult.OK)
				{
					_isResult = true;
					btnResult.Text = "Посмотреть результаты";
				}
			}
			else
			{
				rf = new(_prescription.ExaminationTypePrescription, false);
				rf.ShowDialog();
			}

		}
	}
}
