using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
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;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// NumericPad.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class NumericPad2 : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// Noraml Bakcground 
        /// </summary>
        public static readonly DependencyProperty NormalBrushProperty =
                 DependencyProperty.Register(nameof(NormalBrush), typeof(string), typeof(NumericPad2), new PropertyMetadata("White"));
        /// <summary>
        /// Touch Down Bakcground 
        /// </summary>
        public static readonly DependencyProperty DownBrushProperty =
                 DependencyProperty.Register(nameof(DownBrush), typeof(string), typeof(NumericPad2), new PropertyMetadata("DarkGray"));
        /// <summary>
        /// Cancel Noraml Bakcground 
        /// </summary>
        public static readonly DependencyProperty CancelNormalBrushProperty =
                 DependencyProperty.Register(nameof(CancelNormalBrush), typeof(string), typeof(NumericPad2), new PropertyMetadata("DarkRed"));
        /// <summary>
        /// Cancel Down Bakcground 
        /// </summary>
        public static readonly DependencyProperty CancelDownBrushProperty =
                 DependencyProperty.Register(nameof(CancelDownBrush), typeof(string), typeof(NumericPad2), new PropertyMetadata("DarkGray"));
        /// <summary>
        /// Click Animation Type
        /// </summary>
        public static readonly DependencyProperty ClickAnimationTypeProperty =
                 DependencyProperty.Register(nameof(ClickAnimationType), typeof(ButtonAnimationType), typeof(NumericPad2), new PropertyMetadata(ButtonAnimationType.None));
        /// <summary>
        /// Button Text Foreground
        /// </summary>
        public static readonly DependencyProperty TextForegroundProperty =
                 DependencyProperty.Register(nameof(TextForeground), typeof(string), typeof(NumericPad2), new PropertyMetadata("Black"));
        /// <summary>
        /// Cancel & Back Button Foreground
        /// </summary>
        public static readonly DependencyProperty CancelForegroundProperty =
                 DependencyProperty.Register(nameof(CancelForeground), typeof(string), typeof(NumericPad2), new PropertyMetadata("White"));
        /// <summary>
        /// Input Text
        /// </summary>
        public static readonly DependencyProperty InputTextProperty =
                 DependencyProperty.Register(nameof(InputText), typeof(string), typeof(NumericPad2), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnInputTextPropertyChanged)));
        /// <summary>
        /// Show Text With Mask
        /// </summary>
        public static readonly DependencyProperty ShowTextProperty =
                 DependencyProperty.Register(nameof(ShowText), typeof(string), typeof(NumericPad2), new PropertyMetadata(string.Empty));
        /// <summary>
        /// Text Area Visible
        /// </summary>
        public static readonly DependencyProperty TextVisibleProperty =
                 DependencyProperty.Register(nameof(TextVisibility), typeof(Visibility), typeof(NumericPad2), new PropertyMetadata(Visibility.Visible));
        /// <summary>
        /// Guid Text (Mutil Language)
        /// </summary>
        public static readonly DependencyProperty GuidTextProperty =
                 DependencyProperty.Register(nameof(GuidText), typeof(List<M_Language>), typeof(NumericPad2), new PropertyMetadata(default(List<M_Language>), new PropertyChangedCallback(OnGuidTextPropertyChanged)));
        /// <summary>
        /// Cancel Text
        /// </summary>
        public static readonly DependencyProperty CancelTextProperty =
                 DependencyProperty.Register(nameof(CancelText), typeof(string), typeof(NumericPad2), new PropertyMetadata("Cancel", new PropertyChangedCallback(OnCancelTextPropertyChanged)));
        /// <summary>
        /// Cancel Text (Mutil Language)
        /// </summary>
        public static readonly DependencyProperty CancelLanguageTextProperty =
                 DependencyProperty.Register(nameof(CancelLanguageText), typeof(List<M_Language>), typeof(NumericPad2), new PropertyMetadata(default(List<M_Language>), new PropertyChangedCallback(OnCancelLanguageTextPropertyChanged)));
        /// <summary>
        /// Display Language Type
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(NumericPad2), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
        /// <summary>
        /// Max input Length
        /// </summary>
        public static readonly DependencyProperty MaxLengthPropery =
                 DependencyProperty.Register(nameof(MaxLength), typeof(int), typeof(NumericPad2), new PropertyMetadata(0));
        /// <summary>
        /// Is Formated Show Text
        /// </summary>
        public static readonly DependencyProperty FormatedPropery =
                 DependencyProperty.Register(nameof(Formated), typeof(bool), typeof(NumericPad2), new PropertyMetadata(false));
        /// <summary>
        /// Is Formated Show Text
        /// </summary>
        public bool Formated
        {
            get { return (bool)GetValue(FormatedPropery); }
            set { SetValue(FormatedPropery, value); }
        }
        /// <summary>
        /// Max input Length
        /// </summary>
        public int MaxLength
        {
            get { return (int)GetValue(MaxLengthPropery); }
            set { SetValue(MaxLengthPropery, value); }
        }

        /// <summary>
        /// Text Area Visibility
        /// </summary>
        public Visibility TextVisibility
        {
            get { return (Visibility)GetValue(TextVisibleProperty); }
            set { SetValue(TextVisibleProperty, value); }
        }
        /// <summary>
        /// Input Text (Real Data)
        /// </summary>
        public string InputText
        {
            get { return (string)GetValue(InputTextProperty); }
            set { SetValue(InputTextProperty, value); }
        }
        /// <summary>
        /// Show Text With Mask
        /// </summary>
        public string ShowText
        {
            get { return (string)GetValue(ShowTextProperty); }
            set { SetValue(ShowTextProperty, value); }
        }
        /// <summary>
        /// Guid Text (Multi Language)
        /// </summary>
        public List<M_Language> GuidText
        {
            get { return (List<M_Language>)GetValue(GuidTextProperty); }
            set { SetValue(GuidTextProperty, value); }
        }
        /// <summary>
        /// Display Language Type
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        /// <summary>
        /// Cancel Text
        /// </summary>
        public string CancelText
        {
            get { return (string)GetValue(CancelTextProperty); }
            set { SetValue(CancelTextProperty, value); }
        }
        /// <summary>
        /// Cancel Text (Multi Language) 
        /// </summary>
        public List<M_Language> CancelLanguageText
        {
            get { return (List<M_Language>)GetValue(CancelLanguageTextProperty); }
            set { SetValue(CancelLanguageTextProperty, value); }
        }
        /// <summary>
        /// Noraml Bakcground 
        /// </summary>
        public string NormalBrush
        {
            get { return (string)GetValue(NormalBrushProperty); }
            set { SetValue(NormalBrushProperty, value); }
        }
        /// <summary>
        /// Mouse/Touch Down Bakcground 
        /// </summary>
        public string DownBrush
        {
            get { return (string)GetValue(DownBrushProperty); }
            set { SetValue(DownBrushProperty, value); }
        }
        /// <summary>
        /// Cancel Button NormalBrush
        /// </summary>
        public string CancelNormalBrush
        {
            get { return (string)GetValue(CancelNormalBrushProperty); }
            set { SetValue(CancelNormalBrushProperty, value); }
        }
        /// <summary>
        /// Mouse/Touch Down Bakcground 
        /// </summary>
        public string CancelDownBrush
        {
            get { return (string)GetValue(CancelDownBrushProperty); }
            set { SetValue(CancelDownBrushProperty, value); }
        }
        /// <summary>
        /// Button Animation Type
        /// </summary>
        public ButtonAnimationType ClickAnimationType
        {
            get { return (ButtonAnimationType)GetValue(ClickAnimationTypeProperty); }
            set { SetValue(ClickAnimationTypeProperty, value); }
        }
        /// <summary>
        /// Button Text Foreground
        /// </summary>
        public string TextForeground
        {
            get { return (string)GetValue(TextForegroundProperty); }
            set { SetValue(TextForegroundProperty, value); }
        }
        /// <summary>
        /// Cancel Button Foreground
        /// </summary>
        public string CancelForeground
        {
            get { return (string)GetValue(CancelForegroundProperty); }
            set { SetValue(CancelForegroundProperty, value); }
        }
        #endregion Members

        #region [ Ctor/Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public NumericPad2()
        {
            InitializeComponent();
            
            foreach (var aElement in this.BaseGrid.Children)
            {
                if (aElement is AnimationButton aNumberButton)
                {
                    aNumberButton.MouseClicked += NumberButton_MouseClicked;
                }
            }
        }

        #endregion Ctor

        #region [ Methods ]

        private void SetShowText(string _showText)
        {
            string displayText = _showText;
            if (!string.IsNullOrEmpty(displayText))
            {
                if (displayText.Length < 10)
                {
                    //pass
                }
                else if (displayText.Length.Equals(10) && Formated)
                {
                    var displayChars = displayText.ToCharArray();
                    displayText = string.Empty;
                    for (int i = 0; i < displayChars.Length; i++)
                    {
                        if (i > 2 && i < 6)
                        {
                            displayText += '*';
                        }
                        else
                        {
                            displayText += displayChars[i];
                        }
                    }
                }
                else if (displayText.Length.Equals(11) && Formated)
                {
                    var displayChars = displayText.ToCharArray();
                    displayText = string.Empty;
                    for (int i = 0; i < displayChars.Length; i++)
                    {
                        if (i > 2 && i < 7)
                        {
                            displayText += '*';
                        }
                        else
                        {
                            displayText += displayChars[i];
                        }
                    }
                }
                else if (displayText.Length < 17 && Formated)
                {
                    var displayChars = displayText.ToCharArray();
                    displayText = string.Empty;
                    for (int i = 0; i < displayChars.Length; i++)
                    {
                        if (i > 5 && i < 14)
                        {
                            displayText += '*';
                        }
                        else
                        {
                            displayText += displayChars[i];
                        }
                    }

                }
                else if (Formated)
                {
                    var displayChars = displayText.ToCharArray();
                    displayText = string.Empty;
                    for (int i = 0; i < displayChars.Length; i++)
                    {
                        if (i > 3 && i < 16)
                        {
                            displayText += '*';
                        }
                        else
                        {
                            displayText += displayChars[i];
                        }
                    }
                }
                string formatedString = string.Empty;
                if (Formated)
                {
                    if (displayText.Length.Equals(10))
                    {
                        formatedString = string.Format("{0}-{1}-{2}"
                                                , displayText.Substring(0, 3)
                                                , displayText.Substring(3, 3)
                                                , displayText.Substring(6, 4));
                    }
                    else if (displayText.Length.Equals(11))
                    {
                        formatedString = string.Format("{0}-{1}-{2}"
                                                , displayText.Substring(0, 3)
                                                , displayText.Substring(3, 4)
                                                , displayText.Substring(7, 4));
                    }
                    else if (displayText.Length.Equals(16))
                    {
                        formatedString = string.Format("{0}-{1}-{2}-{3}"
                                                , displayText.Substring(0, 4)
                                                , displayText.Substring(4, 4)
                                                , displayText.Substring(8, 4)
                                                , displayText.Substring(12, 4));
                    }
                    else if (displayText.Length.Equals(20))
                    {
                        formatedString = string.Format("{0}-{1}-{2}-{3}-{4}"
                                                , displayText.Substring(0, 4)
                                                , displayText.Substring(4, 4)
                                                , displayText.Substring(8, 4)
                                                , displayText.Substring(12, 4)
                                                , displayText.Substring(16, 4));
                    }
                }
                else
                {
                    formatedString = (int.Parse(displayText)).ToString("#,###");
                }
                if (!string.IsNullOrEmpty(formatedString)) displayText =  formatedString;
                this.ShowText = displayText;
            }
            else
            {
                this.ShowText = this.GuidText.GetLanguageData(this.DisplayLanguage);
            }
        }
        private double SetEtcTextFontSize(AnimationButton _target, string _text)
        {
            double result = 20d;
            try
            {
                var textLength = string.IsNullOrEmpty(_text.Trim()) ? 1 : _text.Trim().Length;
                var fontSize = 0d;
                switch (textLength)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        fontSize = 0.35;
                        break;
                    default:
                        fontSize = 0.3;
                        break;
                }
                result = _target.ActualHeight > 0 ? _target.ActualHeight * fontSize : 20d;

            }
            catch 
            {
                result = 20d;
            }
            return result;
        }
        #endregion Methods

        #region [ Event Handlers ]
        private static void OnCancelTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null )
            {
                var target = d as NumericPad2;
                var aElement = target.BaseGrid.FindName("ButtonCancel");
                if (aElement is AnimationButton aNumberButton)
                {
                    var newFontSize = target.SetEtcTextFontSize(aNumberButton, (string)e.NewValue);
                    target.ButtonCancel.TextFontSize = newFontSize;
                }
                
            }
        }
        private static void OnCancelLanguageTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null )
            {
                var target = d as NumericPad2;
                var aElement = target.BaseGrid.FindName("ButtonCancel");
                if (aElement is AnimationButton aNumberButton)
                {
                    var newFontSize = target.SetEtcTextFontSize(aNumberButton
                                                                ,((List<M_Language>)e.NewValue).GetLanguageData(target.DisplayLanguage));
                    target.ButtonCancel.TextFontSize = newFontSize;
                }
            }
        }
        private static void OnGuidTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var target = d as NumericPad2;
                target.SetShowText(target.InputText);
            }
        }

        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null )
            {
                var target = d as NumericPad2;
                foreach (var aElement in target.BaseGrid.Children)
                {
                    if (aElement is AnimationButton aNumberButton
                        && aNumberButton.Name.StartsWith("ButtonCancel"))
                    {
                        aNumberButton.DisplayLanguage = (SupportLanguageType)e.NewValue;
                    }
                }
                target.SetShowText(target.InputText);
            }
        }
        private static void OnInputTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as NumericPad2;
            var inputText = (string)e.NewValue;
            target.SetShowText(inputText);
        }
        private void NumberButton_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (sender is AnimationButton aNumberButton)
            {
                switch (aNumberButton.Name)
                {
                    case nameof(ButtonCancel):
                        this.InputText = string.Empty;
                        break;
                    case nameof(ButtonBack):
                        if (this.InputText != null && this.InputText.Length > 0)
                        {
                            this.InputText = this.InputText.Remove(this.InputText.Length - 1, 1);
                        }
                        break;
                    default:
                        if (!this.MaxLength.Equals(0) )
                        {
                            if (this.InputText.Length < this.MaxLength)
                            {
                                this.InputText = this.InputText + aNumberButton.Text; 
                            }
                        }
                        else
                        {
                            if (!(this.InputText.Length.Equals(0) && aNumberButton.Text.Equals("0")))
                            {
                                this.InputText = this.InputText + aNumberButton.Text;
                            }

                        }
                        break;
                }
            }
        }

        #endregion Event Handlers
    }

}
