using SewingDressesContracts.BindingModels;
using SewingDressesContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
using System.Windows.Forms;
using SewingDressesBusinessLogic.BusinessLogic;

namespace SewingDressesView
{
	public partial class MainForm : Form
	{
		private readonly ILogger _logger;
		private readonly IOrderLogic _orderLogic;
		private readonly IReportLogic _reportLogic;
		private readonly IWorkProcess _workProcess;
		public MainForm(ILogger<MainForm> logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkProcess workProcess)
		{
			InitializeComponent();
			_logger = logger;
			_orderLogic = orderLogic;
			_reportLogic = reportLogic;
			_workProcess = workProcess;
		}
		private void LoadData()
		{
			try
			{
				var list = _orderLogic.ReadList(null);
				if (list != null)
				{
					dataGridView.DataSource = list;
					dataGridView.Columns["DressId"].Visible = false;
					dataGridView.Columns["ClientId"].Visible = false;
					dataGridView.Columns["ImplementId"].Visible = false;
				}
				_logger.LogInformation("Load orders");
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Load orders error");
			}
		}

		private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(ComponentsForm));
			if (service is ComponentsForm form)
			{
				form.ShowDialog();
			}
		}

		private void DressesToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(DressesForm));
			if (service is DressesForm form)
			{
				form.ShowDialog();
			}
		}

		private void buttonCreate_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(OrderForm));
			if (service is OrderForm form)
			{
				form.ShowDialog();
				LoadData();
			}
		}


		private void buttonOrderGive_Click(object sender, EventArgs e)
		{
			if (dataGridView.SelectedRows.Count == 1)
			{
				int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
				_logger.LogInformation("Order №{id}. Status change on 'Given'", id);
				try
				{
					var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id });
					if (!operationResult)
					{
						throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
					}
					_logger.LogInformation("Order №{id} given", id);
					LoadData();
				}
				catch (Exception ex)
				{
					_logger.LogError(ex, "Change status on 'Give' error");
					MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void buttonOrderUpdate_Click(object sender, EventArgs e)
		{
			LoadData();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			LoadData();
		}
		private void ComponentProductsToolStripMenuItemReport_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(ReportDressComponentsForm));
			if (service is ReportDressComponentsForm form)
			{
				form.ShowDialog();
			}
		}
		private void OrdersToolStripMenuItemReport_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(ReportOrdersForm));
			if (service is ReportOrdersForm form)
			{
				form.ShowDialog();
			}
		}

		private void DressesToolStripMenuItemReport_Click(object sender, EventArgs e)
		{
			using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				_reportLogic.SaveComponentsToWordFile(new ReportBindingModel
				{
					FileName = dialog.FileName
				});
				MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		private void clientsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(ClientsForm));
			if (service is ClientsForm form)
			{
				form.ShowDialog();
			}
		}

		private void ImplementToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(ImplementsForm));
			if (service is ImplementsForm form)
			{
				form.ShowDialog();
			}
		}

		private void запускРаботToolStripMenuItem_Click(object sender, EventArgs e)
		{
			_workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementLogic)) as IImplementLogic)!, _orderLogic);
			MessageBox.Show("Процесс обработки запущен", "Сообщение",
		   MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		private void emailsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var service = Program.ServiceProvider?.GetService(typeof(MailForm));
			if (service is MailForm form)
			{
				form.ShowDialog();
			}
		}
	}
}
