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 OptionItem : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty ItemDataProperty =
                 DependencyProperty.Register(nameof(ItemData), typeof(M_ItemOption), typeof(OptionItem), new PropertyMetadata(null, new PropertyChangedCallback(OnItemDataPropertyChanged)));
        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(OptionItem), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
        /// <summary>
        /// Option Select Mode
        /// </summary>
        public static readonly DependencyProperty OptionModePropery =
                 DependencyProperty.Register(nameof(OptionMode), typeof(OptionSelectMode), typeof(OptionItem), new PropertyMetadata(OptionSelectMode.None));
        /// <summary>
        /// Button Switch
        /// </summary>
        public static readonly DependencyProperty SwitchPropery =
                 DependencyProperty.Register(nameof(Switch), typeof(bool), typeof(OptionItem), new PropertyMetadata(false));
        /// <summary>
        /// Option Select Mode
        /// </summary>
        public OptionSelectMode OptionMode
        {
            get { return (OptionSelectMode)GetValue(OptionModePropery); }
            set { SetValue(OptionModePropery, value); }
        }
        /// <summary>
        /// Button Switch
        /// </summary>
        public bool Switch
        {
            get { return (bool)GetValue(SwitchPropery); }
            set { SetValue(SwitchPropery, value); SelectedChange(value);}

        }
        /// <summary>
        /// ItemData
        /// </summary>
        public M_ItemOption ItemData
        {
            get { return (M_ItemOption)GetValue(ItemDataProperty); }
            set { SetValue(ItemDataProperty, value); }
        }
        /// <summary>
        /// Display Language
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        /// <summary>
        /// Delete Button NormalBrush
        /// </summary>
        #region [ RoutedEvent 'MouseClicked' ]

        /// <summary>
        /// MouseClicked Event
        /// </summary>
        public static readonly RoutedEvent MouseClickedEvent = EventManager.RegisterRoutedEvent(nameof(MouseClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(OptionItem));
        /// <summary>
        /// MouseClicked Routed Event Handler
        /// </summary>
        public event RoutedEventHandler MouseClicked
        {
            add { AddHandler(MouseClickedEvent, value); }
            remove { RemoveHandler(MouseClickedEvent, value); }
        }
        #endregion RoutedEvent 'MouseClicked'

        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public OptionItem()
        {
            InitializeComponent();
            OptionValueCombo.LeftButtonData = new M_AnimationButton
            {
                NormalBrush = ResourceManager.GetNximagePathAdd("btn_count_minus_n.png", CommonValue.PBdesignImagesPath),
                DownBrush = ResourceManager.GetNximagePathAdd("btn_count_minus_p.png", CommonValue.PBdesignImagesPath),
                BrushStretch = Stretch.Fill,
                DragEnable = false,
                Width = 36,
                Height = 36,
                Switch = false,
                AutoToggle = false,
                TextVisible = Visibility.Collapsed,
                ClickAnimationType = ButtonAnimationType.None,
                CircleBackBrush = "Transparent",
                CircleTextBruch = "Transparent",
                CircleText = string.Empty,
            };
            OptionValueCombo.RightButtonData = new M_AnimationButton
            {
                NormalBrush = ResourceManager.GetNximagePathAdd("btn_count_plus_n.png", CommonValue.PBdesignImagesPath),
                DownBrush = ResourceManager.GetNximagePathAdd("btn_count_plus_p.png", CommonValue.PBdesignImagesPath),
                BrushStretch = Stretch.Fill,
                DragEnable = false,
                Width = 36,
                Height = 36,
                Switch = false,
                AutoToggle = false,
                TextVisible = Visibility.Collapsed,
                ClickAnimationType = ButtonAnimationType.None,
                CircleBackBrush = "Transparent",
                CircleTextBruch = "Transparent",
                CircleText = string.Empty,
            };
            OptionValueCombo.SelectChange += OptionValueCombo_SelectChange;

        }

        #endregion Ctor & Etc

        #region [ Methods ]
        /// <summary>
        /// Raise MouseClicked Event
        /// </summary>
        private void RaiseMouseClickedEvent(object _sender)
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(OptionItem.MouseClickedEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void OptionValueCombo_SelectChange(object sender, RoutedEventArgs e)
        {
            if (sender is NumericCombo numericCombo)
            {
                ItemData.SelectValue = double.Parse(numericCombo.SelectedValue.ToString());
                if (!ItemData.SelectValue.Equals(0) && ItemData.SelectValue >= ItemData.AmountMin && !ItemData.IsSelected)
                {
                    SelectedChange(true, true);
                }
                else if (ItemData.SelectValue.Equals(0))
                {
                    SelectedChange(false, true);
                }
            }
        }
        private static void OnItemDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is M_ItemOption newValue)
            {
                var target = d as OptionItem;
                target.OptionButton.NormalBrush = ResourceManager.GetNximagePathAdd("btn_option_n.png", CommonValue.PBdesignImagesPath);
                target.OptionButton.SwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_option_s.png", CommonValue.PBdesignImagesPath);
                target.OptionButton.DisableBrush = ResourceManager.GetNximagePathAdd("btn_option_s.png", CommonValue.PBdesignImagesPath);

                switch (newValue.Kind)
                {
                    case OptionKind.Button:
                        target.OptionValueCombo.Visibility = Visibility.Collapsed;
                        target.OptionAmount.Visibility = Visibility.Collapsed;
                        break;
                    case OptionKind.Count:
                        target.OptionValueCombo.Visibility = Visibility.Visible;
                        target.OptionAmount.Visibility = Visibility.Collapsed;
                        target.OptionValueCombo.MinValue = newValue.AmountMin;
                        target.OptionValueCombo.MaxValue = newValue.AmountMax;
                        target.OptionValueCombo.ValueStep = int.Parse(newValue.Amount.ToString());
                        if (newValue.IsSelected)
                        {
                            target.OptionValueCombo.SelectedValue = int.Parse(newValue.SelectValue.ToString());
                        }
                        else
                        {
                            target.OptionValueCombo.SelectedValue = newValue.AmountMin;
                        }
                        break;
                    case OptionKind.Discount:
                        target.OptionValueCombo.Visibility = Visibility.Collapsed;
                        target.OptionAmount.Visibility = Visibility.Visible;
                        target.OptionAmount.Text = newValue.Amount.ToString("#,##0");
                        break;
                    case OptionKind.UpCharge:
                        target.OptionValueCombo.Visibility = Visibility.Collapsed;
                        target.OptionAmount.Visibility = Visibility.Visible;
                        target.OptionAmount.Text = newValue.Amount.ToString("#,##0");
                        break;
                }
                target.SelectedChange(newValue.IsSelected);
                target.OptionButton.LanguageText = newValue.Name;
            }
        }

        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is SupportLanguageType newValue)
            {
                var target = d as OptionItem;
                target.OptionButton.DisplayLanguage = newValue;
            }
        }
        private void OptionButton_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (ItemData is M_ItemOption)
            {
                switch (ItemData.Kind)
                {
                    case OptionKind.Button:
                    case OptionKind.Discount:
                    case OptionKind.UpCharge:
                        if (ItemData.IsSelected)
                        {
                            this.ItemData.SelectValue = 0d;
                            SelectedChange(false,true);
                        }
                        else
                        {
                            this.ItemData.SelectValue = ItemData.Amount;
                            SelectedChange(true,true);
                        }
                        break;
                    case OptionKind.Count:
                        if (ItemData.IsSelected)
                        {
                            this.ItemData.SelectValue = 0d;
                            OptionValueCombo.SelectedValue = ItemData.AmountMin;
                            SelectedChange(false,true);
                        }
                        else
                        {
                            if (OptionValueCombo.SelectedValue > 0)
                            {
                                this.ItemData.SelectValue = double.Parse(OptionValueCombo.SelectedValue.ToString());
                                SelectedChange(true,true);
                            }
                        }
                        break;
                }
            }
        }
        private void SelectedChange(bool _switch, bool _sendEvent = false)
        {
            switch (OptionMode)
            {
                case OptionSelectMode.Single:
                    ItemData.IsSelected = _switch;
                    this.OptionButton.Enabled = !_switch;
                    break;
                case OptionSelectMode.Multi:
                    ItemData.IsSelected = _switch;
                    this.OptionButton.Switch = _switch;
                    break;
            }
            if (_sendEvent) RaiseMouseClickedEvent(this);
        }
        #endregion Event Handlers

    }
}
