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.Category
{
    /// <summary>
    /// Interaction logic for ChangeProductGroupOrderWindow.xaml
    /// </summary>
    public partial class ChangeProductGroupOrderWindow : Window
    {
        ObservableCollection<Database.Models.ProductGroupEntity> _prodGroupList = new ObservableCollection<Database.Models.ProductGroupEntity>();
        public ChangeProductGroupOrderWindow()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            ProductGroupRepository categoryRepository = new ProductGroupRepository();
            List<Database.Models.ProductGroupEntity> categories = categoryRepository.GetAll();
            foreach (Database.Models.ProductGroupEntity category in categories)
            {
                _prodGroupList.Add(category);
            }
            lstCategory.Items.Clear();
            lstCategory.DisplayMemberPath = "Name";
            lstCategory.ItemsSource = _prodGroupList;

            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)));
            lstCategory.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.ProductGroupEntity droppedData = e.Data.GetData(typeof(Database.Models.ProductGroupEntity)) as Database.Models.ProductGroupEntity;
            Database.Models.ProductGroupEntity target = ((ListBoxItem)(sender)).DataContext as Database.Models.ProductGroupEntity;

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

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

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            ProductGroupRepository categoryRepository = new ProductGroupRepository();
            for (int i = 0; i < _prodGroupList.Count; i++)
            {
                Database.Models.ProductGroupEntity productGroup = _prodGroupList[i];
                categoryRepository.SetIndex(productGroup.Id,i);
            }
            this.Close();
        }
    }
}
