using Caliburn.Micro;
using MECF.Framework.UI.Client.CenterViews.Dialogs;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using OpenSEMI.Ctrlib.Controls;
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.Input;
using TextBox2 = OpenSEMI.Ctrlib.Controls.TextBoxEx;
namespace MECF.Framework.UI.Client.Themes.lightgreen
{
    public partial class TextBoxEx2 : ResourceDictionary
    {
        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
        /// 
        /// 是否数字字符串
        /// 
        /// 输入字符串
        /// 
        public static bool IsNumber(string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }
        /// 
        /// 是否数字字符串 可带正负号
        /// 
        /// 输入字符串
        /// 
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }
        /// 
        /// 是否是浮点数
        /// 
        /// 输入字符串
        /// 
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }
        /// 
        /// 是否是浮点数 可带正负号
        /// 
        /// 输入字符串
        /// 
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }
        public void TextBoxExOnClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is TextBox2)
            {
                TextBox2 textBox = sender as TextBox2;
                string strRet = Show(sender, textBox.Text);
                if (string.IsNullOrEmpty(strRet)) return;
                if (!CheckEditBoxMode(textBox, strRet))
                {
                    DialogBox.ShowInfo($"Please enter the corresponding type data {textBox.EditBoxMode.ToString()}");
                    return;
                }
                if (!textBox.AllowIsOutOfRangeSaveChange && textBox.IsOutOfRange(double.Parse(strRet)))
                {
                    DialogBox.ShowInfo($"Out Of Range{textBox.MinValue}-{textBox.MaxValue}");
                    return;
                }
                if (textBox.EditBoxMode == EditBoxMode.SignInteger || textBox.EditBoxMode == EditBoxMode.UnSignInteger || textBox.EditBoxMode == EditBoxMode.Decimal || textBox.EditBoxMode == EditBoxMode.UnSignDecimal)
                {
                    var controlValue = double.Parse(strRet);
                    if (textBox.KeepDecimals != -1)
                    {
                        if (textBox.KeepIntegers != -1)
                        {
                            StringBuilder strFormat = new StringBuilder();
                            for (int i = 0; i < textBox.KeepIntegers; i++)
                            {
                                strFormat.Append("0");
                            }
                            strFormat.Append(".");
                            for (int i = 0; i < textBox.KeepDecimals; i++)
                            {
                                strFormat.Append("0");
                            }
                            textBox.Text = controlValue.ToString(strFormat.ToString());
                        }
                        else
                        { textBox.Text = controlValue.ToString($"f{textBox.KeepDecimals}"); }
                    }
                    else
                    {
                        if (textBox.KeepIntegers != -1)
                        {
                            textBox.Text = ((int)controlValue).ToString($"D{textBox.KeepIntegers}");
                        }
                        else
                        { textBox.Text = controlValue.ToString(); }
                    }
                }
                else
                {
                    textBox.Text = strRet;
                }
            }
        }
        private bool CheckEditBoxMode(TextBox2 boxEx, string value)
        {
            bool modeIsOk = false;
            switch (boxEx.EditBoxMode)
            {
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.Default:
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.Email:
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.Time:
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.FileName:
                    modeIsOk = true;
                    break;
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.SignInteger:
                    modeIsOk = IsNumberSign(value);
                    break;
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.UnSignInteger:
                    modeIsOk = IsNumber(value);
                    break;
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.Decimal:
                    modeIsOk = IsDecimalSign(value) || IsNumberSign(value);
                    break;
                case OpenSEMI.Ctrlib.Controls.EditBoxMode.UnSignDecimal:
                    modeIsOk = IsDecimal(value) || IsNumber(value);
                    break;
                default:
                    break;
            }
            return modeIsOk;
        }
        private string Show(object sender, string strDefaultValue)
        {
            Control control = sender as Control;
            string strRet = string.Empty;
            if (control.Tag != null && control.Tag.ToString().Equals("Number"))
            {
                var point = control.PointFromScreen(new Point(0, 0));
                double x = SystemParameters.WorkArea.Width;
                double y = SystemParameters.WorkArea.Height;
                NumberKeyboard numberKeyboard = new NumberKeyboard("", strDefaultValue);
                if (-point.Y + control.ActualHeight + 5 + numberKeyboard.Height < y)
                {
                    numberKeyboard.Top = -point.Y + control.ActualHeight + 5;
                }
                else
                {
                    numberKeyboard.Top = -point.Y - numberKeyboard.Height - 5;
                }
                if (-point.X + numberKeyboard.Width < x)
                {
                    numberKeyboard.Left = -point.X;
                }
                else
                {
                    numberKeyboard.Left = -point.X - (numberKeyboard.Width - control.ActualWidth);
                }
                if ((bool)numberKeyboard.ShowDialog()) strRet = numberKeyboard.ValueString;
            }
            else if (control.Tag != null && control.Tag.ToString().Contains("None"))
            {
                return string.Empty;
            }
            else
            {
                FullKeyboard fullKeyboard = new FullKeyboard("", strDefaultValue);
                var point = control.PointFromScreen(new Point(0, 0));
                double x = SystemParameters.WorkArea.Width;
                double y = SystemParameters.WorkArea.Height;
                if (-point.Y + control.ActualHeight + 5 + fullKeyboard.Height < y)
                {
                    fullKeyboard.Top = -point.Y + control.ActualHeight + 5;
                }
                else
                {
                    fullKeyboard.Top = -point.Y - fullKeyboard.Height - 5;
                }
                if (-point.X + fullKeyboard.Width < x)
                {
                    fullKeyboard.Left = -point.X;
                }
                else
                {
                    fullKeyboard.Left = -point.X - (fullKeyboard.Width - control.ActualWidth);
                }
                if ((bool)fullKeyboard.ShowDialog()) strRet = fullKeyboard.ValueString;
            }
            return strRet;
        }
    }
}