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

namespace Register_office.View.TypesV
{
	public partial class SyndromeExaminationTypeForm : Form
	{
		private TypeViewModel _viewModel;
		private SyndromeExaminationType? _selectedType;
		private SyndromeExaminationType _temp;

		public SyndromeExaminationTypeForm()
		{
			InitializeComponent();
			TopMost = true;
			Text = "Справочник соответствий синдромов и обследований";
			_viewModel = new();
			_viewModel.ConfigureSettingsDGV<SyndromeExaminationTypeWrapper>(dgv);
			UpdateDgv();
			SetDataSourceToComboBoxes();
			UpdateElements();
		}

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

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

		private void UpdateElements(int idExam = -1, int idSynd = -1, bool enableButtons = false)
		{
			cbExamination.SelectedValue = idExam;
			cbSyndrome.SelectedValue = idSynd;
			btnDelete.Enabled = btnUpdate.Enabled = enableButtons;
		}

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

		private void dgv_SelectionChanged(object sender, EventArgs e)
		{
			if (dgv.SelectedRows.Count != 0 && !dgv.CurrentRow.IsNewRow)
			{
				DataGridViewRow selectedRow = dgv.SelectedRows[0];
				int idExam = int.Parse(selectedRow.Cells["ExaminationTypeId"].Value.ToString() ?? "");
				int idSynd = int.Parse(selectedRow.Cells["SyndromeId"].Value.ToString() ?? "");
				_selectedType = _viewModel.General.Context.SyndromeExaminationTypes
					.Where(x => x.ExaminationTypeId == idExam && x.SyndromeId == idSynd).FirstOrDefault();
				if (_selectedType != null)
					UpdateElements(_selectedType.ExaminationTypeId, _selectedType.SyndromeId, true);
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (_selectedType != null)
			{
				_selectedType.ExaminationTypeId = int.Parse(cbExamination.SelectedValue?.ToString() ?? "-1");
				_selectedType.SyndromeId = int.Parse(cbSyndrome.SelectedValue?.ToString() ?? "-1");
				_viewModel.General.Context.SaveChanges();
				UpdateElements();
				UpdateDgv();
			}
		}

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

		private void btnAdd_Click(object sender, EventArgs e)
		{
			_temp = new()
			{
				ExaminationTypeId = int.Parse(cbExamination.SelectedValue?.ToString() ?? "-1"),
				SyndromeId = int.Parse(cbSyndrome.SelectedValue?.ToString() ?? "-1")
			};
			_viewModel.AddType(_temp);
			UpdateElements();
			UpdateDgv();
		}
	}
}
