using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
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 FormDresses : Form
    {
        private readonly ILogger _logger;
        private readonly IDressLogic _logic;

        public FormDresses(ILogger<FormDresses> logger, IDressLogic logic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = logic;
        }

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

        private void LoadData()
        {
            try
            {
                var list = _logic.ReadList(null);
                
                if (list != null)
                {
                    dressGridView.DataSource = list;
                    dressGridView.Columns["ID"].Visible = false;
                    dressGridView.Columns["DressName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dressGridView.Columns["DressComponents"].Visible = false;
                }
                _logger.LogInformation("Loading dresses");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error loading dresses");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void ButtonUpdate_Click(object sender, EventArgs e)
        {
            if (dressGridView.SelectedRows.Count == 1)
            {
                var service = Program.ServiceProvider?.GetService(typeof(FormDress));
                if (service is FormDress form)
                {
                    form.ID = Convert.ToInt32(dressGridView.SelectedRows[0].Cells["ID"].Value);
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        LoadData();
                    }

                }
            }
        }

        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (dressGridView.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int id = Convert.ToInt32(dressGridView.SelectedRows[0].Cells["ID"].Value);
                    _logger.LogInformation("Dress deletion");
                    try
                    {
                        if (!_logic.Delete(new DressBindingModel
                        {
                            ID = id
                        }))
                        {
                            throw new Exception("Deletion error. Extra information in logs.");
                        }
                        LoadData();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Dress deletion error");
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

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


    }
}
