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>
    /// DiscountAndValue.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class OptionGroup : UserControl
    {
        #region [ Members ]
        private string optionGroupDefault = ResourceManager.GetNximagePathAdd("btn_count_plus_p.png", CommonValue.PBdesignImagesPath);
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty GroupDataProperty =
                 DependencyProperty.Register(nameof(GroupData), typeof(M_ItemOptionGroup), typeof(OptionGroup), new PropertyMetadata(null, new PropertyChangedCallback(OnGroupDataPropertyChanged)));


        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(OptionGroup), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));

        /// <summary>
        /// Horizontal Scroll Offset
        /// </summary>
        public static readonly DependencyProperty HorizontalOffsetProperty =
                 DependencyProperty.Register(nameof(HorizontalOffset), typeof(double), typeof(OptionGroup), new PropertyMetadata(0d, new PropertyChangedCallback(OnHorizontalOffsetPropertyChanged)));
        /// <summary>
        /// EditMode
        /// </summary>
        public static readonly DependencyProperty EditModeProperty =
                 DependencyProperty.Register(nameof(EditMode), typeof(bool), typeof(OptionGroup), new PropertyMetadata(false));

        /// <summary>
        /// Horizontal Scroll Offset
        /// </summary>
        public double HorizontalOffset
        {
            get { return (double)GetValue(HorizontalOffsetProperty); }
            set { SetValue(HorizontalOffsetProperty, value); }
        }
        /// <summary>
        /// ItemData
        /// </summary>
        public M_ItemOptionGroup GroupData
        {
            get { return (M_ItemOptionGroup)GetValue(GroupDataProperty); }
            set { SetValue(GroupDataProperty, 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(OptionGroup));
        /// <summary>
        /// MouseClicked Routed Event Handler
        /// </summary>
        public event RoutedEventHandler SelectChange
        {
            add { AddHandler(SelectChangeEvent, value); }
            remove { RemoveHandler(SelectChangeEvent, value); }
        }
        #endregion RoutedEvent 'SelectChange'
        private double mouseDownXposition = -1;
        private bool MoseDown = false;
        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public OptionGroup()
        {
            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)
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(OptionGroup.SelectChangeEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void SetOptionStack(M_ItemOptionGroup newValue)
        {
            this.OptionGroupName.MultiLanguage = newValue.Name;
            this.OptionGroupIcon.NormalBrush = ResourceManager.GetOptionImage(newValue.ImageFile, CommonValue.OptionDefaultImage);
            foreach (var aItem in OptionStack.Children)
            {
                if (aItem is OptionItem aOptionItem)
                {
                    aOptionItem.MouseClicked -= OptionItem_MouseClicked; 
                }
            }
            OptionStack.Children.Clear();
            var Index = 0;
            
            foreach (var aOption in newValue.Options)
            {
                if (!EditMode)
                { 
                    if (newValue.IsMustSelect && Index.Equals(0))
                    {
                        switch (aOption.Kind)
                        {
                            case OptionKind.Button:
                                aOption.IsSelected = true;
                                break;
                            case OptionKind.Discount:
                            case OptionKind.UpCharge:
                                aOption.IsSelected = true;
                                aOption.SelectValue = aOption.Amount;
                                break;
                            case OptionKind.Count:
                                aOption.IsSelected = true;
                                aOption.SelectValue = aOption.AmountMin.Equals(0) ? aOption.Amount : aOption.AmountMin;
                                break;
                        }
                    }
                    else
                    {
                        aOption.IsSelected = false;
                    }
                }
                var newOptionItem = new OptionItem
                {
                    DisplayLanguage = this.DisplayLanguage,
                    OptionMode = newValue.SelectMode,
                    ItemData = aOption,
                };
                newOptionItem.MouseClicked += OptionItem_MouseClicked;
                newOptionItem.Name = string.Format("OptionItem{0}", aOption.Index);
                this.ReregisterName(newOptionItem.Name, newOptionItem);
                this.OptionStack.Children.Add(newOptionItem);
                Index++;
            }
        }



        private string GetGroupImage(string _ImageFile)
        {
            string result = optionGroupDefault;
            try
            {
                if (string.IsNullOrEmpty(_ImageFile))
                {
                    result = ResourceManager.GetNximagePathAdd(_ImageFile, CommonValue.PBdesignImagesPath); ;
                    if (string.IsNullOrEmpty(result)) result = optionGroupDefault;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "GetGroupImage()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
            return result;
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void OptionItem_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (sender is OptionItem getOption)
            {
                switch (GroupData.SelectMode)
                {
                    case OptionSelectMode.Single:
                        foreach (var aItem in OptionStack.Children)
                        {
                            if (aItem is OptionItem otherOption &&  !otherOption.Equals(getOption))
                            {
                                otherOption.Switch = false;
                                otherOption.ItemData.SelectValue = 0d;
                            }
                        };
                        break;
                    case OptionSelectMode.Multi:
                        break;
                }
                RaiseSelectChangeEvent(GroupData);
            }
        }
        private static void OnHorizontalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                var target = d as OptionGroup;
                target.MainScroll.ScrollToVerticalOffset((double)e.NewValue);
            }
        }

        private static void OnGroupDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is M_ItemOptionGroup newValue)
            {
                var target = d as OptionGroup;
                target.SetOptionStack(newValue);
            }
        }


        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is SupportLanguageType newValue)
            {
                var target = d as OptionGroup;
                target.OptionGroupName.DisplayLanguage = newValue;
                foreach (var aOptoin in target.OptionStack.Children)
                {
                    if (aOptoin is OptionItem optionItem)
                    {
                        optionItem.DisplayLanguage = newValue;
                    }
                }
            }
        }
        private void OptionGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var requstGrid = sender as Grid;
            mouseDownXposition = e.GetPosition(sender as Grid).X;
            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 curMouseXposition = e.GetPosition(requstGrid).X;
            HorizontalOffset = HorizontalOffset + (mouseDownXposition - curMouseXposition) * 2;
            mouseDownXposition = curMouseXposition;
        }

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

        #endregion Event Handlers

    }
}
