using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Sunny.UI.UITextBox;

namespace PlcUiControl
{
    public partial class FormInput : UIForm
    {
        public static bool GetInputValue<T>(string currentValue, out T inputValue, T maximum = default, T minimum = default)
        {
            if (typeof(T) != typeof(int) &&
                typeof(T) != typeof(double) &&
                typeof(T) != typeof(string))
            {
                inputValue = default;
                return false;
            }
            if (typeof(T) == typeof(string))
            {
                var formInput = new FormInput(currentValue);
                formInput.ShowDialog();
                if (formInput.DialogResult == DialogResult.OK)
                {
                    inputValue = (T)formInput.InputValue;
                    return true;
                }
                else
                {
                    inputValue = default;
                    return false;
                }
            }
            else if (typeof(T) == typeof(int))
            {
                var formInput = new FormInput(Convert.ToInt32(maximum), Convert.ToInt32(minimum), currentValue);
                formInput.ShowDialog();
                if (formInput.DialogResult == DialogResult.OK)
                {
                    inputValue = (T)formInput.InputValue;
                    return true;
                }
                else
                {
                    inputValue = default;
                    return false;
                }
            }
            else if (typeof(T) == typeof(double))
            {
                var formInput = new FormInput(Convert.ToDouble(maximum), Convert.ToDouble(minimum), currentValue);
                formInput.ShowDialog();
                if (formInput.DialogResult == DialogResult.OK)
                {
                    inputValue = (T)formInput.InputValue;
                    return true;
                }
                else
                {
                    inputValue = default;
                    return false;
                }
            }
            else
            {
                inputValue = default;
                return false;
            }
        }
        //public double Maximum { private get => txtInput.Maximum; set => txtInput.Maximum = value; }
        //public double Minimum { private get=> txtInput.Minimum; set => txtInput.Minimum = value; }

        public double Maximum { get; set; }// { private get => txtInput.Maximum; set => txtInput.Maximum = value; }
        public double Minimum { get; set; }// { private get => txtInput.Minimum; set => txtInput.Minimum = value; }
        public UIEditType InputType { get=>txtInput.Type; set=>txtInput.Type=value; }

        public object InputValue;
        private FormInput()
        {
            InitializeComponent();
        }
        public FormInput(string currentValue) : this()
        {
            InputType = UIEditType.String;
            txtInput.Text = currentValue;
        }
        public FormInput(int maximum, int minimum, string currentValue) : this()
        {
            Maximum = maximum; Minimum = minimum;
            InputType = UIEditType.Integer;
            txtInput.MaxLength = 10;
            //txtInput.DecimalPlaces = 0;
            txtInput.Text = currentValue;
        }
        public FormInput(double maximum, double minimum, string currentValue) : this()
        {
            Maximum = maximum; Minimum = minimum;
            InputType = UIEditType.Double;
            txtInput.DecimalPlaces = 6;
            txtInput.Text = currentValue;
        }

        private void btnEnsure_Click(object sender, EventArgs e)
        {
            if (InputType == UIEditType.Integer || InputType == UIEditType.Double)
            {
                double value = Convert.ToDouble(txtInput.Text);
                if (value > Maximum || value < Minimum)
                {
                    MessageBox.Show("数据超范围", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            switch (InputType)
            {
                case UIEditType.String:
                    InputValue = txtInput.Text;
                    break;
                case UIEditType.Integer:

                    InputValue = txtInput.IntValue;
                    break;
                case UIEditType.Double:
                    InputValue = txtInput.DoubleValue;
                    break;
                default:
                    break;
            }
            DialogResult = DialogResult.OK;
            Close();
        }
        private void FormInput_Shown(object sender, EventArgs e)
        {
            lblInputRange.Text = $"({Minimum.ToString()}~{Maximum.ToString()})";
            txtInput.SelectAll();
        }
    }
}
