using ComputersShopContracts.BisnessLogicsContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDataModels;
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 ComputersShop
{
    public partial class FormSell : Form
    {
        private readonly List<ComputerViewModel>? _ComputerList;
        IShopLogic _shopLogic;
        IComputerLogic _ComputerLogic;
        public FormSell(IComputerLogic ComputerLogic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _shopLogic = shopLogic;
            _ComputerLogic = ComputerLogic;
            _ComputerList = ComputerLogic.ReadList(null);
            if (_ComputerList != null)
            {
                ComputerComboBox.DisplayMember = "ComputerName";
                ComputerComboBox.ValueMember = "Id";
                ComputerComboBox.DataSource = _ComputerList;
                ComputerComboBox.SelectedItem = null;
            }
        }
        public int ComputerId
        {
            get
            {
                return
       Convert.ToInt32(ComputerComboBox.SelectedValue);
            }
            set
            {
                ComputerComboBox.SelectedValue = value;
            }
        }

        public IComputerModel? ComputerModel
        {
            get
            {
                if (_ComputerList == null)
                {
                    return null;
                }
                foreach (var elem in _ComputerList)
                {
                    if (elem.Id == ComputerId)
                    {
                        return elem;
                    }
                }
                return null;
            }
        }


        public int Count
        {
            get { return Convert.ToInt32(CountTextBox.Text); }
            set
            { CountTextBox.Text = value.ToString(); }
        }

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

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CountTextBox.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (ComputerComboBox.SelectedValue == null)
            {
                MessageBox.Show("Выберите компьютер", "Ошибка",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                int count = Convert.ToInt32(CountTextBox.Text);
                var computer = _ComputerLogic.ReadElement(new() { Id = Convert.ToInt32(ComputerComboBox.SelectedValue) });
                if (computer == null)
                    return;
                bool res = _shopLogic.MakeSell(computer, count);

                if (!res)
                {
                    throw new Exception("Ошибка при продаже.");
                }

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

            }
            catch (Exception err)
            {
                MessageBox.Show("Ошибка продажи");
                return;
            }
        }
    }
}
