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;

namespace FLY.UI.OSK
{
    /// <summary>
    /// Window_NumPad.xaml 的交互逻辑
    /// </summary>
    public partial class Window_NumPad : Window, INotifyPropertyChanged
    {
        private System.Windows.Threading.DispatcherTimer timer;
        private TextBox textbox = null;


        public Window_NumPad()
        {
            InitializeComponent();

            timer = new System.Windows.Threading.DispatcherTimer();

            timer.Interval = TimeSpan.FromSeconds(0.1);
            timer.Tick += new EventHandler(timer_Tick);
        }
        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();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.textblock_result.DataContext = this;
        }
        private void button_7_Click(object sender, RoutedEventArgs e)
        {
            Result += "7";
        }

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

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

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

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

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

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

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

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

        private void button_backspace_Click(object sender, RoutedEventArgs e)
        {
            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;
            }
            this.Close();
        }

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

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

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

        string result;

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


        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

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