using ConfectioneryContracts.BusinessLogicsContracts;
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 ConfectioneryView
{
    public partial class FormSellPastry : Form
    {
        private readonly IShopLogic _shopLogic; 
        private readonly IPastryLogic _pastryLogic; 

        public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _pastryLogic = logic;
            _shopLogic = shopLogic;
            var list = logic.ReadList(null);
            if (list != null)
            {
                comboBoxPastry.DisplayMember = "PastryName";
                comboBoxPastry.ValueMember = "Id";
                comboBoxPastry.DataSource = list;
                comboBoxPastry.SelectedItem = null;
            }
        }

        private void ButtonSell_Click(object sender, EventArgs e)
        {
            if (comboBoxPastry.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (numericUpDownCount.Value <= 0)
            {
                MessageBox.Show("Количество должно быть больше нуля", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var count = (int)numericUpDownCount.Value;
            var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue });
            if (pastry == null || !_shopLogic.SellPastries(pastry, count))
            {
                MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult = DialogResult.OK;
            Close();
        }

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