using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DataModels.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class FormSuppliers : Form
    {
        private readonly ILogger _logger;
        private readonly ISupplierLogic _supplierLogic;
        private readonly IProductLogic _productLogic;
        public FormSuppliers(ILogger<FormMain> logger, ISupplierLogic supplierLogic, IProductLogic productLogic)
        {
            InitializeComponent();
            _supplierLogic = supplierLogic;
            _logger = logger;
            _productLogic = productLogic;
        }

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

        private void LoadData()
        {
            try
            {
                var list = _supplierLogic.ReadList(null);
                if (list != null)
                {
                    dataGridView.DataSource = list;
                }
                _logger.LogInformation("Загрузка компьютеров");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка загрузки компьютеров");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void buttonEdit_Click(object sender, EventArgs e)
        {

            if (dataGridView.SelectedRows.Count == 1)
            {
                var service = Program.ServiceProvider?.GetService(typeof(FormSupplier));
                if (service is FormSupplier form)
                {
                    form.Id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value;
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
            }
        }
    }
}
