using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Windows;
using System.Globalization;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.MaskedInput;
using System.Collections.Generic;
using System.Reflection;
using System;

namespace GreetNumericBehavior.Helper
{

    public static class TextBoxFilters
    {
        public static readonly List<Key> _controlKeys = new List<Key>
                                                            {
                                                                Key.Back,
                                                                Key.CapsLock,
                                                                Key.Ctrl,
                                                                Key.Down,
                                                                Key.End,
                                                                Key.Enter,
                                                                Key.Escape,
                                                                Key.Home,
                                                                Key.Insert,
                                                                Key.Left,
                                                                Key.PageDown,
                                                                Key.PageUp,
                                                                Key.Right,
                                                                Key.Shift,
                                                                Key.Tab,
                                                                Key.Up
                                                            };

        public static bool _IsDigit(Key key, int PlatformKeyCode, NumericFilterType filterType, string currentValue)
        {
            bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;

            bool retVal;
            if (((key >= Key.D0 && key <= Key.D9) || ((key == Key.Decimal || PlatformKeyCode == 190) && !currentValue.Contains(".")) &&
                filterType == NumericFilterType.NumericWithDecimal) && !shiftKey)
            {
                retVal = true;
            }
            else
            {
                retVal = key >= Key.NumPad0 && key <= Key.NumPad9;
            }
            return retVal;
        }


    }

    public partial class NumericTextBoxBehavior : Behavior<RadMaskedTextBox>
    {

        public static NumericFilterType GetNumericFilter(DependencyObject src)
        {
            return (NumericFilterType)src.GetValue(NumericFilterProperty);
        }

        public static void SetNumericFilter(DependencyObject src, NumericFilterType value)
        {
            src.SetValue(NumericFilterProperty, value);
        }

        public static DependencyProperty NumericFilterProperty =
            DependencyProperty.RegisterAttached(
            "NumericFilter", typeof(NumericFilterType), typeof(TextBoxFilters),
            new PropertyMetadata(NumericFilterType.None, NumericFilterChanged));

        public static void NumericFilterChanged
            (DependencyObject src, DependencyPropertyChangedEventArgs args)
        {
            if (src != null && src is RadMaskedTextBox)
            {
                RadMaskedTextBox textBox = src as RadMaskedTextBox;
                if ((NumericFilterType)args.NewValue != NumericFilterType.None)
                {
                    textBox.KeyDown += _TextBoxNumericKeyDown;

                }
            }
        }

        static void _TextBoxNumericKeyDown(object sender, KeyEventArgs e)
        {
            bool handled = true;
            RadMaskedTextBox textBox = sender as RadMaskedTextBox;

            //NumericFilterType filterType = (NumericFilterType)textBox.ReadLocalValue(NumericFilterProperty);
            NumericFilterType filterType = (NumericFilterType)textBox.GetValue(NumericFilterProperty);

            if (TextBoxFilters._controlKeys.Contains(e.Key) || TextBoxFilters._IsDigit(e.Key, e.PlatformKeyCode, filterType, textBox.Value == null
                ? string.Empty : textBox.Value.ToString()))
            {
                handled = false;
            }

            e.Handled = handled;
        }

        #region Overrides

        //Unsubscribe from the event to make sure memory leaks are prevented
        protected override void OnDetaching()
        {
            if (AssociatedObject != null)
            {
                AssociatedObject.KeyDown -= _TextBoxNumericKeyDown;
            }

            base.OnDetaching();
        }
        #endregion
    }

    public enum NumericFilterType
    {
        None = 0,
        NumericWithDecimal = 1,
        NumericWithOutDecimal = 2
    }
}
