using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using Microsoft.Extensions.Logging;
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 BlacksmithWorkshop
{
    public partial class FormSell : Form
    {
        private readonly List<ManufactureViewModel>? _manufactureList;
        IShopLogic _shopLogic;
        IManufactureLogic _manufactureLogic;
        public FormSell(IManufactureLogic manufactureLogic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _shopLogic = shopLogic;
            _manufactureLogic = manufactureLogic;
            _manufactureList = manufactureLogic.ReadList(null);
            if (_manufactureList != null)
            {
                ManufactureComboBox.DisplayMember = "ManufactureName";
                ManufactureComboBox.ValueMember = "Id";
                ManufactureComboBox.DataSource = _manufactureList;
                ManufactureComboBox.SelectedItem = null;
            }
        }
        public int ManufactureId
        {
            get
            {
                return Convert.ToInt32(ManufactureComboBox.SelectedValue);
            }
            set
            {
                ManufactureComboBox.SelectedValue = value;
            }
        }
        public IManufactureModel? ManufactureModel
        {
            get
            {
                if (_manufactureList == null)
                {
                    return null;
                }
                foreach (var elem in _manufactureList)
                {
                    if (elem.Id == ManufactureId)
                    {
                        return elem;
                    }
                }
                return null;
            }
        }
        public int Count
        {
            get { return Convert.ToInt32(CountTextBox.Text); }
            set
            { CountTextBox.Text = value.ToString(); }
        }
        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CountTextBox.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (ManufactureComboBox.SelectedValue == null)
            {
                MessageBox.Show("Выберите кузнечное изделие", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                int count = Convert.ToInt32(CountTextBox.Text);
                var manufacture = _manufactureLogic.ReadElement(new() { Id = Convert.ToInt32(ManufactureComboBox.SelectedValue) });
                if (manufacture == null)
                {
                    throw new ApplicationException("Ошибка при продаже. Ошибка получения данных об элементе.");
                }
                if (!_shopLogic.SellManufactures(manufacture, count))
                {
                    throw new ApplicationException("Ошибка при продаже. Недостаточно изделий данного типа в магазинах.");
                }
                MessageBox.Show("Продажа прошла успешно");
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Ошибка при продаже.");
                return;
            }
        }
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
