using Contracts.BusinessLogicContracts;
using Contracts.ViewModels;
using DataModels.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 WinFormsApp
{
    public partial class FormSupplierProduct : Form
    {
        private readonly List<ProductViewModel>? _list;
        public Guid Id
        {
            get
            {
                return (Guid)comboBoxProduct.SelectedValue;
            }
            set
            {
                comboBoxProduct.SelectedValue = value;
            }
        }

        public IProduct? ProductModel
        {
            get
            {
                if (_list == null)
                {
                    return null;
                }
                foreach (var elem in _list)
                {
                    if (elem.Id == Id)
                    {
                        return elem;
                    }
                }
                return null;
            }
        }

        public int Count
        {
            get { return Convert.ToInt32(numericUpDownCount.Value); }
            set { numericUpDownCount.Value = value; }
        }
        public FormSupplierProduct(IProductLogic logic)
        {
            InitializeComponent();
            _list = logic.ReadList(null);
            if (_list != null)
            {
                comboBoxProduct.DisplayMember = "Name";
                comboBoxProduct.ValueMember = "Id";
                comboBoxProduct.DataSource = _list;
                comboBoxProduct.SelectedItem = null;
            }
        }
        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (numericUpDownCount.Value == null || numericUpDownCount.Value <= 0)
            {
                MessageBox.Show("Кол-во товаров должно иметь значение больше 0", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxProduct.SelectedValue == null)
            {
                MessageBox.Show("Выберите товар", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult = DialogResult.OK;
            Close();
        }
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
