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

namespace FLY.UI.OSK
{
    /// <summary>
    /// Window_FullPad.xaml 的交互逻辑
    /// </summary>
    public partial class Window_FullPad : Window, INotifyPropertyChanged, IVirtualKeyboard
    {
        private DispatcherTimer timer;
        private TextBox textbox = null;
        private bool press1st = true;

        FullKeyPad keypad;
        /// <summary>
        /// 全键盘
        /// </summary>
        public Window_FullPad()
        {
            InitializeComponent();
            keypad = new FullKeyPad();
            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();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = keypad;
            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)
        {
            keypad.IsCapsLock = !keypad.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)
        {
            keypad.Is123 = !keypad.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();
        }



        string result;
        /// <summary>
        /// 结果
        /// </summary>
        public string Result
        {
            get { return result; }
            set
            {
                if (result != value)
                {
                    result = value;
                    NotifyPropertyChanged("Result");
                }
            }
        }

        /// <summary>
        /// 通知属性改变
        /// </summary>
        /// <param name="propertyName"></param>
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        /// <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 += keypad.KeyDot;
        }


    }
    public class FullKeyPad : INotifyPropertyChanged
    {

        string keyq;

        public string KeyQ
        {
            get { return keyq; }
            set
            {
                if (keyq != value)
                {
                    keyq = value;
                    NotifyPropertyChanged("KeyQ");
                }
            }
        }


        string keyw;

        public string KeyW
        {
            get { return keyw; }
            set
            {
                if (keyw != value)
                {
                    keyw = value;
                    NotifyPropertyChanged("KeyW");
                }
            }
        }


        string keye;

        public string KeyE
        {
            get { return keye; }
            set
            {
                if (keye != value)
                {
                    keye = value;
                    NotifyPropertyChanged("KeyE");
                }
            }
        }


        string keyr;

        public string KeyR
        {
            get { return keyr; }
            set
            {
                if (keyr != value)
                {
                    keyr = value;
                    NotifyPropertyChanged("KeyR");
                }
            }
        }


        string keyt;

        public string KeyT
        {
            get { return keyt; }
            set
            {
                if (keyt != value)
                {
                    keyt = value;
                    NotifyPropertyChanged("KeyT");
                }
            }
        }


        string keyy;

        public string KeyY
        {
            get { return keyy; }
            set
            {
                if (keyy != value)
                {
                    keyy = value;
                    NotifyPropertyChanged("KeyY");
                }
            }
        }


        string keyu;

        public string KeyU
        {
            get { return keyu; }
            set
            {
                if (keyu != value)
                {
                    keyu = value;
                    NotifyPropertyChanged("KeyU");
                }
            }
        }


        string keyi;

        public string KeyI
        {
            get { return keyi; }
            set
            {
                if (keyi != value)
                {
                    keyi = value;
                    NotifyPropertyChanged("KeyI");
                }
            }
        }


        string keyo;

        public string KeyO
        {
            get { return keyo; }
            set
            {
                if (keyo != value)
                {
                    keyo = value;
                    NotifyPropertyChanged("KeyO");
                }
            }
        }


        string keyp;

        public string KeyP
        {
            get { return keyp; }
            set
            {
                if (keyp != value)
                {
                    keyp = value;
                    NotifyPropertyChanged("KeyP");
                }
            }
        }


        string keya;

        public string KeyA
        {
            get { return keya; }
            set
            {
                if (keya != value)
                {
                    keya = value;
                    NotifyPropertyChanged("KeyA");
                }
            }
        }


        string keys;

        public string KeyS
        {
            get { return keys; }
            set
            {
                if (keys != value)
                {
                    keys = value;
                    NotifyPropertyChanged("KeyS");
                }
            }
        }


        string keyd;

        public string KeyD
        {
            get { return keyd; }
            set
            {
                if (keyd != value)
                {
                    keyd = value;
                    NotifyPropertyChanged("KeyD");
                }
            }
        }


        string keyf;

        public string KeyF
        {
            get { return keyf; }
            set
            {
                if (keyf != value)
                {
                    keyf = value;
                    NotifyPropertyChanged("KeyF");
                }
            }
        }


        string keyg;

        public string KeyG
        {
            get { return keyg; }
            set
            {
                if (keyg != value)
                {
                    keyg = value;
                    NotifyPropertyChanged("KeyG");
                }
            }
        }


        string keyh;

        public string KeyH
        {
            get { return keyh; }
            set
            {
                if (keyh != value)
                {
                    keyh = value;
                    NotifyPropertyChanged("KeyH");
                }
            }
        }


        string keyj;

        public string KeyJ
        {
            get { return keyj; }
            set
            {
                if (keyj != value)
                {
                    keyj = value;
                    NotifyPropertyChanged("KeyJ");
                }
            }
        }


        string keyk;

        public string KeyK
        {
            get { return keyk; }
            set
            {
                if (keyk != value)
                {
                    keyk = value;
                    NotifyPropertyChanged("KeyK");
                }
            }
        }



        string keyl;

        public string KeyL
        {
            get { return keyl; }
            set
            {
                if (keyl != value)
                {
                    keyl = value;
                    NotifyPropertyChanged("KeyL");
                }
            }
        }


        string keyz;

        public string KeyZ
        {
            get { return keyz; }
            set
            {
                if (keyz != value)
                {
                    keyz = value;
                    NotifyPropertyChanged("KeyZ");
                }
            }
        }


        string keyx;

        public string KeyX
        {
            get { return keyx; }
            set
            {
                if (keyx != value)
                {
                    keyx = value;
                    NotifyPropertyChanged("KeyX");
                }
            }
        }


        string keyc;

        public string KeyC
        {
            get { return keyc; }
            set
            {
                if (keyc != value)
                {
                    keyc = value;
                    NotifyPropertyChanged("KeyC");
                }
            }
        }


        string keyv;

        public string KeyV
        {
            get { return keyv; }
            set
            {
                if (keyv != value)
                {
                    keyv = value;
                    NotifyPropertyChanged("KeyV");
                }
            }
        }


        string keyb;

        public string KeyB
        {
            get { return keyb; }
            set
            {
                if (keyb != value)
                {
                    keyb = value;
                    NotifyPropertyChanged("KeyB");
                }
            }
        }



        string keyn;

        public string KeyN
        {
            get { return keyn; }
            set
            {
                if (keyn != value)
                {
                    keyn = value;
                    NotifyPropertyChanged("KeyN");
                }
            }
        }


        string keym;

        public string KeyM
        {
            get { return keym; }
            set
            {
                if (keym != value)
                {
                    keym = value;
                    NotifyPropertyChanged("KeyM");
                }
            }
        }


        bool isCapsLock;

        public bool IsCapsLock
        {
            get { return isCapsLock; }
            set
            {
                if (isCapsLock != value)
                {
                    isCapsLock = value;
                    NotifyPropertyChanged("IsCapsLock");
                }
            }
        }


        bool is123;

        public bool Is123
        {
            get { return is123; }
            set
            {
                if (is123 != value)
                {
                    is123 = value;
                    NotifyPropertyChanged("Is123");
                }
            }
        }

        string keydot;

        public string KeyDot
        {
            get { return keydot; }
            set
            {
                if (keydot != value)
                {
                    keydot = value;
                    NotifyPropertyChanged("KeyDot");
                }
            }
        }


        public FullKeyPad()
        {
            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 = ",";
        }
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
