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;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// MiniBasketItem.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class HappyAppCoupons : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty CouponDataProperty =
                 DependencyProperty.Register(nameof(CouponData), typeof(List<M_HappyCoupon>), typeof(HappyAppCoupons), new PropertyMetadata(null, new PropertyChangedCallback(OnCouponDataPropertyChanged)));


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

        /// <summary>
        /// Command
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
                 DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(HappyAppCoupons), 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_HappyCoupon> CouponData
        {
            get { return (List<M_HappyCoupon>)GetValue(CouponDataProperty); }
            set { SetValue(CouponDataProperty, value); }
        }
        /// <summary>
        /// Display Language
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        #region [ RoutedEvent 'CouponSelect' ]

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

        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public HappyAppCoupons()
        {
            InitializeComponent();
            CouponGrid.PreviewMouseDown += CouponGrid_PreviewMouseDown;
            CouponGrid.PreviewMouseUp += CouponGrid_PreviewMouseUp;
            CouponGrid.PreviewMouseMove += CouponGrid_PreviewMouseMove;
            CouponGrid.MouseLeave += CouponGrid_MouseLeave;

        }

        #endregion Ctor & Etc

        #region [ Methods ]
        /// <summary>
        /// Raise MouseClicked Event
        /// </summary>
        private void RaiseCouponSelectEvent(object _sender)
        {
            if (Command != null && Command.CanExecute(_sender))
            {
                Command.Execute(_sender);
            }
            RoutedEventArgs newEventArgs = new RoutedEventArgs(HappyAppCoupons.CouponSelectEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void SetCouponStack(List<M_HappyCoupon> newValue)
        {
            foreach (var aGrid in CouponStack.Children)
            {
                if (aGrid is Grid aCoupons)
                {
                    foreach (var aStack in aCoupons.Children)
                    {
                        if (aStack is StackPanel aItems)
                        {
                            foreach (var aItem in aItems.Children)
                            {
                                if (aItem is HappyAppCoupon aCoupon)
                                {
                                    aCoupon.MouseClicked -= ACoupon_MouseClicked;
                                }
                            }
                            aItems.Children.Clear();
                        }
                    }
                    aCoupons.Children.Clear();
                }
            }
            CouponStack.Children.Clear();
            if (newValue.Count.Equals(0)) return;
            var index = 0;
            Grid lineGrid = null;
            StackPanel lineStack = null;
            foreach (var aCouponDetail in newValue)
            {
                var newCoupon = new HappyAppCoupon
                {
                    ItemData = aCouponDetail,
                    DisplayLanguage = this.DisplayLanguage
                };
                if ((index % 3).Equals(0))
                {
                    lineGrid = new Grid
                    {
                        VerticalAlignment = VerticalAlignment.Top,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Width = newCoupon.Width * 3,
                        Height = newCoupon.Height,
                    };
                    lineStack = new StackPanel
                    {
                        VerticalAlignment = VerticalAlignment.Top,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Orientation = Orientation.Horizontal,
                    };
                    
                }
                newCoupon.MouseClicked += ACoupon_MouseClicked;
                newCoupon.Name = string.Format("HappyAppCoupon{0}", index);
                this.ReregisterName(newCoupon.Name, newCoupon);
                lineStack.Children.Add(newCoupon);
                index++;
                if ((index % 3).Equals(0))
                {
                    lineGrid.Children.Add(lineStack);
                    CouponStack.Children.Add(lineGrid);
                }
            }
            if (!(index % 3).Equals(0))
            {
                lineGrid.Children.Add(lineStack);
                CouponStack.Children.Add(lineGrid);
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void ACoupon_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (sender is HappyAppCoupon selectCoupon)
            {
                foreach (var aGrid in CouponStack.Children)
                {
                    if (aGrid is Grid aCoupons)
                    {
                        foreach (var aStack in aCoupons.Children)
                        {
                            if (aStack is StackPanel aItems)
                            {
                                foreach (var aItem in aItems.Children)
                                {
                                    if (aItem is HappyAppCoupon aCoupon)
                                    {
                                        if (aCoupon.Name.Equals(selectCoupon.Name))
                                        {
                                            aCoupon.ItemData.IsUsed = !aCoupon.ItemData.IsUsed;
                                            selectCoupon.Switch = aCoupon.ItemData.IsUsed;
                                        }
                                        else
                                        {
                                            aCoupon.Switch = false;
                                            aCoupon.ItemData.IsUsed = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            RaiseCouponSelectEvent(e.OriginalSource);
        }
        private static void OnVerticalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                var target = d as HappyAppCoupons;
                target.MainScroll.ScrollToVerticalOffset((double)e.NewValue);
            }
        }

        private static void OnCouponDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is List<M_HappyCoupon> newValue)
            {
                var target = d as HappyAppCoupons;
                target.SetCouponStack(newValue);
            }
        }


        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is SupportLanguageType newValue)
            {
                var target = d as HappyAppCoupons;
                foreach (var aGrid in target.CouponStack.Children)
                {
                    if (aGrid is Grid aCoupons)
                    {
                        foreach (var aStack in aCoupons.Children)
                        {
                            if (aStack is StackPanel aItems)
                            {
                                foreach (var aItem in aItems.Children)
                                {
                                    if (aItem is HappyAppCoupon aCoupon)
                                    {
                                        aCoupon.DisplayLanguage = newValue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void CouponGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var requstGrid = sender as Grid;
            mouseDownYposition = e.GetPosition(sender as Grid).Y;
            MoseDown = true;
        }

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

        private void CouponGrid_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 CouponGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            MoseDown = false;
        }
        #endregion Event Handlers

    }
}
