using Register_office;
using Register_office.Model;
using Register_office.View.LoginV;
using Register_office.View.PatientV;
using Register_office.View.TypesV;
using Register_office.ViewModel;
using System.Data;

namespace Register_office.View.MenuV
{
	public partial class OnlyMenuForm : Form
	{
		OnlyMenuViewModel _viewModel;

		public OnlyMenuForm()
		{
			InitializeComponent();
			_viewModel = new();
			SetFormByRole();
		}

		private void SetFormByRole()
		{
			if (_viewModel.General.DoctorId < 1)
			{
				справочникиToolStripMenuItem.Visible =
					госпитализацииToolStripMenuItem.Visible =
					отчетыToolStripMenuItem.Visible = false;
			}
		}

		private T ShowForm<T>() where T : Form, new()
		{
			T? form = _viewModel.General.GetActivatedForm<T>();

			form ??= new();

			form.Visible = true;
			form.Show();
			return form;
		}

		private void ReportAnalyses()
		{

			List<Analysis> sales = _viewModel.GetReport<Analysis>();

			string fileName = _viewModel.General.PathReport + "/Результаты анализов " +
				 DateOnly.FromDateTime(DateTime.Now) + ".xlsx";

			ExcelManager.ExportToExcel(sales, fileName);
			ExcelManager.OpenExcelFile(fileName);
		}

		private void HideForms()
		{
			FormCollection formCollection = Application.OpenForms;
			bool hide = false;
			if (this.WindowState == FormWindowState.Minimized)
				hide = false;
			else if (this.WindowState == FormWindowState.Maximized)
				hide = true;

			foreach (Form form in formCollection)
				if (form.Name != this.Name && form.Name != new LoginForm().Name)
					form.Visible = hide;
		}

		private void Form_SizeChanged(object sender, EventArgs e)
		{
			HideForms();
		}

		private void заболеваниеToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			ShowForm<DiseaseForm>();
		}

		private void способыприемалекарствToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<DrugIntakeMethodForm>();
		}

		private void обследованияToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<ExaminationTypeForm>();
		}

		private void процедурыToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<PatientForm>();
		}

		private void нормалиОбследованийToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<ReferenceValueForm>();
		}

		private void синдромыToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<SyndromeTypeForm>();
		}

		private void CоответствиеСиндромовИОбследованийToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<SyndromeExaminationTypeForm>();
		}

		private void единицыИзмеренияToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<UnitForm>();
		}

		private void лекарстваToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<DrugTypeForm>();
		}

		private void соответствиеЗаболеванийИСиндромовToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<DiseaseSyndromeForm>();
		}

		private void пациентыToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<PatientForm>();
		}

		private void добавитьПациентаToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<PatientDataForm>();
		}

		private void назначенияToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<PrescriptionForm>();
		}

		private void госпитализацииToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm<HospitalForm>();
		}

		private void результатыАнализовToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ReportAnalyses();
		}
	}
}
