using SecureShopContracts.BusinessLogicsContracts;
using SecureShopContracts.ViewModels;
using SecureShopDataModels.Models;

namespace SecureShopView
{
    public partial class FormSecureFacilities : Form
    {
        private readonly List<FacilitiesViewModel>? _list;
        public int Id
        {
            get
            {
                return Convert.ToInt32(comboBoxFacilities.SelectedValue);
            }
            set
            {
                comboBoxFacilities.SelectedValue = value;
            }
        }
        public IFacilitiesModel? FacilitiesModel
        {
            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(textBoxCount.Text);
            }
            set
            {
                textBoxCount.Text = value.ToString();
            }
        }

        public FormSecureFacilities(IFacilitiesLogic logic)
        {
            InitializeComponent();
            _list = logic.ReadList(null);
            if (_list != null)
            {
                comboBoxFacilities.DisplayMember = "FacilitiesName";
                comboBoxFacilities.ValueMember = "Id";
                comboBoxFacilities.DataSource = _list;
                comboBoxFacilities.SelectedItem = null;
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCount.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxFacilities.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();
        }
    }
}