using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
using IceCreamShopContracts.BindingModels;

namespace IceCreamShopView
{
    public partial class FormSellIceCream : Form
    {
        private readonly ILogger _logger;
        private readonly IIceCreamLogic _logicI;
        private readonly IShopLogic _logicS;
        public FormSellIceCream(ILogger<FormSellIceCream> logger, IIceCreamLogic logicIceCream, IShopLogic logicShop)
        {
            InitializeComponent();
            _logger = logger;
            _logicI = logicIceCream;
            _logicS = logicShop;
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(QuantityTextBox.Text))
            {
                MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (IceCreamСomboBox.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            _logger.LogInformation("Product sale.");

            try
            {
                var operationResult = _logicS.SellIceCreams(
                    new IceCreamBindingModel
                    {
                        Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue)
                    },
                    Convert.ToInt32(QuantityTextBox.Text)
                );

                if (!operationResult)
                {
                    throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах.");
                }

                MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;

                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Product sale error.");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void FormSellIceCream_Load(object sender, EventArgs e)
        {
            _logger.LogInformation("Loading icecream for sale.");

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