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 partial class ucNumberKey2 : UserControl
    {
        public ucNumberKey2()
        {
            InitializeComponent();
        }

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

        public string strValue = "";

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

        private void btnValue_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int m = 0;
            if (txtValue1.Text.Length < 3)
            {
                iFocus = 1;
                setTxtValue1(btn, m);
            }
            else if (txtValue2.Text.Length < 3)
            {
                iFocus = 2;
                setTxtValue2(btn, m);
            }
            else if (txtValue3.Text.Length < 3) 
            {
                iFocus = 3;
                setTxtValue3(btn, m);
            }
        }

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

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

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

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

        int iFocus = 1;
        private void btnLeft_Click(object sender, EventArgs e)
        {
            switch (iFocus) 
            {
                case 1:
                    txtValue1.Focus(); 
                    break;
                case 2:
                    txtValue2.Focus();
                    break;
                case 3:
                    txtValue3.Focus();
                    break;
            }

            if (txtValue3.Focused && txtValue3.Text != "")
            {
                int i = txtValue3.SelectionStart;
                if (i > 0)
                {
                    iFocus = 3;
                    txtValue3.Focus();
                    txtValue3.Select(--i, 0);
                }
                else
                {
                    iFocus = 2;
                    txtValue2.Focus();
                    txtValue2.Select(3, 0);
                }
            }
            else if (txtValue2.Focused && txtValue2.Text != "")
            {
                int i = txtValue2.SelectionStart;
                if (i > 0)
                {
                    iFocus = 2;
                    txtValue2.Focus();
                    txtValue2.Select(--i, 0);
                }
                else
                {
                    iFocus = 1;
                    txtValue1.Focus();
                    txtValue1.Select(3, 0);
                }
            }
            else if (txtValue1.Focused && txtValue1.Text != "") 
            {
                iFocus = 1;
                int i = txtValue1.SelectionStart;
                txtValue1.Focus();
                if (i > 0)
                    txtValue1.Select(--i, 0);
            }
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            switch (iFocus)
            {
                case 1:
                    txtValue1.Focus();
                    break;
                case 2:
                    txtValue2.Focus();
                    break;
                case 3:
                    txtValue3.Focus();
                    break;
            }

            if (txtValue1.Focused && txtValue1.Text != "")
            {
                int i = txtValue1.SelectionStart;
                txtValue1.Focus();
                if (i < txtValue1.Text.Trim().Length)
                {
                    txtValue1.Select(++i, 0);
                }
                else
                {
                    iFocus = 2;
                    txtValue2.Focus();
                }
            }
            else if (txtValue2.Focused && txtValue2.Text != "")
            {
                int i = txtValue2.SelectionStart;
                txtValue2.Focus();
                if (i < txtValue2.Text.Trim().Length)
                {
                    txtValue2.Select(++i, 0);
                }
                else
                {
                    iFocus = 3;
                    txtValue3.Focus();
                }
            }
            else if (txtValue3.Focused && txtValue3.Text != "")
            {
                int i = txtValue3.SelectionStart;
                iFocus = 3;
                txtValue3.Focus();
                if (i < txtValue3.Text.Trim().Length)
                {
                    txtValue3.Select(++i, 0);
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (txtValue3.Text != "")
            {
                iFocus = 3;
                int i = txtValue3.SelectionStart;
                txtValue3.Focus();
                if (i > 0)
                {
                    txtValue3.Text = txtValue3.Text.Trim().Substring(0, i - 1) + txtValue3.Text.Trim().Substring(i);
                    txtValue3.Select(--i, 0);
                }
            }
            else if (txtValue2.Text != "")
            {
                iFocus = 2;
                int i = txtValue2.SelectionStart;
                txtValue2.Focus();
                if (i > 0)
                {
                    txtValue2.Text = txtValue2.Text.Trim().Substring(0, i - 1) + txtValue2.Text.Trim().Substring(i);
                    txtValue2.Select(--i, 0);
                }
            }
            else if (txtValue1.Text != "")
            {
                iFocus = 1;
                int i = txtValue1.SelectionStart;
                txtValue1.Focus();
                if (i > 0)
                {
                    txtValue1.Text = txtValue1.Text.Trim().Substring(0, i - 1) + txtValue1.Text.Trim().Substring(i);
                    txtValue1.Select(--i, 0);
                }
            }
        }

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