using BarContracts.BusinessLogicContracts;
using BarContracts.ViewModels;
using BarDataModels.Models;

namespace BarView.Forms
{
    public partial class FormCocktailComponent : Form
    {
        private readonly List<ComponentViewModel>? _list;

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

        public IComponentModel? ComponentModel
        {
            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(CountTextBox.Text); }
            set { CountTextBox.Text = value.ToString(); }
        }

        public FormCocktailComponent(IComponentLogic Logic)
        {
            InitializeComponent();

            _list = Logic.ReadList(null);

            if (_list != null)
            {
                ComboBox.DisplayMember = "ComponentName";
                ComboBox.ValueMember = "Id";
                ComboBox.DataSource = _list;
                ComboBox.SelectedItem = null;
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CountTextBox.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ComboBox.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();
        }
    }
}
