using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace SushiBarView
{
    public partial class FormCreateSupply : Form
    {
        private readonly ILogger _logger;
        private readonly ISushiLogic _logicP;
        private readonly IShopLogic _logicS;
        private List<ShopViewModel> _shopList = new List<ShopViewModel>();
        private List<SushiViewModel> _sushiList = new List<SushiViewModel>();

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

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

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (comboBoxShop.SelectedValue == null)
            {
                MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxSushi.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Создание поставки");
            try
            {
                var operationResult = _logicS.MakeSupply(new SupplyBindingModel
                {
                    ShopId = Convert.ToInt32(comboBoxShop.SelectedValue),
                    SushiId = Convert.ToInt32(comboBoxSushi.SelectedValue),
                    Count = Convert.ToInt32(textBoxCount.Text)
                });
                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();
        }
    }
}
