using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
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 FormSupply : Form
    {
        private readonly List<ManufactureViewModel>? _manufactureList;
        private readonly List<ShopViewModel>? _shopsList;
        IShopLogic _shopLogic;
        IManufactureLogic _manufactureLogic;
        public int ShopId
        {
            get
            {
                return Convert.ToInt32(ShopComboBox.SelectedValue);
            }
            set
            {
                ShopComboBox.SelectedValue = value;
            }
        }
        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(); }
        }
        public FormSupply(IManufactureLogic ManufactureLogic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _shopLogic = shopLogic;
            _manufactureLogic = ManufactureLogic;
            _manufactureList = ManufactureLogic.ReadList(null);
            _shopsList = shopLogic.ReadList(null);
            if (_manufactureList != null)
            {
                ManufactureComboBox.DisplayMember = "ManufactureName";
                ManufactureComboBox.ValueMember = "Id";
                ManufactureComboBox.DataSource = _manufactureList;
                ManufactureComboBox.SelectedItem = null;
            }
            if (_shopsList != null)
            {
                ShopComboBox.DisplayMember = "ShopName";
                ShopComboBox.ValueMember = "Id";
                ShopComboBox.DataSource = _shopsList;
                ShopComboBox.SelectedItem = null;
            }
        }
        private void SaveButton_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;
            }
            if (ShopComboBox.SelectedValue == null)
            {
                MessageBox.Show("Выберите магазин", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                int count = Convert.ToInt32(CountTextBox.Text);
                bool res = _shopLogic.ReplenishManufactures(
                    new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) },
                    _manufactureLogic.ReadElement(new() { Id = Convert.ToInt32(ManufactureComboBox.SelectedValue) }),
                    count
                );
                if (!res)
                {
                    throw new Exception("Ошибка при пополнении. Дополнительная информация в логах");
                }
                MessageBox.Show("Пополнение прошло успешно");
                DialogResult = DialogResult.OK;
                Close();

            }
            catch (Exception)
            {
                MessageBox.Show("Ошибка пополнения");
                return;
            }
        }
        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
