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

namespace ProjectShoeShop.Forms
{
    public partial class FormOrders : Form
    {
        private readonly IUnityContainer _container;
        private readonly IOrderRepository _orderRepository;
        public FormOrders(IUnityContainer container, IOrderRepository orderRepository)
        {
            InitializeComponent();
            _container = container ??
                throw new ArgumentNullException(nameof(container));
            _orderRepository = orderRepository ??
                throw new ArgumentNullException(nameof(orderRepository));
        }

        private void FormOrders_Load(object sender, EventArgs e)
        {
            try
            {
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                _container.Resolve<FormOrder>().ShowDialog();
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при добавлении",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadList() => dataGridView.DataSource = _orderRepository.ReadOrder();
    }
}
