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

namespace BWP.WinFormControl
{
  /// <summary>
  /// VirtualKeyPad.xaml 的交互逻辑
  /// </summary>
  public partial class VirtualKeyPad : INotifyPropertyChanged
  {
    private bool _showNumericKeyboard;
    public bool ShowNumericKeyboard
    {
      get { return _showNumericKeyboard; }
      set { _showNumericKeyboard = value; OnPropertyChanged("ShowNumericKeyboard"); }
    }

    private string _result;
    public string Result
    {
      get { return _result; }
      private set { _result = value; OnPropertyChanged("Result"); }
    }

    public VirtualKeyPad()
    {
      InitializeComponent();
      DataContext = this;
      Result = "";
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
      var button = sender as Button;
      if (button != null)
      {
        switch (button.CommandParameter.ToString())
        {
          case "LSHIFT":
            var upperCaseRegex = new Regex("[A-Z]");
            var lowerCaseRegex = new Regex("[a-z]");
            Button btn;
            foreach (UIElement elem in AlfaKeyboard.Children) //iterate the main grid
            {
              var grid = elem as Grid;
              if (grid != null)
              {
                foreach (UIElement uiElement in grid.Children)  //iterate the single rows
                {
                  btn = uiElement as Button;
                  if (btn != null) // if button contains only 1 character
                  {
                    if (btn.Content.ToString().Length == 1)
                    {
                      if (upperCaseRegex.Match(btn.Content.ToString()).Success) // if the char is a letter and uppercase
                        btn.Content = btn.Content.ToString().ToLower();
                      else if (lowerCaseRegex.Match(button.Content.ToString()).Success) // if the char is a letter and lower case
                        btn.Content = btn.Content.ToString().ToUpper();
                    }
                  }
                }
              }
            }
            break;

          case "ALT":
          case "CTRL":
            break;
          case "RETURN":
            DialogResult = true;
            break;
          case "BACK":
            if (Result.Length > 0)
              Result = Result.Remove(Result.Length - 1);
            break;
          default:
            Result += button.Content.ToString();
            break;
        }
      }
    }

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