using Microsoft.Extensions.Logging;
using PrecastConcretePlantBusinessLogic.BusinessLogic;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;

namespace PrecastConcretePlantView
{
    public partial class FormSellReinforced : Form
    {
        private readonly ILogger _logger;
        private readonly IReinforcedLogic _logicReinforced;
        private readonly IShopLogic _logicShop;
        public FormSellReinforced(ILogger<FormSellReinforced> logger, IReinforcedLogic logicReinforced, IShopLogic logicShop)
        {
            InitializeComponent();
            _logger = logger;
            _logicReinforced = logicReinforced;
            _logicShop = logicShop;
            LoadData();
        }

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

        private void LoadData()
        {
            _logger.LogInformation("Loading reinforcediss for sale.");

            try
            {
                var list = _logicReinforced.ReadList(null);
                if (list != null)
                {
                    ReindorcedСomboBox.DisplayMember = "ReinforcedName";
                    ReindorcedСomboBox.ValueMember = "Id";
                    ReindorcedСomboBox.DataSource = list;
                    ReindorcedСomboBox.SelectedItem = null;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "List loading error.");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (ReindorcedСomboBox.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(numericUpDownCount.Text))
            {
                MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Продажа изделий");
            try
            {
                var reinforced = _logicReinforced.ReadElement(new()
                {
                    Id = (int)ReindorcedСomboBox.SelectedValue
                });
                if (reinforced == null)
                {
                    throw new Exception("Изделие не найдено. Дополнительная информация в логах.");
                }
                var operationResult = _logicShop.SellReinforced(
                    reinforced: reinforced,
                    quantity: (int)numericUpDownCount.Value
                );
                if (!operationResult)
                {
                    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();
        }
    }
}
