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

namespace Register_office.View.PatientV
{
	public partial class HospitalForm : Form
	{
		private HospitalViewModel _viewModel;
		private HospitalAdmission? _selectedHospital;
		private string _columnIdName = "HospitalAdmissionId";

		public HospitalForm()
		{
			Init();
		}

		public HospitalForm(Patient patient)
		{
			Init();
			dgv.DataSource = new SortableBindingList<HospitalAdmissionWrapper>(
						HospitalAdmissionWrapper.ToList(_viewModel.GetHospitalAdmission()
						.Where(x => x.PatientId == patient.PatientId)
						.ToList()));
		}

		private void Init()
		{
			InitializeComponent();
			TopMost = true;
			Text = "Госпитализации";
			_viewModel = new();
			_viewModel.ConfigureSettingsDGV<HospitalAdmissionWrapper>(dgv);
			dtpArrive.Value =
				dtpArrive.MaxDate = DateTime.Now;
			SetDataSourceToComboBoxes();
			UpdateDgv();
			UpdateElements();
		}

		private void SetDataSourceToComboBoxes()
		{
			cbDisease.DataSource = _viewModel.General.Context.Diseases
				.Select(x => new ComboBoxItem { Name = x.Name, Id = x.DiseaseId })
				.OrderBy(cbi => cbi.Name).ToList();
			cbDisease.DisplayMember = "Name";
			cbDisease.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 UpdateElements(bool enableButtons = false)
		{
			if (_selectedHospital != null)
			{
				cbPatient.SelectedValue = _selectedHospital.PatientId;
				cbDisease.SelectedValue = _selectedHospital.PreliminaryDiseaseId;
				dtpArrive.Value = _selectedHospital.AdmissionDatetime;
				btnHospitalDischarge.Enabled = true;
				btnHospitalDischarge.Text = _selectedHospital.HospitalDischarges.Any()
					? "Результаты госпитализации"
					: "Завершить госпитализацию";
			}

			btnDelete.Enabled = btnUpdate.Enabled = btnHospitalDischarge.Enabled = enableButtons;
		}

		private void UpdateDgv()
		{
			_viewModel.SetDefaultDataSource<HospitalAdmission>(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() ?? "");
				_selectedHospital = _viewModel.GetHospitalAdmission(id).FirstOrDefault();
				if (_selectedHospital != null)
					UpdateElements(true);
			}
		}

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

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (_selectedHospital != null)
			{
				_selectedHospital.PatientId = int.Parse(cbPatient.SelectedValue?.ToString() ?? "-1");
				_selectedHospital.PreliminaryDiseaseId = int.Parse(cbDisease.SelectedValue?.ToString() ?? "-1");
				_selectedHospital.AdmissionDatetime = dtpArrive.Value;
				_viewModel.Update();
				UpdateElements();
				UpdateDgv();
			}
		}

		private void btnAddHospitalAdmission_Click(object sender, EventArgs e)
		{
			HospitalAdmission _temp = new()
			{
				PatientId = int.Parse(cbPatient.SelectedValue?.ToString() ?? "-1"),
				PreliminaryDiseaseId = int.Parse(cbDisease.SelectedValue?.ToString() ?? "-1"),
				AdmissionDatetime = dtpArrive.Value
			};
			_viewModel.AddHospital(_temp);
			UpdateElements();
			UpdateDgv();
		}

		private void btnHospitalDischarge_Click(object sender, EventArgs e)
		{
			HospitalDischargeDataForm hdf;

			if (_selectedHospital.HospitalDischarges.Any())
			{
				hdf = new(_selectedHospital, false);
				hdf.ShowDialog();
			}
			else
			{
				hdf = new(_selectedHospital);
				if (hdf.ShowDialog() == DialogResult.OK)
				{
					btnHospitalDischarge.Text = "Результаты госпитализации";
				}
			}

		}
	}
}
