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

namespace Register_office.View.TypesV
{
	public partial class ReferenceValueForm : Form
	{
		private TypeViewModel _viewModel;
		private ReferenceValue? _selectedType;
		private string _columnIdName = "ReferenceValueId";
		private ReferenceValue _temp;

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

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

			cbGender.DataSource = new string[] { "Мужчина", "Женщина" };
		}

		private void UpdateElements(int examination = -1, string gender = "",
			int? ageMin = null, int? ageMax = null, bool enableButtons = false)
		{
			cbExamination.SelectedValue = examination;
			cbGender.Text = gender;
			nudAgeMin.Value = ageMin ?? -1;
			nudAgeMax.Value = ageMax ?? -1;
			btnDelete.Enabled = btnUpdate.Enabled = btnOpen.Enabled = enableButtons;
		}

		private void UpdateDgv()
		{
			_viewModel.SetDefaultDataSource<ReferenceValue>(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() ?? "");
				_selectedType = _viewModel.General.Context.ReferenceValues.Where(x => x.ReferenceValueId == id).FirstOrDefault();
				if (_selectedType != null)
					UpdateElements(_selectedType.ExaminationTypeId, _selectedType.Gender,
						_selectedType.AgeMin, _selectedType.AgeMax, true);
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (_selectedType != null)
			{
				_selectedType.ExaminationTypeId = int.Parse(cbExamination.SelectedValue?.ToString() ?? "1");
				_selectedType.Gender = cbGender.Text;
				_selectedType.AgeMin = nudAgeMin.Value == -1 ? null : (int)nudAgeMin.Value;
				_selectedType.AgeMax = nudAgeMax.Value == -1 ? null : (int)nudAgeMax.Value;
				_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"),
				Gender = cbGender.Text,
				AgeMin = nudAgeMin.Value == -1 ? null : (int)nudAgeMin.Value,
				AgeMax = nudAgeMax.Value == -1 ? null : (int)nudAgeMax.Value,
			};
			_viewModel.AddType(_temp);
			UpdateElements();
			UpdateDgv();
		}

		private void btnOpen_Click(object sender, EventArgs e)
		{
			ReferenceValueQNForm f = new(_selectedType.ReferenceValueId);
			f.ShowDialog();
		}
	}
}
