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;

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


        /// <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(MiniBasketItem));
        /// <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; }
        #endregion Members

        #region [ Ctor & Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public MiniBasketItem()
        {
            BackImage=ResourceManager.GetNximagePathAdd("bg_basket_box.png", CommonValue.PBdesignImagesPath);
            DeleteImageData = ResourceManager.GetNximagePathAdd("btn_delete_2.png", CommonValue.PBdesignImagesPath);
            DeleteDownImageData = ResourceManager.GetNximagePathAdd("btn_delete_2_p.png", CommonValue.PBdesignImagesPath);
            InitializeComponent();
        }
        #endregion Ctor & Etc

        #region [ Methods ]
        /// <summary>
        /// Raise MouseClicked Event
        /// </summary>
        private void RaiseMouseClickedEvent(object _sender)
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(MiniBasketItem.MouseClickedEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void DeleteAnimationRun()
        {
            var deteleAnimation = Animations.GetShutterAnimation(this.MainGrid, ShutterAnchor.Right, this.MainGrid.ActualWidth, 0, 1, 0.5, 0.3);
            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 MiniBasketItem;
                    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,
                    };

                    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)
        {
            try
            {
                if (sender is M_BasketItem newValue)
                {
                    switch (e.PropertyName)
                    {
                        case "Count":
                        case "Price":
                            this.Dispatcher.Invoke((Action)(() =>
                            {
                                Animations.GetClickAnimation(this.MainGrid, ButtonAnimationType.FlipY, 0.2).Begin();
                                ItemPrice.Text = (newValue.Count * newValue.Price).ToString("#,##0");
                            }));
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "NewValue_PropertyChanged()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        private void DeleteItem_MouseClicked(object sender, RoutedEventArgs e)
        {
            DeleteAnimationRun();
        }

        #endregion Event Handlers

    }
}
