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 ReferenceValueQNForm : Form
	{
		private TypeViewModel _viewModel;
		private int _referenceValueId;
		private ReferenceNumericValue? _referenceNumericValue;
		private ReferenceQualitativeValue? _referenceQualitativeValue;

		public ReferenceValueQNForm(int referenceValueId)
		{
			InitializeComponent();
			TopMost = true;
			_viewModel = new();
			_referenceValueId = referenceValueId;
			_referenceNumericValue = _viewModel.General.Context.ReferenceNumericValues
				.Where(x => x.ReferenceValueId == _referenceValueId).FirstOrDefault();
			_referenceQualitativeValue = _viewModel.General.Context.ReferenceQualitativeValues
				.Where(x => x.ReferenceValueId == _referenceValueId).FirstOrDefault();
			SetDataSourceToComboBoxes();
			GetReferenceValues();
		}

		private void SetDataSourceToComboBoxes()
		{
			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";
			cbUnit.SelectedValue = -1;
		}

		private void GetReferenceValues()
		{
			if (_referenceNumericValue != null)
			{
				nudMinValue.Value = _referenceNumericValue.MinValue ?? -1;
				nudMaxValue.Value = _referenceNumericValue.MaxValue ?? -1;
				cbUnit.SelectedValue = _referenceNumericValue.UnitId;
			}

			if (_referenceQualitativeValue != null)
			{
				rtbDescriptionFalse.Text = _referenceQualitativeValue.DescriptionFalse;
				rtbDescriptionTrue.Text = _referenceQualitativeValue.DescriptionTrue;
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if ((nudMinValue.Value >= 0 || nudMaxValue.Value >= 0) && cbUnit.Text != "")
			{
				if (_referenceNumericValue == null)
				{
					_referenceNumericValue = new()
					{
						ReferenceValueId = _referenceValueId,
						MinValue = nudMinValue.Value < 0 ? null : (int)nudMinValue.Value,
						MaxValue = nudMaxValue.Value < 0 ? null : (int)nudMaxValue.Value,
						UnitId = int.Parse(cbUnit.SelectedValue?.ToString() ?? "1")
					};
					_viewModel.AddType(_referenceNumericValue);
				}
				else
				{
					_referenceNumericValue.ReferenceValueId = _referenceValueId;
					_referenceNumericValue.MinValue = nudMinValue.Value < 0 ? null : (int)nudMinValue.Value;
					_referenceNumericValue.MaxValue = nudMaxValue.Value < 0 ? null : (int)nudMaxValue.Value;
					_referenceNumericValue.UnitId = int.Parse(cbUnit.SelectedValue?.ToString() ?? "1");
					_viewModel.General.Context.SaveChanges();
				}
			}

			if (rtbDescriptionFalse.Text.Trim() != "" && rtbDescriptionTrue.Text.Trim() != "")
			{
				if (_referenceQualitativeValue == null)
				{
					_referenceQualitativeValue = new()
					{
						ReferenceValueId = _referenceValueId,
						DescriptionFalse = rtbDescriptionFalse.Text,
						DescriptionTrue = rtbDescriptionTrue.Text
					};
					_viewModel.AddType(_referenceQualitativeValue);
				}
				else
				{
					_referenceQualitativeValue.ReferenceValueId = _referenceValueId;
					_referenceQualitativeValue.DescriptionFalse = rtbDescriptionFalse.Text;
					_referenceQualitativeValue.DescriptionTrue = rtbDescriptionTrue.Text;
					_viewModel.General.Context.SaveChanges();
				}
			}
		}
	}
}
