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

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

		public DiseaseSyndromeForm()
		{
			InitializeComponent();
			TopMost = true;
			Text = "Справочник соответствий заболеваний и синдромов";
			_viewModel = new();
			_viewModel.ConfigureSettingsDGV<DiseaseSyndromeWrapper>(dgv);
			UpdateDgv();
			SetDataSourceToComboBoxes();
			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";

			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 idDis = -1, int idSynd = -1, bool enableButtons = false)
		{
			cbDisease.SelectedValue = idDis;
			cbSyndrome.SelectedValue = idSynd;
			btnDelete.Enabled = btnUpdate.Enabled = enableButtons;
		}

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

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

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (_selectedType != null)
			{
				_selectedType.DiseaseId = int.Parse(cbDisease.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()
			{
				DiseaseId = int.Parse(cbDisease.SelectedValue?.ToString() ?? "-1"),
				SyndromeId = int.Parse(cbSyndrome.SelectedValue?.ToString() ?? "-1")
			};
			_viewModel.AddType(_temp);
			UpdateElements();
			UpdateDgv();
		}
	}
}
