using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierContracts.SearchModels;
using DressAtelierDataModels.Models;
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 FormDress : Form
    {
        private readonly ILogger _logger;

        private readonly IDressLogic _logic;

        private int? _id;

        private Dictionary<int, (IMaterialModel, int)> _dressComponents;

        public int ID { set { _id = value; } }

        public FormDress(ILogger<FormDress> logger, IDressLogic logic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = logic;
            _dressComponents = new Dictionary<int, (IMaterialModel, int)>();

        }

        private void FormDress_Load(object sender, EventArgs e)
        {
            if (_id.HasValue)
            {
                _logger.LogInformation("Downloading dresses");
                try
                {
                    var view = _logic.ReadElement(new DressSearchModel
                    {
                        ID = _id.Value,
                    });
                    if (view != null)
                    {
                        nameDressTextBox.Text = view.DressName;
                        priceDressTextBox.Text = view.Price.ToString();
                        _dressComponents = view.DressComponents ?? new Dictionary<int, (IMaterialModel, int)>();
                        LoadData();
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Downloading dress error");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                }
            }
        }

        private void LoadData()
        {
            _logger.LogInformation("Downloading material for dress");
            try
            {
                if (_dressComponents != null)
                {
                    dressGridView.Rows.Clear();
                    foreach (var pc in _dressComponents)
                    {
                        dressGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 });
                    }
                    priceDressTextBox.Text = CalcPrice().ToString();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error downloading material for dress");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
               MessageBoxIcon.Error);
            }
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            var service = Program.ServiceProvider?.GetService(typeof(FormDressMaterial));
            if (service is FormDressMaterial form)
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    if (form.MaterialModel == null)
                    {
                        return;
                    }

                    _logger.LogInformation("Adding new material: { ComponentName}- { Count}", form.MaterialModel.ComponentName, form.Count);

                    if (_dressComponents.ContainsKey(form.ID))
                    {
                        _dressComponents[form.ID] = (form.MaterialModel, form.Count);
                    }
                    else
                    {
                        _dressComponents.Add(form.ID, (form.MaterialModel, form.Count));
                    }
                    LoadData();

                }
            }
        }

        private void ButtonUpdate_Click(object sender, EventArgs e)
        {
            if (dressGridView.SelectedRows.Count == 1)
            {
                var service = Program.ServiceProvider?.GetService(typeof(FormDressMaterial));
                if (service is FormDressMaterial form)
                {
                    int id = Convert.ToInt32(dressGridView.SelectedRows[0].Cells[0].Value);
                    form.ID = id;
                    form.Count = _dressComponents[id].Item2;
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        if (form.MaterialModel == null)
                        {
                            return;
                        }
                        _logger.LogInformation("Changing material:{ ComponentName}- { Count}", form.MaterialModel.ComponentName, form.Count);
                        _dressComponents[form.ID] = (form.MaterialModel,form.Count);
                        LoadData();
                    }
                }
            }
        }

        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (dressGridView.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("Delete record?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        _logger.LogInformation("Deletion material:{ ComponentName} - { Count}", dressGridView.SelectedRows[0].Cells[1].Value);
                        _dressComponents?.Remove(Convert.ToInt32(dressGridView.SelectedRows[0].Cells[0].Value));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    LoadData();
                }
            }
        }

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

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(nameDressTextBox.Text))
            {
                MessageBox.Show("Fill field name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(priceDressTextBox.Text))
            {
                MessageBox.Show("Fill field price", "Error", MessageBoxButtons.OK,
               MessageBoxIcon.Error);
                return;
            }
            if (_dressComponents == null || _dressComponents.Count == 0)
            {
                MessageBox.Show("Fill materials", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Saving dress");
            try
            {
                var model = new DressBindingModel
                {
                    ID = _id ?? 0,
                    DressName = nameDressTextBox.Text,
                    Price = Convert.ToDouble(priceDressTextBox.Text),
                    DressComponents = _dressComponents
                };
                var operationResult = _id.HasValue ? _logic.Update(model) :
               _logic.Create(model);
                if (!operationResult)
                {
                    throw new Exception("Saving error. Extra information in logs.");
                }
                MessageBox.Show("Saving was succesfull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Saving dress error");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();

        }

        private double CalcPrice()
        {
            double price = 0;
            foreach (var elem in _dressComponents)
            {
                price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
            }
            return Math.Round(price * 1.1, 2);
        }
    }
}