using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
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.Navigation;
using System.Windows.Shapes;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;

namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// MiniBasketItem.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class OptionSelector : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty OptionDataProperty =
                 DependencyProperty.Register(nameof(OptionData), typeof(List<M_ItemOptionGroup>), typeof(OptionSelector), new PropertyMetadata(null, new PropertyChangedCallback(OnOptionDataPropertyChanged)));


        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(OptionSelector), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
        /// <summary>
        /// Vertical Scroll Offset
        /// </summary>
        public static readonly DependencyProperty VerticalOffsetProperty =
                 DependencyProperty.Register(nameof(VerticalOffset), typeof(double), typeof(OptionSelector), new PropertyMetadata(0d, new PropertyChangedCallback(OnVerticalOffsetPropertyChanged)));
        /// <summary>
        /// EditMode
        /// </summary>
        public static readonly DependencyProperty EditModeProperty =
                 DependencyProperty.Register(nameof(EditMode), typeof(bool), typeof(OptionSelector), new PropertyMetadata(false));



        /// <summary>
        /// Command
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
                 DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(OptionSelector), new UIPropertyMetadata(null));

        /// <summary>
        /// Command 
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
        /// <summary>
        /// Vertical Scroll Offset
        /// </summary>
        public double VerticalOffset
        {
            get { return (double)GetValue(VerticalOffsetProperty); }
            set { SetValue(VerticalOffsetProperty, value); }
        }

        /// <summary>
        /// ItemData
        /// </summary>
        public List<M_ItemOptionGroup> OptionData
        {
            get { return (List<M_ItemOptionGroup>)GetValue(OptionDataProperty); }
            set { SetValue(OptionDataProperty, value); }
        }
        /// <summary>
        /// Display Language
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        /// <summary>
        /// Edit Mode
        /// </summary>
        public bool EditMode
        {
            get { return (bool)GetValue(EditModeProperty); }
            set { SetValue(EditModeProperty, value); }
        }
        #region [ RoutedEvent 'SelectChange' ]

        /// <summary>
        /// MouseClicked Event
        /// </summary>
        public static readonly RoutedEvent SelectChangeEvent = EventManager.RegisterRoutedEvent(nameof(SelectChange), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(OptionSelector));
        /// <summary>
        /// MouseClicked Routed Event Handler
        /// </summary>
        public event RoutedEventHandler SelectChange
        {
            add { AddHandler(SelectChangeEvent, value); }
            remove { RemoveHandler(SelectChangeEvent, value); }
        }
        #endregion RoutedEvent 'SelectChange'
        private double mouseDownYposition = -1;
        private bool MoseDown = false;

        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public OptionSelector()
        {
            InitializeComponent();
            OptionGrid.PreviewMouseDown += OptionGrid_PreviewMouseDown;
            OptionGrid.PreviewMouseUp += OptionGrid_PreviewMouseUp;
            OptionGrid.PreviewMouseMove += OptionGrid_PreviewMouseMove;
            OptionGrid.MouseLeave += OptionGrid_MouseLeave;

        }

        #endregion Ctor & Etc

        #region [ Methods ]
        /// <summary>
        /// Raise MouseClicked Event
        /// </summary>
        private void RaiseSelectChangeEvent(object _sender)
        {
            if (Command != null && Command.CanExecute(_sender))
            {
                Command.Execute(_sender);
            }
            RoutedEventArgs newEventArgs = new RoutedEventArgs(OptionSelector.SelectChangeEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void SetOptionStack(List<M_ItemOptionGroup> newValue)
        {
            foreach (var aItem in OptionStack.Children)
            {
                if (aItem is OptionGroup aOptionGroup)
                {
                    aOptionGroup.SelectChange -= OptionGroup_SelectChange;
                }
            }
            OptionStack.Children.Clear();
            foreach (var aOptionGroup in newValue)
            {
                var newOptionGroup = new OptionGroup
                {
                    EditMode = this.EditMode,
                    GroupData = aOptionGroup,
                    DisplayLanguage = this.DisplayLanguage
                };
                newOptionGroup.SelectChange += OptionGroup_SelectChange;
                newOptionGroup.Name = string.Format("OptionGroup{0}", aOptionGroup.Index);
                this.ReregisterName(newOptionGroup.Name, newOptionGroup);
                this.OptionStack.Children.Add(newOptionGroup);
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void OptionGroup_SelectChange(object sender, RoutedEventArgs e)
        {
            RaiseSelectChangeEvent(OptionData);
        }
        private static void OnVerticalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                var target = d as OptionSelector;
                target.MainScroll.ScrollToVerticalOffset((double)e.NewValue);
            }
        }

        private static void OnOptionDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is List<M_ItemOptionGroup> newValue)
            {
                var target = d as OptionSelector;
                target.SetOptionStack(newValue);
            }
        }


        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is SupportLanguageType newValue)
            {
                var target = d as OptionSelector;
                foreach (var aGroup in target.OptionStack.Children)
                {
                    if (aGroup is OptionGroup optionGroup)
                    {
                        optionGroup.DisplayLanguage = newValue;
                    }
                }
            }
        }
        private void OptionGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var requstGrid = sender as Grid;
            mouseDownYposition = e.GetPosition(sender as Grid).Y;
            MoseDown = true;
        }

        private void OptionGrid_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            MoseDown = false;
        }

        private void OptionGrid_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (!MoseDown) return;
            var requstGrid = sender as Grid;
            var curMouseYposition = e.GetPosition(requstGrid).Y;
            VerticalOffset = VerticalOffset + (mouseDownYposition - curMouseYposition) * 2;
            mouseDownYposition = curMouseYposition;
        }

        private void OptionGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            MoseDown = false;
        }
        #endregion Event Handlers

    }
}
