using DressAtelierBusinessLogic.BusinessLogic;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierDataModels.Enums;
using Microsoft.Extensions.Logging;
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 SewingDresses
{
    public partial class FormMain : Form
    {
        private readonly ILogger _logger;

        private readonly IOrderLogic _orderLogic;
        private readonly IReportLogic _reportLogic;
        private readonly IWorkImitation _workImitation;
        public FormMain(ILogger<FormMain> logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkImitation workImitation)
        {
            InitializeComponent();
            _logger = logger;
            _orderLogic = orderLogic;
            _reportLogic = reportLogic;
            _workImitation = workImitation;
        }

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

        private void LoadData()
        {
            _logger.LogInformation("Downloading orders");
            try
            {
                var list = _orderLogic.ReadList(null);
                if (list != null)
                {
                    dataGridView.DataSource = list;
                    dataGridView.Columns["DressID"].Visible = false;
                    dataGridView.Columns["ClientID"].Visible = false;
                    dataGridView.Columns["EmployeeID"].Visible = false;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error downloading orders");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
               MessageBoxIcon.Error);
            }
        }

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

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

        }

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

        private void ButtonIssuedOrder_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value);
                _logger.LogInformation("Order №{id}. Changing status to 'Given'", id);
                try
                {
                    var operationResult = _orderLogic.GivenOrder(new OrderBindingModel
                    {
                        ID = id,
                    });
                    if (!operationResult)
                    {
                        MessageBox.Show("Order must be in 'Ready' state.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        throw new Exception("Saving error. Extra information in logs.");
                    }
                    _logger.LogInformation("Order №{id} is given", id);
                    LoadData();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error in giving 'Is given' status to order");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
                }
            }
        }
        private void ButtonRef_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void materialsListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                _reportLogic.SaveMaterialsToWordFile(new ReportBindingModel
                {
                    FileName = dialog.FileName
                });
                MessageBox.Show("Done", "Success", MessageBoxButtons.OK,
               MessageBoxIcon.Information);
            }


        }

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

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

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

        private void startWorkingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _workImitation.DoWork((Program.ServiceProvider?.GetService(typeof(IEmployeeLogic)) as IEmployeeLogic)!, _orderLogic);
            MessageBox.Show("Work process has started!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

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