using Microsoft.Extensions.Logging;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
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 SushiBarView
{
    public partial class FormSellSushi : Form
    {
        private readonly ILogger _logger;
        private readonly ISushiLogic _logicP;
        private readonly IShopLogic _logicS;
        private List<SushiViewModel> _sushiList = new List<SushiViewModel>();

        public FormSellSushi(ILogger<FormSellSushi> logger, ISushiLogic logicP, IShopLogic logicS)
        {
            InitializeComponent();
            _logger = logger;
            _logicP = logicP;
            _logicS = logicS;
        }

        private void FormSellingSushi_Load(object sender, EventArgs e)
        {
            _sushiList = _logicP.ReadList(null);
            if (_sushiList != null)
            {
                comboBoxSushi.DisplayMember = "SushiName";
                comboBoxSushi.ValueMember = "Id";
                comboBoxSushi.DataSource = _sushiList;
                comboBoxSushi.SelectedItem = null;
                _logger.LogInformation("Загрузка суши для продажи");
            }
        }

        private void ButtonSell_Click(object sender, EventArgs e)
        {
            if (comboBoxSushi.SelectedValue == null)
            {
                MessageBox.Show("Выберите суши", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Создание покупки");
            try
            {
                bool resout = _logicS.Sale(new SupplySearchModel
                {
                    SushiId = Convert.ToInt32(comboBoxSushi.SelectedValue),
                    Count = Convert.ToInt32(textBoxCount.Text)
                });
                if (resout)
                {
                    _logger.LogInformation("Проверка пройдена, продажа  проведена");
                    MessageBox.Show("Продажа проведена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    _logger.LogInformation("Проверка не пройдена");
                    MessageBox.Show("Продажа не может быть создана.", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            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();
        }
    }
}