using DressAtelierContracts.BusinessLogicContracts;
using Microsoft.EntityFrameworkCore.Diagnostics;
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 FormEmployees : Form
    {
        private readonly ILogger _logger;
        private readonly IEmployeeLogic _logic;

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

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

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

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

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

    }
}
