using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;

namespace SecuritySystemView
{
    public partial class FormSecureComponent : Form
    {
        private readonly List<ComponentViewModel>? _list;
        public int Id
        {
            get
            {
                return Convert.ToInt32(comboBoxComponents.SelectedValue);
            }
            set
            {
                comboBoxComponents.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(textBoxCount.Text); }
            set { textBoxCount.Text = value.ToString(); }
        }
        public FormSecureComponent(IComponentLogic logic)
        {
            InitializeComponent();
            _list = logic.ReadList(null);
            if (_list != null)
            {
                comboBoxComponents.DisplayMember = "ComponentName";
                comboBoxComponents.ValueMember = "Id";
                comboBoxComponents.DataSource = _list;
                comboBoxComponents.SelectedItem = null;
            }
        }
        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCount.Text))
            {
                MessageBox.Show("Заполните поле Количество", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (Count <= 0)
            {
                MessageBox.Show("Количество должно быть больше 0", "Ошибка",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxComponents.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();
        }
    }
}
