using Microsoft.EntityFrameworkCore;
using Register_office.Model;
using Register_office.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Register_office.View.TypesV
{
	public partial class ResultDataForm : Form
	{
		private ResultViewModel _viewModel;
		private ExaminationTypePrescription? _prescription;
		bool _isNumericResult;
		public ResultDataForm(ExaminationTypePrescription p, bool isForAdd = true)
		{
			Init();
			_prescription = p;
			cbExamination.SelectedValue = p.ExaminationTypeId;
			cbExamination.Enabled = false;
			if (!isForAdd) SetData(p);
		}

		private void Init()
		{
			InitializeComponent();
			TopMost = true;
			_viewModel = new();
			SetDataSourceToComboBoxes();
		}

		private void SetData(ExaminationTypePrescription prescription)
		{
			_prescription = _viewModel.General.Context.ExaminationTypePrescriptions
				.Include(x => x.Result)
				.ThenInclude(y => y.NumericResult)
				.Include(x => x.Result)
				.ThenInclude(y => y.QualitativeResult)
				.First(x => x.ExaminationPrescriptionId == prescription.ExaminationPrescriptionId);
			_isNumericResult = _prescription.Result.QualitativeResult == null;
			cbTypeResult.Text = _isNumericResult
				? "Числовой"
				: "Качественный";
			if (_isNumericResult)
			{
				nudCount.Value = _prescription.Result.NumericResult.Value;
				cbUnit.SelectedValue = _prescription.Result.NumericResult.UnitId;

				nudCount.Visible = cbUnit.Visible =
					lblCount.Visible = lblUnit.Visible = true;
				cbValue.Visible = lblBool.Visible = false;
			}
			else
			{
				cbValue.Text = _prescription.Result.QualitativeResult.Value
					? "Положительный"
					: "Отрицательный";
				nudCount.Visible = cbUnit.Visible =
					lblCount.Visible = lblUnit.Visible = false;
				cbValue.Visible = lblBool.Visible = true;
			}

			Text = "Просмотр результатов";
			btnOK.Visible = cbTypeResult.Enabled =
				nudCount.Enabled = cbUnit.Enabled =
				cbValue.Enabled = false;
		}

		private void SetDataSourceToComboBoxes()
		{
			cbTypeResult.SelectedItem = cbTypeResult.Items[0];
			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";

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

		private void btnOK_Click(object sender, EventArgs e)
		{
			Result r = new()
			{
				ExaminationId = int.Parse(cbExamination.SelectedValue?.ToString() ?? "-1")
			};

			int id = _viewModel.AddResult(r);
			if (id == -1)
			{
				DialogResult = DialogResult.None;
				return;
			}

			if (cbTypeResult.Text == "Качественный")
			{
				QualitativeResult qr = new()
				{
					ResultId = id,
					Value = cbValue.Text == "Положительный"
				};
				if (_viewModel.AddResult(qr) == -1)
				{
					DialogResult = DialogResult.None;
					_viewModel.DeleteResult(r);
					return;
				}
			}
			else
			{

				NumericResult nr = new()
				{
					ResultId = id,
					UnitId = int.Parse(cbUnit.SelectedValue?.ToString() ?? "0"),
					Value = nudCount.Value
				};
				if (_viewModel.AddResult(nr) == -1)
				{
					DialogResult = DialogResult.None;
					_viewModel.DeleteResult(r);
					return;
				}
			}

			_prescription.ResultId = id;
			_viewModel.Update();

			MessageBox.Show("Результаты успешно добавлены", "Результаты обследования",
								MessageBoxButtons.OK, MessageBoxIcon.Information);
			Close();

		}

		private void cbTypeResult_TextChanged(object sender, EventArgs e)
		{
			if (cbTypeResult.Text == "Качественный")
			{
				nudCount.Visible = cbUnit.Visible = 
					lblCount.Visible = lblUnit.Visible = false;
				cbValue.Visible = lblBool.Visible = true;
			}
			else
			{
				nudCount.Visible = cbUnit.Visible = 
					lblCount.Visible = lblUnit.Visible = true;
				cbValue.Visible = lblBool.Visible = false;
			}
		}
	}
}
