using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Register_office.Model;
using Register_office.ViewModel;
using System.Data;
using System.Xml.Linq;

namespace Register_office.View.PatientV
{
	public partial class PrescriptionForm : Form
	{
		private PrescriptionViewModel _viewModel;
		private Prescription? _selectedPrescription;
		private string _columnIdName = "PrescriptionId";

		public PrescriptionForm()
		{
			Init();
		}

		public PrescriptionForm(Patient patient)
		{
			Init();
			dgv.DataSource = new SortableBindingList<PrescriptionWrapper>(
				PrescriptionWrapper.ToList(_viewModel.General.Context.Prescriptions
				.Where(x => x.PatientId == patient.PatientId)
				.ToList()));
		}

		private void Init()
		{
			InitializeComponent();
			TopMost = true;
			Text = "Назначения";
			_viewModel = new();
			_viewModel.ConfigureSettingsDGV<PrescriptionWrapper>(dgv);
			UpdateDgv();
			UpdateElements();
		}

		private void UpdateElements(bool enableButtons = false)
		{
			if (_selectedPrescription == null)
				btnDrugShow.Enabled = btnExaminationShow.Enabled = btnProcedureShow.Enabled = false;
			else
			{
				btnDrugShow.Enabled = _selectedPrescription.DrugTypePrescription != null;
				btnExaminationShow.Enabled = _selectedPrescription.ExaminationTypePrescription != null;
				btnProcedureShow.Enabled = _selectedPrescription.ProcedureTypePrescription != null;
			}
			btnDelete.Enabled = enableButtons;
		}

		private void UpdateDgv()
		{
			_viewModel.SetDefaultDataSource<Prescription>(dgv);
		}

		private void dgv_SelectionChanged(object sender, EventArgs e)
		{
			if (dgv.SelectedRows.Count != 0 && !dgv.CurrentRow.IsNewRow)
			{
				DataGridViewRow selectedRow = dgv.SelectedRows[0];
				int id = int.Parse(selectedRow.Cells[_columnIdName].Value.ToString() ?? "");
				_selectedPrescription = _viewModel.General.Context.Prescriptions
					.Include(x => x.ProcedureTypePrescription)
					.Include(x => x.DrugTypePrescription)
					.Include(x => x.ExaminationTypePrescription)
					.Where(x => x.PrescriptionId == id).FirstOrDefault();
				if (_selectedPrescription != null)
					UpdateElements(true);
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (_selectedPrescription != null)
			{
				_viewModel.DeletePrescription(_selectedPrescription);
				UpdateElements();
				UpdateDgv();
			}
		}

		private void btnProcedureAdd_Click(object sender, EventArgs e)
		{
			ProcedureDataForm pf = new();
			pf.ShowDialog();
			UpdateDgv();
		}

		private void btnProcedureShow_Click(object sender, EventArgs e)
		{
			ProcedureDataForm pf = new(_selectedPrescription);
			pf.ShowDialog();
		}

		private void btnDrugAdd_Click(object sender, EventArgs e)
		{
			DrugDataForm pf = new();
			pf.ShowDialog();
			UpdateDgv();
		}

		private void btnDrugShow_Click(object sender, EventArgs e)
		{
			DrugDataForm pf = new(_selectedPrescription);
			pf.ShowDialog();
		}

		private void btnExaminationAdd_Click(object sender, EventArgs e)
		{
			ExaminationDataForm pf = new();
			pf.ShowDialog();
			UpdateDgv();
		}

		private void btnExaminationShow_Click(object sender, EventArgs e)
		{
			ExaminationDataForm pf = new(_selectedPrescription);
			pf.ShowDialog();
		}
	}
}
