using Microsoft.Extensions.Logging;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.ViewModels;
using System;
using System.Collections;
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 PrecastConcretePlantView
{
    public partial class FormReinforcedShop : Form
    {
        private readonly ILogger _logger;
        private readonly IShopLogic _shopLogic;
        private readonly IReinforcedLogic _reinforcedLogic;
        private readonly List<ShopViewModel>? _listShops;
        private readonly List<ReinforcedViewModel>? _listReinforcedies;

        public FormReinforcedShop(ILogger<FormReinforcedShop> logger, IShopLogic shopLogic, IReinforcedLogic reinforcedLogic)
        {
            InitializeComponent();
            _shopLogic = shopLogic;
            _reinforcedLogic = reinforcedLogic;
            _logger = logger;
            _listShops = shopLogic.ReadList(null);
            if (_listShops != null)
            {
                comboBoxShop.DisplayMember = "Name";
                comboBoxShop.ValueMember = "Id";
                comboBoxShop.DataSource = _listShops;
                comboBoxShop.SelectedItem = null;
            }

            _listReinforcedies = reinforcedLogic.ReadList(null);
            if (_listReinforcedies != null)
            {
                comboBoxReinforced.DisplayMember = "ReinforcedName";
                comboBoxReinforced.ValueMember= "Id";
                comboBoxReinforced.DataSource = _listReinforcedies;
                comboBoxReinforced.SelectedItem = null;
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (comboBoxShop.SelectedValue == null)
            {
                MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxReinforced.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Добавление изделия в магазин");
            try
            {
                var reinforced = _reinforcedLogic.ReadElement(new() 
                { 
                    Id = (int)comboBoxReinforced.SelectedValue 
                });
                if (reinforced == null) 
                {
                    throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
                }
                var resultOperation = _shopLogic.AddReinforced(
                     model: new() { Id = (int)comboBoxShop.SelectedValue },
                    reinforced: reinforced,
                     count: (int)numericUpDownCount.Value
                );
                if (!resultOperation)
                {
                    throw new Exception("Ошибка при добавлении. Дополнительная информация в логах.");
                }
                MessageBox.Show("Сохранение прошло успешно", "Сообщение",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка сохранения изделия");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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