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;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// NumericPad.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class PBNumericPad : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// Button Images
        /// </summary>
        public static readonly DependencyProperty ButtonImagesProperty =
                 DependencyProperty.Register(nameof(ButtonImages), typeof(List<M_NumberPadImages>), typeof(PBNumericPad), new PropertyMetadata(null, new PropertyChangedCallback(OnButtonImagesPropertyChanged)));
        /// <summary>
        /// Input Text
        /// </summary>
        public static readonly DependencyProperty InputTextProperty =
                 DependencyProperty.Register(nameof(InputText), typeof(string), typeof(PBNumericPad), 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(PBNumericPad), new PropertyMetadata(string.Empty));
        /// <summary>
        /// Text Area Visible
        /// </summary>
        public static readonly DependencyProperty TextVisibleProperty =
                 DependencyProperty.Register(nameof(TextVisibility), typeof(Visibility), typeof(PBNumericPad), new PropertyMetadata(Visibility.Visible));
        /// <summary>
        /// Guid Text (Mutil Language)
        /// </summary>
        public static readonly DependencyProperty GuidTextProperty =
                 DependencyProperty.Register(nameof(GuidText), typeof(List<M_Language>), typeof(PBNumericPad), new PropertyMetadata(default(List<M_Language>), new PropertyChangedCallback(OnGuidTextPropertyChanged)));
        /// <summary>
        /// Max input Length
        /// </summary>
        public static readonly DependencyProperty MaxLengthPropery =
                 DependencyProperty.Register(nameof(MaxLength), typeof(int), typeof(PBNumericPad), new PropertyMetadata(0));
        /// <summary>
        /// Is Formated Show Text
        /// </summary>
        public static readonly DependencyProperty FormatedPropery =
                 DependencyProperty.Register(nameof(Formated), typeof(bool), typeof(PBNumericPad), new PropertyMetadata(false));
        /// <summary>
        /// CurrencyFormat Text
        /// </summary>
        public static readonly DependencyProperty CurrencyFormatPropery =
                 DependencyProperty.Register(nameof(CurrencyFormat), typeof(string), typeof(PBNumericPad), new PropertyMetadata("#,##0"));
        /// <summary>
        /// Display Language Type
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
                 DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(PBNumericPad), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
        /// <summary>
        /// Is PassWord
        /// </summary>
        public static readonly DependencyProperty IsPassWordPropery =
                 DependencyProperty.Register(nameof(IsPassWord), typeof(bool), typeof(PBNumericPad), new PropertyMetadata(false));

        /// <summary>
        /// CurrencyFormat Text
        /// </summary>
        public string CurrencyFormat
        {
            get { return (string)GetValue(CurrencyFormatPropery); }
            set { SetValue(CurrencyFormatPropery, value); }
        }
        /// <summary>
        /// Is PassWord
        /// </summary>
        public bool IsPassWord
        {
            get { return (bool)GetValue(IsPassWordPropery); }
            set { SetValue(IsPassWordPropery, value); }
        }
        /// <summary>
        /// Is Formated Show Text
        /// </summary>
        public List<M_NumberPadImages> ButtonImages
        {
            get { return (List<M_NumberPadImages>)GetValue(ButtonImagesProperty); }
            set { SetValue(ButtonImagesProperty, value); }
        }
        /// <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); }
        }
        #endregion Members

        #region [ Ctor/Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public PBNumericPad()
        {
            try
            {
                InitializeComponent();

                foreach (var aElement in this.BaseGrid.Children)
                {
                    if (aElement is AnimationButton aNumberButton)
                    {
                        aNumberButton.MouseClicked += NumberButton_MouseClicked;
                    }
                }
                SetImage();
            }
            catch 
            {
            }
        }

        #endregion Ctor

        #region [ Methods ]

        private void SetShowText(string _showText)
        {
            string displayText = _showText;
            if (!string.IsNullOrEmpty(displayText))
            {
                if (displayText.Length < 10)
                {
                    
                }
                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 (IsPassWord)
                {
                    formatedString = string.Empty.PadRight(displayText.Length, '*');
                }
                else
                {
                    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
                    {
                        if (string.IsNullOrEmpty(CurrencyFormat))
                        {
                            formatedString = displayText;
                        }
                        else
                        {
                            formatedString = (double.Parse(displayText)).ToString(CurrencyFormat);
                        }
                    }
                }
                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;
        }

        private void SetImage(object _buttonImages = null)
        {
            var getButtonImages = new List<M_NumberPadImages>();
            if (_buttonImages is List<M_NumberPadImages>)
            {
                getButtonImages = _buttonImages as List<M_NumberPadImages>;
            }
            else
            {
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button0,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_0.png", CommonValue.PBdesignImagesPath)
                    }    
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button1,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_1.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button2,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_2.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button3,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_3.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button4,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_4.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button5,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_5.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button6,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_6.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button7,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_7.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button8,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_8.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.Button9,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_9.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.ButtonCancel,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_c.png", CommonValue.PBdesignImagesPath)
                    }
                );
                getButtonImages.Add
                (
                    new M_NumberPadImages
                    {
                        ButtonType = NumberPadButton.ButtonBack,
                        ButtonImage = ResourceManager.GetNximagePathAdd("btn_number_d.png", CommonValue.PBdesignImagesPath)
                    }
                );
            }
            foreach (var aElement in this.BaseGrid.Children)
            {
                if (aElement is AnimationButton aNumberButton)
                {
                    var getType = NumberPadButton.ButtonNone;
                    var ButtonTag = string.Empty;
                    switch (aNumberButton.Name)
                    {
                        case nameof(Button0):
                            getType = NumberPadButton.Button0;
                            ButtonTag = "0";
                            break;
                        case nameof(Button1):
                            getType = NumberPadButton.Button1;
                            ButtonTag = "1";
                            break;
                        case nameof(Button2):
                            getType = NumberPadButton.Button2;
                            ButtonTag = "2";
                            break;
                        case nameof(Button3):
                            getType = NumberPadButton.Button3;
                            ButtonTag = "3";
                            break;
                        case nameof(Button4):
                            getType = NumberPadButton.Button4;
                            ButtonTag = "4";
                            break;
                        case nameof(Button5):
                            getType = NumberPadButton.Button5;
                            ButtonTag = "5";
                            break;
                        case nameof(Button6):
                            getType = NumberPadButton.Button6;
                            ButtonTag = "6";
                            break;
                        case nameof(Button7):
                            getType = NumberPadButton.Button7;
                            ButtonTag = "7";
                            break;
                        case nameof(Button8):
                            getType = NumberPadButton.Button8;
                            ButtonTag = "8";
                            break;
                        case nameof(Button9):
                            getType = NumberPadButton.Button9;
                            ButtonTag = "9";
                            break;
                        case nameof(ButtonCancel):
                            getType = NumberPadButton.ButtonCancel;
                            break;
                        case nameof(ButtonBack):
                            getType = NumberPadButton.ButtonBack;
                            break;
                    }
                    aNumberButton.Tag = ButtonTag;
                    var findButtonImage = getButtonImages.Find(r => r.ButtonType.Equals(getType));
                    aNumberButton.Width = this.Width / 3;
                    aNumberButton.Height = this.Height / 4;
                    if (findButtonImage is M_NumberPadImages)
                    {
                        aNumberButton.NormalBrush = findButtonImage.ButtonImage;
                    }

                }
            }
            this.UpdateLayout();
        }
        #endregion Methods

        #region [ Event Handlers ]
        private static void OnButtonImagesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var target = d as PBNumericPad;
                target.SetImage(e.NewValue);
            }
        }

        private static void OnGuidTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var target = d as PBNumericPad;
                target.SetShowText(target.InputText);
            }
        }

        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null )
            {
                var target = d as PBNumericPad;
                target.SetShowText(target.InputText);
            }
        }
        private static void OnInputTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as PBNumericPad;
            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 + (string)aNumberButton.Tag; 
                            }
                        }
                        else
                        {
                            if (!(this.InputText.Length.Equals(0) && aNumberButton.Tag.Equals("0")))
                            {
                                this.InputText = this.InputText + (string)aNumberButton.Tag;
                            }

                        }
                        break;
                }
            }
        }

        #endregion Event Handlers
    }

}
