using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;

namespace Pos.Setting.Product
{
    /// <summary>
    /// Interaction logic for ChangeProductOrderWindow.xaml
    /// </summary>
    public partial class ChangeProductOrderWindow : Window
    {
        ObservableCollection<Database.Models.ProductEntity> _productList = new ObservableCollection<Database.Models.ProductEntity>();
        private int _productGroupId;
        public ChangeProductOrderWindow(int productGroupId)
        {
            InitializeComponent();
            _productGroupId = productGroupId;
            LoadData();
        }

        private void LoadData()
        {
            ProductRepository productRepository = new ProductRepository();
            List<Database.Models.ProductEntity> products = productRepository.GetByProductGroup(_productGroupId);
            foreach (Database.Models.ProductEntity product in products)
            {
                _productList.Add(product);
            }
            lstProduct.Items.Clear();
            lstProduct.DisplayMemberPath = "Name";
            lstProduct.ItemsSource = _productList;

            Style itemContainerStyle = new Style(typeof(ListBoxItem));
            itemContainerStyle.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
            itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(s_PreviewMouseLeftButtonDown)));
            itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(lstCategory_Drop)));
            lstProduct.ItemContainerStyle = itemContainerStyle;
        }

        void s_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (sender is ListBoxItem)
            {
                ListBoxItem draggedItem = sender as ListBoxItem;
                DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
                draggedItem.IsSelected = true;
            }
        }

        void lstCategory_Drop(object sender, DragEventArgs e)
        {
            Database.Models.ProductEntity droppedData = e.Data.GetData(typeof(Database.Models.ProductEntity)) as Database.Models.ProductEntity;
            Database.Models.ProductEntity target = ((ListBoxItem)(sender)).DataContext as Database.Models.ProductEntity;

            int removedIdx = lstProduct.Items.IndexOf(droppedData);
            int targetIdx = lstProduct.Items.IndexOf(target);

            if (removedIdx < targetIdx)
            {
                _productList.Insert(targetIdx + 1, droppedData);
                _productList.RemoveAt(removedIdx);
            }
            else
            {
                int remIdx = removedIdx + 1;
                if (_productList.Count + 1 > remIdx)
                {
                    _productList.Insert(targetIdx, droppedData);
                    _productList.RemoveAt(remIdx);
                }
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            ProductRepository productRepository = new ProductRepository();
            for (int i = 0; i < _productList.Count; i++)
            {
                Database.Models.ProductEntity product = _productList[i];
                productRepository.SetIndex(product.Id, i);
            }
            this.Close();
        }

    }
}
