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>
    /// DiscountAndValue.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class VirtureMobileHappyCoupon : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// ItemData
        /// </summary>
        public static readonly DependencyProperty ItemDataProperty =
                 DependencyProperty.Register(nameof(ItemData), typeof(M_MobileCouponDetail), typeof(VirtureMobileHappyCoupon), new PropertyMetadata(new M_MobileCouponDetail(), new PropertyChangedCallback(OnItemDataPropertyChanged)));
        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(VirtureMobileHappyCoupon), new PropertyMetadata(SupportLanguageType.ko));
        /// <summary>
        /// Button Switch
        /// </summary>
        public static readonly DependencyProperty SwitchPropery =
                 DependencyProperty.Register(nameof(Switch), typeof(bool), typeof(VirtureMobileHappyCoupon), new PropertyMetadata(false, new PropertyChangedCallback(OnSwitchProperyChanged)));
        public static readonly DependencyProperty EnabledPropery =
                 DependencyProperty.Register(nameof(Enabled), typeof(bool), typeof(VirtureMobileHappyCoupon), new PropertyMetadata(true, new PropertyChangedCallback(OnEnabledProperyChanged)));



        /// <summary>
        /// Button Switch
        /// </summary>
        public bool Switch
        {
            get { return (bool)GetValue(SwitchPropery); }
            set { SetValue(SwitchPropery, value);}

        }
        /// <summary>
        /// Button Enabled
        /// </summary>
        public bool Enabled
        {
            get { return (bool)GetValue(EnabledPropery); }
            set { SetValue(EnabledPropery, value); }

        }
        /// <summary>
        /// ItemData
        /// </summary>
        public M_MobileCouponDetail ItemData
        {
            get { return (M_MobileCouponDetail)GetValue(ItemDataProperty); }
            set { SetValue(ItemDataProperty, value); }
        }
        /// <summary>
        /// Display Language
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        /// <summary>
        /// Button NormalBrush
        /// </summary>
        public string ButtonNormalBrush { get; set; }
        public string ButtonDownBrush { get; set; }
        public string ButtonSwitchOnBrush { get; set; }
        public string ButtonDisableBrush { get; set; }

        #region [ RoutedEvent 'MouseClicked' ]

        /// <summary>
        /// MouseClicked Event
        /// </summary>
        public static readonly RoutedEvent MouseClickedEvent = EventManager.RegisterRoutedEvent(nameof(MouseClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(VirtureMobileHappyCoupon));
        /// <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 VirtureMobileHappyCoupon()
        {
            ButtonNormalBrush = ResourceManager.GetNximagePathAdd("btn_coupon_n.png", CommonValue.PBdesignImagesPath);
            ButtonDownBrush = ResourceManager.GetNximagePathAdd("btn_coupon_p.png", CommonValue.PBdesignImagesPath);
            ButtonSwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_coupon_s.png", CommonValue.PBdesignImagesPath);
            ButtonDisableBrush = ResourceManager.GetNximagePathAdd("btn_coupon_d.png", CommonValue.PBdesignImagesPath);
            InitializeComponent();
        }

        #endregion Ctor & Etc

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

        #region [ Event Handlers ]
        private static void OnSwitchProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as VirtureMobileHappyCoupon;
            target.OptionButton.Switch = (bool)e.NewValue;
        }
        private static void OnEnabledProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as VirtureMobileHappyCoupon;
            target.OptionButton.Enabled = (bool)e.NewValue;
        }

        private static void OnItemDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as VirtureMobileHappyCoupon;
            if (e.OldValue is M_MobileCouponDetail oldValue)
            {
                oldValue.PropertyChanged -= target.ItemData_PropertyChanged;
            }
            if (e.NewValue is M_MobileCouponDetail newValue)
            {
                newValue.PropertyChanged += target.ItemData_PropertyChanged;
                if (newValue.Status.Equals(MobileCouponStatusType.CanUse))
                {
                    switch (newValue.Type)
                    {
                        case MobileCouponType.NormalValue:
                            target.RemainAmount.Text = string.Format("잔액:{0}", newValue.CanUsingPrice.ToString("#,##0"));
                            break;
                        case MobileCouponType.ValueFixed:
                            target.RemainAmount.Text = string.Format("1회:{0}", newValue.CanUsingPrice.ToString("#,##0"));
                            break;
                        case MobileCouponType.ValueLimit:
                            target.RemainAmount.Text = string.Format("최대:{0}", newValue.CanUsingPrice.ToString("#,##0"));
                            break;
                    }
                    target.Enabled = true;
                    target.UsingValue.Visibility = newValue.DiscountPrice.Equals(0)
                                                   ? Visibility.Collapsed
                                                   : Visibility.Visible;

                }
                else
                {
                    target.Enabled = false;
                    target.UsingValue.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void ItemData_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "DiscountPrice":
                    if (this.ItemData.Status.Equals(MobileCouponStatusType.CanUse))
                    {
                        this.UsingValue.Visibility = this.ItemData.DiscountPrice.Equals(0)
                                                   ? Visibility.Collapsed
                                                   : Visibility.Visible;
                        this.Switch = this.ItemData.DiscountPrice > 0;
                    }
                    else
                    {
                        this.UsingValue.Visibility = Visibility.Collapsed;
                    }
                    break;
            }
        }

        private void OptionButton_MouseClicked(object sender, RoutedEventArgs e)
        {
            RaiseMouseClickedEvent(this.ItemData);
        }
        #endregion Event Handlers

    }
}
