using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterModuleLibrary
{
    public delegate void EventSetNumKey(object o,string s);
    public partial class ucNumberKey : UserControl
    {
        public ucNumberKey()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 默认控制进行数字验证
        /// </summary>
        public bool bControl = true;

        public string strValue = "";

        /// <summary>
        /// 主界面调用填充数值
        /// </summary>
        public event EventSetNumKey evoice;

        public void setTxtNum(string sValue) 
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                Button btn = new Button();
                btn.Text = sValue;
                setTxtValue(btn, sValue.Length - 1);
            }

        }

        private void btnValue_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            
            if (bControl)
            {
                switch (btn.Name)
                {
                    case "btnSpot":
                        if (txtValue.Text.Trim() != "" && !txtValue.Text.Trim().Contains("."))
                        {
                            setTxtValue(btn, 0);
                        }
                        break;
                    case "btn00":
                        if (txtValue.Text.Trim() != "" && (txtValue.Text.Trim().Substring(0, 1) != "0" || txtValue.Text.Trim().Contains(".")))
                        {
                            setTxtValue(btn, 1);
                        }
                        break;
                    case "btn0":
                        if (txtValue.Text.Trim() == "" || txtValue.Text.Trim().Contains(".") || (txtValue.Text.Trim() != "" && txtValue.Text.Substring(0, 1) != "0"))
                        {
                            setTxtValue(btn, 0);
                        }
                        break;
                    default:
                        setTxtValue(btn, 0);
                        break;
                }
            }
            else
            {
                int m = 0;
                if (btn.Name == "btn00")
                {
                    m = 1;
                }
                setTxtValue(btn, m);
            }
        }

        private void setTxtValue(Button btn, int m)
        {
            int i = txtValue.SelectionStart;
            txtValue.Focus();
            txtValue.Text = txtValue.Text.Trim() + btn.Text;
            txtValue.Select(++i + m, 0);
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            txtValue.Text = "";
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            int i = txtValue.SelectionStart;
            txtValue.Focus();
            if (i > 0)
                txtValue.Select(--i, 0);
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            int i = txtValue.SelectionStart;
            txtValue.Focus();
            if (i < txtValue.Text.Trim().Length)
                txtValue.Select(++i, 0);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            int i = txtValue.SelectionStart;
            txtValue.Focus();
            if (i > 0)
            {
                txtValue.Text = txtValue.Text.Trim().Substring(0, i - 1) + txtValue.Text.Trim().Substring(i);
                txtValue.Select(--i, 0);
            }   
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            strValue = txtValue.Text.Trim();
            txtValue.Text = "";
            evoice(this, strValue);
            //此处还需调用加载该用户控件的界面关闭方法
        }
    }
}
