using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.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 ProjectFlowerShop
{
    public partial class SupplyForm : Form
    {
        private readonly List<FlowerViewModel>? _flowerList;
        private readonly List<ShopViewModel>? _shopsList;
        IShopLogic _shopLogic;
        IFlowerLogic _flowerLogic;
        public SupplyForm(IFlowerLogic flowerLogic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _shopLogic = shopLogic;
            _flowerLogic = flowerLogic;
            _flowerList = flowerLogic.ReadList(null);
            _shopsList = shopLogic.ReadList(null);
            if (_flowerList != null)
            {
                comboBoxFlower.DisplayMember = "FlowerName";
                comboBoxFlower.ValueMember = "Id";
                comboBoxFlower.DataSource = _flowerList;
                comboBoxFlower.SelectedItem = null;
            }
            if (_shopsList != null)
            {
                comboBoxShop.DisplayMember = "ShopName";
                comboBoxShop.ValueMember = "Id";
                comboBoxShop.DataSource = _shopsList;
                comboBoxShop.SelectedItem = null;
            }
        }
        public int ShopId
        {
            get
            {
                return Convert.ToInt32(comboBoxShop.SelectedValue);
            }
            set
            {
                comboBoxShop.SelectedValue = value;
            }
        }

        public int FlowerId
        {
            get
            {
                return Convert.ToInt32(comboBoxFlower.SelectedValue);
            }
            set
            {
                comboBoxFlower.SelectedValue = value;
            }
        }

        public IFlowerModel? FlowerModel
        {
            get
            {
                if (_flowerList == null)
                {
                    return null;
                }
                foreach (var elem in _flowerList)
                {
                    if (elem.Id == FlowerId)
                    {
                        return elem;
                    }
                }
                return null;
            }
        }
        public int Number
        {
            get { return Convert.ToInt32(textBoxNumber.Text); }
            set { textBoxNumber.Text = value.ToString(); }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxNumber.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxFlower.SelectedValue == null)
            {
                MessageBox.Show("Выберите цветы", "Ошибка",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxShop.SelectedValue == null)
            {
                MessageBox.Show("Выберите магазин", "Ошибка",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                int count = Convert.ToInt32(textBoxNumber.Text);

                bool res = _shopLogic.MakeSupply(
                    new ShopSearchModel() { Id = Convert.ToInt32(comboBoxShop.SelectedValue) },
                    _flowerLogic.ReadElement(new() { Id = Convert.ToInt32(comboBoxFlower.SelectedValue) }),
                    count
                );

                if (!res)
                {
                    throw new Exception("Ошибка при пополнении. Дополнительная информация в логах");
                }

                MessageBox.Show("Пополнение прошло успешно");
                DialogResult = DialogResult.OK;
                Close();

            }
            catch (Exception err)
            {
                MessageBox.Show("Ошибка пополнения");
                return;
            }
        }

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