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_NumPad.xaml 的交互逻辑
    /// </summary>
    public partial class Window_NumPad : Window, INotifyPropertyChanged, IVirtualKeyboard
    {
        private DispatcherTimer timer;
        private TextBox textbox = null;
        private bool press1st = true;
        /// <summary>
        /// 数字键盘
        /// </summary>
        public Window_NumPad()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(0.5);

        }

        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.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;

            if (text == ".")
            {
                //只能出现一次
                if (Result.Contains("."))
                    return;
            }
            Result += text;
        }

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

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

        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 void button_symbol_Click(object sender, RoutedEventArgs e)
        {
            if (press1st)
            {
                press1st = false;
                Result = "";
            }

            if (string.IsNullOrEmpty(Result))
            {
                Result = "-";
            }
            else
            {
                if (Result[0] == '-')
                    Result = Result.Substring(1);
                else
                    Result = "-" + Result;
            }
        }

        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();
        }
    }
    /// <summary>
    /// 虚拟键盘
    /// </summary>
    public interface IVirtualKeyboard
    {
        /// <summary>
        /// 打开键盘
        /// </summary>
        /// <param name="textbox"></param>
        void Open(TextBox textbox);
    }
}
