using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using SPC.Kiosk.Base;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// 다국어 지원 TextBlock
    /// </summary>
    public class MutiLanguageTextBlock : TextBlock
    {
        #region [ Members ]
        /// <summary>
        /// Muti Language Data
        /// </summary>
        public static readonly DependencyProperty MultiLanguageProperty =
         DependencyProperty.Register(nameof(MultiLanguage), typeof(List<M_Language>), typeof(MutiLanguageTextBlock), new PropertyMetadata(default(List<M_Language>), new PropertyChangedCallback(OnMultiLanguagePropertyChanged)));
        /// <summary>
        /// Display Language
        /// </summary>
        public static readonly DependencyProperty DisplayLanguagePropery =
         DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(MutiLanguageTextBlock), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
        /// <summary>
        /// Guid Text (if Show Text is empty)
        /// </summary>
        public static readonly DependencyProperty GuidTextProperty =
                 DependencyProperty.Register(nameof(GuidText), typeof(List<M_Language>), typeof(MutiLanguageTextBlock), new PropertyMetadata(default(List<M_Language>), new PropertyChangedCallback(OnGuidTextPropertyChanged)));
        /// <summary>
        /// Text Blinking Flag
        /// </summary>
        public static readonly DependencyProperty BlinkTextProperty =
                 DependencyProperty.Register(nameof(BlinkText), typeof(bool), typeof(MutiLanguageTextBlock), new PropertyMetadata(false, new PropertyChangedCallback(OnBlinkTextPropertyChanged)));
        /// <summary>
        /// Auto Text Trim
        /// </summary>
        public static readonly DependencyProperty AutoTextTrimProperty =
                 DependencyProperty.Register(nameof(AutoTextTrim), typeof(bool), typeof(MutiLanguageTextBlock), new PropertyMetadata(false, new PropertyChangedCallback(OnAutoTextTrimPropertyChanged)));

        ~MutiLanguageTextBlock()
        {
            if (this.BlickTimer != null)
            {
                this.BlickTimer.Stop();
                this.BlickTimer.Tick -= this.BlickTimer_Tick;
                this.BlickTimer = null;
            }
        }

        /// <summary>
        /// Blinking Timer 
        /// </summary>
        public DispatcherTimer BlickTimer { get; protected set; }
        public bool AutoTextTrim
        {
            get { return (bool)GetValue(AutoTextTrimProperty); }
            set { SetValue(AutoTextTrimProperty, value); }
        }

        /// <summary>
        /// Text Blinking Flag
        /// </summary>
        public bool BlinkText
        {
            get { return (bool)GetValue(BlinkTextProperty); }
            set { SetValue(BlinkTextProperty, value); }
        }
        /// <summary>
        /// Guid Text (if Show Text is empty)
        /// </summary>
        public List<M_Language> GuidText
        {
            get { return (List<M_Language>)GetValue(GuidTextProperty); }
            set { SetValue(GuidTextProperty, value); }
        }
        /// <summary>
        /// Muti Language Data
        /// </summary>
        public List<M_Language> MultiLanguage
        {
            get { return (List<M_Language>)GetValue(MultiLanguageProperty); }
            set { SetValue(MultiLanguageProperty, value); }
        }
        /// <summary>
        /// Display Language
        /// </summary>
        public SupportLanguageType DisplayLanguage
        {
            get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
            set { SetValue(DisplayLanguagePropery, value); }
        }
        #endregion Members

        #region [  Methods  ]
        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            base.OnTextInput(e);
            if (string.IsNullOrEmpty(e.Text))
            {
                var target = e.OriginalSource as MutiLanguageTextBlock;
                target.Text = target.GuidText.GetLanguageData(target.DisplayLanguage);
            }
        }
        private void AutoTextTrimming()
        {
            if (this.AutoTextTrim)
            {
                this.Text = this.Text.TextTrimming(this.DisplayLanguage
                                                 , this.FontSize
                                                 , this.FontFamily
                                                 , this.FontStyle
                                                 , this.FontWeight
                                                 , this.FontStretch
                                                 , this.Width);
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private static void OnMultiLanguagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && !e.NewValue.Equals(e.OldValue))
            {
                var target = d as MutiLanguageTextBlock;
                target.Text = string.Empty;
                if (e.NewValue is List<M_Language> multiLnaguage)
                {
                    target.Text = multiLnaguage.GetLanguageData(target.DisplayLanguage);
                    target.AutoTextTrimming();
                }

            }
        }
        private static void OnBlinkTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!e.NewValue.Equals(e.OldValue))
            {
                var target = d as MutiLanguageTextBlock;
                if ((bool)e.OldValue)
                {
                    if (target.BlickTimer != null)
                    {
                        target.BlickTimer.Stop();
                        target.BlickTimer = null;
                    }
                }
                if ((bool)e.NewValue)
                {
                    target.BlickTimer = new DispatcherTimer
                    {
                        Interval = TimeSpan.FromSeconds(0.5d),
                    };
                    target.BlickTimer.Tick += target.BlickTimer_Tick;
                    target.BlickTimer.Start();
                }
            }
        }

        private void BlickTimer_Tick(object sender, EventArgs e)
        {
            BlickTimer.Stop();
            if (this.Visibility.Equals(Visibility.Visible))
            {
                this.Visibility = Visibility.Hidden;
            }
            else
            {
                this.Visibility = Visibility.Visible;
            }
            BlickTimer.Start();
        }

        private static void OnGuidTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as MutiLanguageTextBlock;
            if (e.NewValue is List<M_Language> guideText)
            {
                target.Text = guideText.GetLanguageData(target.DisplayLanguage);
                target.AutoTextTrimming();
            }
        }
        private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && !e.NewValue.Equals(e.OldValue))
            {
                var target = d as MutiLanguageTextBlock;
                target.Text = string.Empty;
                if (target.MultiLanguage is List<M_Language> multiLnaguage && e.NewValue is SupportLanguageType languageType)
                {
                    target.Text = multiLnaguage.GetLanguageData(languageType);
                    target.AutoTextTrimming();
                }
            }
        }
        private static void OnAutoTextTrimPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var target = d as MutiLanguageTextBlock;
                target.AutoTextTrimming();

            }
        }

        #endregion Event Handlers

    }
}
