
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Database.Repository;
using Pos.Setting.Product;

namespace Pos.Setting.ProductGroup
{
    /// <summary>
    /// Interaction logic for ProductWindow.xaml
    /// </summary>
    public partial class ProductWindow : Window
    {
        private List<Database.Models.ProductGroupEntity> _productGroups;
        private int _productId;

        public ProductWindow()
        {
            InitializeComponent();
            Init();
            LoadData();
        }


        private void Init()
        {
            ProductGroupRepository categoryRepository = new ProductGroupRepository();
            _productGroups = categoryRepository.GetAll();
            cmbCat.Items.Clear();
            foreach (Database.Models.ProductGroupEntity productGroup in _productGroups)
            {

                ComboBoxItem comboBoxItem = new ComboBoxItem();
                comboBoxItem.Tag = productGroup.Id;
                comboBoxItem.Content = productGroup.Name;
                cmbCat.Items.Add(comboBoxItem);
            }

            cmbCat.SelectedIndex = 0;
        }

        private void LoadData()
        {

            ProductRepository productRepository = new ProductRepository();
            List<Database.Models.ProductEntity> products = productRepository.GetByProductGroup(_productGroups[cmbCat.SelectedIndex].Id);
            lstProductGroup.Items.Clear();
            foreach (Database.Models.ProductEntity product in products)
            {
                ListBoxItem item = new ListBoxItem();   
                item.Content = product.Name;
                item.Tag = product.Id;
                lstProductGroup.Items.Add(item);
            }

            btnEdit.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            AddProductWindow addProductWindow = new AddProductWindow();
            addProductWindow.Closed += AddProductWindow_Closed;
            addProductWindow.Show();
        }

        private void AddProductWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cmbCat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadData();
        }

        private void LstProductGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnEdit.IsEnabled = true;
            btnDelete.IsEnabled = true;
            //ListBoxItem item = (ListBoxItem)sender;
            //_productId = (int)item.Tag;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedItem = lstProductGroup.SelectedItem as ListBoxItem;
            EditProductWindow editProductWindow = new EditProductWindow((int)selectedItem.Tag, cmbCat.SelectedIndex);
            editProductWindow.Closed += EditProductWindow_Closed;
            editProductWindow.Show();
        }

        private void EditProductWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedItem = lstProductGroup.SelectedItem as ListBoxItem;
            ArchiveProductWindow archiveProductWindow = new ArchiveProductWindow((int)selectedItem.Tag);
            archiveProductWindow.Closed += ArchiveProductWindow_Closed;
            archiveProductWindow.Show();
            
        }

        private void ArchiveProductWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnOrder_Click(object sender, RoutedEventArgs e)
        {
            ChangeProductOrderWindow changeProductOrderWindow = new ChangeProductOrderWindow(_productGroups[cmbCat.SelectedIndex].Id);
            changeProductOrderWindow.Closed += ChangeProductOrderWindow_Closed;
            changeProductOrderWindow.Show();
        }

        private void ChangeProductOrderWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
