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 ProjectShoeShop.Entities;
using ProjectShoeShop.Entities.Enums;
using ProjectShoeShop.Repositories;
using ProjectShoeShop.Repositories.Implementations;

namespace ProjectShoeShop.Forms
{
    public partial class FormShoe : Form
    {
        private readonly IShoesRepository _shoesRepository;
        private int? _shoesId;

        public int Id
        {
            set
            {
                try
                {
                    var shoe = _shoesRepository.ReadShoesById(value);
                    if (shoe == null)
                    {
                        throw new InvalidDataException(nameof(shoe));
                    }

                    foreach (ShoesType elem in Enum.GetValues(typeof(ShoesType)))
                    {
                        if ((elem & shoe.ShoesType) != 0)
                        {
                            checkedListBox.SetItemChecked(checkedListBox.Items.IndexOf(elem), true);
                        }
                    }

                    textBoxName.Text = shoe.Name;
                    numericUpDown.Value = shoe.Price;
                    _shoesId = value;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
        public FormShoe(IShoesRepository shoesRepository)
        {
            InitializeComponent();
            _shoesRepository = shoesRepository ??
                throw new ArgumentNullException(nameof(shoesRepository));

            foreach (var elem in Enum.GetValues(typeof (ShoesType)))
            {
                checkedListBox.Items.Add(elem);
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(textBoxName.Text) ||
                    checkedListBox.CheckedItems.Count == 0 || numericUpDown.Value < 1)
                {
                    throw new Exception("Имеется незаполненное поле");
                }

                if (_shoesId.HasValue)
                {
                    _shoesRepository.UpdateShoes(CreateShoes(_shoesId.Value));
                }
                else
                {
                    _shoesRepository.CreateShoes(CreateShoes(0));
                }

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

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

        private Shoes CreateShoes(int id)
        {
            ShoesType shoesType = ShoesType.None;
            foreach (var elem in checkedListBox.CheckedItems) 
            {
                shoesType |= (ShoesType)elem;
            }

            return Shoes.CreateEntity(id, textBoxName.Text, (int)numericUpDown.Value, shoesType);
        }
    }
}
