using ProjectShoeShop.Entities;
using ProjectShoeShop.Entities.Enums;
using ProjectShoeShop.Repositories;

namespace ProjectShoeShop.Forms
{
    public partial class FormSupply : Form
    {
        private readonly ISupplyRepository _supplyRepository;
        private readonly IShoesRepository _shoesRepository;
        private int? _supplyId;

        public int Id
        {
            set
            {
                try
                {
                    var supply = _supplyRepository.ReadSupplyById(value);
                    if (supply == null)
                    {
                        throw new InvalidDataException(nameof(supply));
                    }

                    comboBox.SelectedItem = supply.SupplyType;
                    _supplyId = value;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        public FormSupply(ISupplyRepository supplyRepository, IShoesRepository shoesRepository)
        {
            InitializeComponent();
            _supplyRepository = supplyRepository ??
                throw new ArgumentNullException(nameof(supplyRepository));

            comboBox.DataSource = Enum.GetValues(typeof(SupplyType));
            _shoesRepository = shoesRepository ??
                throw new ArgumentNullException(nameof(shoesRepository));

            ColumnShoe.DataSource = shoesRepository.ReadShoes();
            ColumnShoe.DisplayMember = "Name";
            ColumnShoe.ValueMember = "Id";
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox.SelectedIndex < 1 || dataGridView.RowCount < 1)
                {
                    throw new Exception("Имеется незаполненное поле");
                }

                if (_supplyId.HasValue)
                {
                    _supplyRepository.UpdateSupply(CreateSupply(_supplyId.Value));
                }
                else
                {
                    _supplyRepository.CreateSupply(CreateSupply(0));
                }

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибкв при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private Supply CreateSupply(int id) => Supply.CreateEntity(id,(SupplyType)comboBox.SelectedItem!, DateTime.Now, CreateListSupplyShoesFromDataGrid());

        private List<Entities.SupplyShoes> CreateListSupplyShoesFromDataGrid()
        {
            var list = new List<Entities.SupplyShoes>();
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                if (row.Cells["ColumnShoe"].Value == null ||
                row.Cells["Size"].Value == null ||
                row.Cells["ColumnNumberOfPairs"].Value == null)
                {
                    continue;
                }
                list.Add(Entities.SupplyShoes.CreateElement(0,
                    Convert.ToInt32(row.Cells["ColumnShoe"].Value),
                    Convert.ToInt32(row.Cells["ColumnNumberOfPairs"].Value),
                    Convert.ToInt32(row.Cells["Size"].Value)));
            }
            return list;
        }
    }
}
