using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using System.Windows.Threading;

namespace FLY.ControlLibrary.UI.OSK
{
    /// <summary>
    /// WdFullKeyboard.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// WdFullKeyboard.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// Window_FullPad.xaml 的交互逻辑
    /// </summary>
    public partial class WdFullKeyboard : Window, INotifyPropertyChanged, IVirtualKeyboard
    {
        private DispatcherTimer timer;
        private TextBox textbox = null;
        private bool press1st = true;
        public bool IsPwMode { get; set; } = false;
        FullKeyboardViewModel keyboard;
        /// <summary>
        /// 全键盘
        /// </summary>
        public WdFullKeyboard()
        {
            InitializeComponent();
            keyboard = new FullKeyboardViewModel();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(1);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            this.ShowDialog();

        }
        public void Open(TextBox textbox)
        {

            this.textbox = textbox;
            this.Result = textbox.Text;

            timer.Start();//不能直接showdialog(), 要让事件继续传递,直到有个框出来!!!

            //this.ShowDialog();
        }

        void updateResultView()
        {
            if (!IsPwMode)
            {
                this.ResultView = this.Result;
            }
            else
            {
                string star = "";
                for (int i = 0; i < this.Result.Length; i++)
                {
                    star += "*";
                }
                this.ResultView = star;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = keyboard;
            this.textblock_result.DataContext = this;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {

            if (press1st)
            {
                press1st = false;
                Result = "";
            }

            string text = ((sender as Button).Content as TextBlock).Text;

            Result += text;
        }

        private void button_caps_Click(object sender, RoutedEventArgs e)
        {
            keyboard.IsCapsLock = !keyboard.IsCapsLock;
        }

        private void button_backspace_Click(object sender, RoutedEventArgs e)
        {
            Result = "";
        }

        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_123_Click(object sender, RoutedEventArgs e)
        {
            keyboard.Is123 = !keyboard.Is123;
        }

        private void button_space_Click(object sender, RoutedEventArgs e)
        {
            Result += " ";
        }

        private void button_enter_Click(object sender, RoutedEventArgs e)
        {
            if (this.textbox != null)
            {
                this.textbox.Text = Result;
                //触发失去焦点事件!!!!!!!
                //让 绑定的 property 生效
                this.textbox.RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
            }
            this.Close();
        }


        private string result;
        /// <summary>
        /// 结果
        /// </summary>
        public string Result
        {
            get { return result; }
            set
            {
                if (result != value)
                {
                    result = value;
                    updateResultView();
                }
            }
        }
        /// <summary>
        /// 正常模式时, ResultView 与 Result 一样, 密码模式时, ResultView 显示*******
        /// </summary>
        public string ResultView { get; set; }

        /// <summary>
        /// 属性改变事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void button_dot_Click(object sender, RoutedEventArgs e)
        {
            Result += keyboard.KeyDot;
        }


    }
    public class FullKeyboardViewModel : INotifyPropertyChanged
    {

        public string KeyQ { get; set; }


        public string KeyW { get; set; }

        public string KeyE { get; set; }


        public string KeyR { get; set; }

        public string KeyT { get; set; }

        public string KeyY { get; set; }

        public string KeyU { get; set; }

        public string KeyI { get; set; }

        public string KeyO { get; set; }

        public string KeyP { get; set; }

        public string KeyA { get; set; }

        public string KeyS { get; set; }

        public string KeyD { get; set; }

        public string KeyF { get; set; }

        public string KeyG { get; set; }

        public string KeyH { get; set; }

        public string KeyJ { get; set; }

        public string KeyK { get; set; }

        public string KeyL { get; set; }

        public string KeyZ { get; set; }

        public string KeyX { get; set; }

        public string KeyC { get; set; }

        public string KeyV { get; set; }

        public string KeyB { get; set; }

        public string KeyN { get; set; }

        public string KeyM { get; set; }


        public bool IsCapsLock { get; set; }

        public bool Is123 { get; set; }

        public string KeyDot { get; set; }



        public FullKeyboardViewModel()
        {
            IsCapsLock = false;
            Is123 = false;

            UpdateKey();

            this.PropertyChanged += new PropertyChangedEventHandler(FullKeyPad_PropertyChanged);
        }

        void FullKeyPad_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Is123")
            {
                UpdateKey();
            }
            if (e.PropertyName == "IsCapsLock")
            {
                UpdateKey();
            }
        }
        void UpdateKey()
        {
            if (Is123)
            {
                To123();
            }
            else
            {
                if (IsCapsLock)
                    ToUpper();
                else
                    ToLower();
            }
        }
        void To123()
        {
            KeyQ = "1";
            KeyW = "2";
            KeyE = "3";
            KeyR = "4";
            KeyT = "5";
            KeyY = "6";
            KeyU = "7";
            KeyI = "8";
            KeyO = "9";
            KeyP = "0";
            KeyA = "!";
            KeyS = "@";
            KeyD = "#";
            KeyF = "$";
            KeyG = "%";
            KeyH = "&";
            KeyJ = "*";
            KeyK = "(";
            KeyL = ")";
            KeyZ = "'";
            KeyX = "/";
            KeyC = "-";
            KeyV = "_";
            KeyB = ":";
            KeyN = ";";
            KeyM = "?";
            KeyDot = ".";
        }
        void ToUpper()
        {
            KeyQ = "Q";
            KeyW = "W";
            KeyE = "E";
            KeyR = "R";
            KeyT = "T";
            KeyY = "Y";
            KeyU = "U";
            KeyI = "I";
            KeyO = "O";
            KeyP = "P";
            KeyA = "A";
            KeyS = "S";
            KeyD = "D";
            KeyF = "F";
            KeyG = "G";
            KeyH = "H";
            KeyJ = "J";
            KeyK = "K";
            KeyL = "L";
            KeyZ = "Z";
            KeyX = "X";
            KeyC = "C";
            KeyV = "V";
            KeyB = "B";
            KeyN = "N";
            KeyM = "M";
            KeyDot = ",";
        }
        void ToLower()
        {
            KeyQ = "q";
            KeyW = "w";
            KeyE = "e";
            KeyR = "r";
            KeyT = "t";
            KeyY = "y";
            KeyU = "u";
            KeyI = "i";
            KeyO = "o";
            KeyP = "p";
            KeyA = "a";
            KeyS = "s";
            KeyD = "d";
            KeyF = "f";
            KeyG = "g";
            KeyH = "h";
            KeyJ = "j";
            KeyK = "k";
            KeyL = "l";
            KeyZ = "z";
            KeyX = "x";
            KeyC = "c";
            KeyV = "v";
            KeyB = "b";
            KeyN = "n";
            KeyM = "m";
            KeyDot = ",";
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

}