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.PB.Model;
using SPC.Kiosk.Popup;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.PB
{
    /// <summary>
    /// DetailBasketItem.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class DetailBasketItem : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty ItemDataProperty =
                 DependencyProperty.Register(nameof(ItemData), typeof(M_BasketItem), typeof(DetailBasketItem), new PropertyMetadata(null, new PropertyChangedCallback(OnItemDataPropertyChanged)));
        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(DetailBasketItem), new PropertyMetadata(SupportLanguageType.ko));//, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)


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

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

        public string BackImage { get; set;}
        public string DeleteImageData { get; set; }
        public string DeleteDownImageData { get; set; }
        public string OptionImageData { get; set; }
        public List<M_Language> OptionButtonLabel { get; set; }
        private PopupManager popupManager = PopupManager.GetInstance;
        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public DetailBasketItem()
        {
            BackImage=ResourceManager.GetNximagePathAdd("basket_list_bg.png", CommonValue.PBdesignImagesPath);
            DeleteImageData = ResourceManager.GetNximagePathAdd("btn_delete_2.png", CommonValue.PBdesignImagesPath);
            DeleteDownImageData = ResourceManager.GetNximagePathAdd("btn_delete_2_p.png", CommonValue.PBdesignImagesPath);
            OptionImageData = ResourceManager.GetNximagePathAdd("btn_option.png", CommonValue.PBdesignImagesPath);
            OptionButtonLabel = new List<M_Language>
            {
                new M_Language
                {
                    Type =SupportLanguageType.ko,
                    LanguageData = "옵션변경",
                },
            };
            InitializeComponent();
        }
        #endregion Ctor & Etc

        #region [ Methods ]
        /// <summary>
        /// Raise MouseClicked Event
        /// </summary>
        private void RaiseMouseClickedEvent(object _sender)
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(DetailBasketItem.MouseClickedEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void DeleteAnimationRun()
        {
            var deteleAnimation = Animations.GetShutterAnimation(this.MainGrid, ShutterAnchor.Bottom, this.MainGrid.ActualHeight, 0, 1, 0.5, 0.5);
            deteleAnimation.Completed += DeteleAnimation_Completed;
            deteleAnimation.Begin();
        }

        #endregion Methods

        #region [ Event Handlers ]
        private void DeteleAnimation_Completed(object sender, EventArgs e)
        {
            RaiseMouseClickedEvent(this.ItemData);
        }
        private static void OnItemDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (e.NewValue is M_BasketItem newValue)
                {
                    var target = d as DetailBasketItem;
                    if (e.OldValue is M_BasketItem oldValue)
                    {
                        oldValue.PropertyChanged -= target.NewValue_PropertyChanged;
                    }
                    target.ItemPrice.Text = (newValue.Count * newValue.Price).ToString("#,##0");
                    target.ItemCountCombo.LeftButtonData = new M_AnimationButton
                    {
                        NormalBrush = newValue.MinusNormalBrush,
                        DownBrush = newValue.MinusDownBrush,
                        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,
                    };
                    target.ItemCountCombo.RightButtonData = new M_AnimationButton
                    {
                        NormalBrush = newValue.PlusNormalBrush,
                        DownBrush = newValue.PlusDownBrush,
                        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,
                    };

                    if (newValue.Option is List<M_ItemOptionGroup> options
                        && options.Count > 0)
                    {
                        target.OptionChange.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        target.OptionChange.Visibility = Visibility.Collapsed;
                    }
                    newValue.PropertyChanged += target.NewValue_PropertyChanged;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(d, "OnItemDataPropertyChanged()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        private  void NewValue_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender is M_BasketItem newValue)
            {
                switch (e.PropertyName)
                {
                    case "Count":
                    case "Price":
                        this.ItemPrice.Text = (newValue.Count * newValue.Price).ToString("#,##0");
                        break;
                }
            }
        }

        private void DeleteItem_MouseClicked(object sender, RoutedEventArgs e)
        {
            //RaiseMouseClickedEvent(this.ItemData);
            DeleteAnimationRun();
        }
        private void OptionChange_MouseClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.ItemData.Option is List<M_ItemOptionGroup>
                    && this.ItemData.Option.Count > 0)
                {
                    var newOptions = new List<M_ItemOptionGroup>();
                    foreach (var aOptionGroup in this.ItemData.Option)
                    {
                        newOptions.Add(aOptionGroup.Clone());
                    }
                    var popupReturn = popupManager.OpenOptionSelect(this.ItemData.ItemName
                                                                     , newOptions
                                                                     , this.ItemData.ItemImage
                                                                     , this.ItemData.Count
                                                                     , this.ItemData.BasePrice - this.ItemData.CampaignPrice
                                                                     , true);
                    if (popupReturn is M_PopupReturn)
                    {
                        if (popupReturn.OKAnswer)
                        {
                            if (popupReturn.PopupArgs is M_OptionReturn getOption)
                            {
                                this.ItemData.Option = getOption.Options;
                                this.ItemData.Count = getOption.ItemCount;
                                this.ItemData.Price = getOption.PriceWithOption;
                                this.ItemData.AllOptionLabel = CommonFunction.GetOptionLabel(getOption.Options);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OptionChange_MouseClicked()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        #endregion Event Handlers

    }
}
