using ImTools;
using System;
using System.Collections.Generic;
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;
using static ImTools.ImMap;

namespace PDF_Office.CustomControl
{
    /// <summary>
    /// CommonWritableComboBox.xaml 的交互逻辑
    /// </summary>
    public partial class CommonWritableComboBox : UserControl
    {
        public CommonWritableComboBox()
        {
            InitializeComponent();
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata("0"));


        /// <summary>
        /// 当前值
        /// </summary>
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set
            {
                if (value > Maximum)
                {
                    SetValue(ValueProperty, Maximum);
                }
                else if (value < Minimum)
                {
                    SetValue(ValueProperty, Minimum);
                    
                }
                else
                {
                    SetValue(ValueProperty, value);
                }
            }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));

        public List<string> TypeSouce
        {
            get { return (List<string>)GetValue(TypeSouceProperty); }
            set { SetValue(TypeSouceProperty, value); }
        }

        public static readonly DependencyProperty TypeSouceProperty =
            DependencyProperty.Register("TypeSouce", typeof(List<string>), typeof(CommonWritableComboBox), new PropertyMetadata(new List<string>()));

        /// <summary>
        /// 最大值
        /// </summary>
        public double Maximum
        {
            get { return (double)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.Register("Maximum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(9999.0));

        
        /// <summary>
        /// 最小值
        /// </summary>
        public double Minimum
        {
            get { return (double)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }

        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.Register("Minimum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));

        /// <summary>
        /// 单位符号
        /// </summary>
        public string Unit
        {
            get { return (string)GetValue(UnitProperty); }
            set { SetValue(UnitProperty, value); }
        }

        public static readonly DependencyProperty UnitProperty =
            DependencyProperty.Register("Unit", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata("%"));

        private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
        {
            
            if (this.TextBox_Num.Text == "" )
            {
                this.TextBox_Num.Text ="0";
            }

            if (!int.TryParse(this.TextBox_Num.Text, out _)&&!int.TryParse(this.TextBox_Num.Text.Replace(Unit, ""), out _)) { this.TextBox_Num.Text = Text; }
            else{ 
            Text = this.TextBox_Num.Text;
            Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            this.TextBox_Num.Text = TypeSouce[ComboBox_Type.SelectedIndex];
            Text = this.TextBox_Num.Text;
            Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
        }
        private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
        }

        private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Value > Maximum)
                {
                    this.TextBox_Num.Text = Maximum.ToString();
                }
                else if (Value < Minimum)
                {
                    this.TextBox_Num.Text = Minimum.ToString();

                }
                else
                {
                    this.TextBox_Num.Text=Value.ToString();
                }
                this.TextBox_Num.Text = this.TextBox_Num.Text.Replace(" " , "").Replace(Unit,"")+" "+Unit;
                

            }
        }
    }
}
