using DressAtelierContracts.ViewModels;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
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;
using DressAtelierDataModels.Models;
using DressAtelierContracts.BusinessLogicContracts;

namespace SewingDresses
{
    public partial class FormDressMaterial : Form
    {
        private readonly List<MaterialViewModel>? _list;

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

        public IMaterialModel? MaterialModel
        {
            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(quantityTextBox.Text); 
            }
            set
            { 
                quantityTextBox.Text = value.ToString(); 
            }
        }


        public FormDressMaterial(IMaterialLogic logic)
        {
            InitializeComponent();

            _list = logic.ReadList(null);

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

        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(quantityTextBox.Text))
            {
                MessageBox.Show("Fill quantity field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (materialComboBox.SelectedValue == null)
            {
                MessageBox.Show("Choose material", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult = DialogResult.OK;
            Close();

        }

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