using GWSocketClient.db;
using GWSocketClient.model;
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 GWSocketClient.util;

namespace GWSocketClient
{
    public delegate void OnHandlerResult(string topmost);

    public partial class IdItemEditor : Form
    {
        private WvBean wbBean = null;
        private bool addOrUpdate = true;

        public event OnHandlerResult HandlerResult;

        public IdItemEditor(WvBean wb = null)
        {
            InitializeComponent();
            this.wbBean = wb;
        }

        private void IdItemEditor_Load(object sender, EventArgs e)
        {
            if (wbBean != null)
            {
                this.Text = "修改";
                buttonSubmit.Text = "修改";
                addOrUpdate = false;

                addModifyText();
            }
            else
            {
                this.Text = "添加";
                buttonSubmit.Text = "添加";
                addOrUpdate = true;
            }

            this.tbDv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.hex_Valid_KeyPress);
            this.tbUv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.hex_Valid_KeyPress);
        }

        private void addModifyText()
        {
            labelId.Text = wbBean.id + "";
            tbIndex.Text = wbBean.index + "";
            tbId.Text = wbBean.myId;
            tbInfo.Text = wbBean.idInfo;
            tbLen.Text = wbBean.len;

            tbDesp.Text = wbBean.desp;
            tbDv.Text = wbBean.defalutValue;
            tbUv.Text = wbBean.userValue;
            tbRemark.Text = wbBean.remark;

            string mode = wbBean.mode.ToUpper();

            if (mode.Contains("W"))
            {
                checkBoxWrite.Checked = true;
            }
            if (mode.Contains("R"))
            {
                checkBoxRead.Checked = true;
            }


            if (!tbLen.Text.Trim().Equals("-1"))
            {
                labelDefaultlen.Text = tbDv.Text.Trim().Length / 2.0 + " / " + tbLen.Text.Trim();
            }
            else
            {
                labelDefaultlen.Text = tbDv.Text.Trim().Length / 2.0 + "";
            }

            if (!tbLen.Text.Trim().Equals("-1"))
            {
                labelUserLen.Text = tbUv.Text.Trim().Length / 2.0 + " / " + tbLen.Text.Trim();
            }
            else
            {
                labelUserLen.Text = tbUv.Text.Trim().Length / 2.0 + "";
            }
        }


        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            string tbIndexString = tbIndex.Text.Trim();
            string myIdString = tbId.Text.Trim();
            string lenString = tbLen.Text.Trim();
            string defalutValueString = tbDv.Text.Trim();
            string userValueString = tbUv.Text.Trim();
            bool modeBool = checkBoxWrite.Checked || checkBoxRead.Checked;

            #region ISNULL

            if (tbIndexString.Equals(""))
            {
                richTextBoxTip.Text = "Index不能为空!";
                return;
            }

            if (myIdString.Equals(""))
            {
                richTextBoxTip.Text = "ID 16进制不能为空!";
                return;
            }
            if (lenString.Equals(""))
            {
                richTextBoxTip.Text = "长度不能为空!";

                return;
            }
            if (!modeBool)
            {
                richTextBoxTip.Text = "读取不能为空!";
                return;
            }
            if (defalutValueString.Equals(""))
            {
                richTextBoxTip.Text = "默认值不能为空!";
                return;
            }

            #endregion


            if (userValueString.Equals(""))
            {
                userValueString = defalutValueString;
            }


            if (lenString.Contains("-"))
            {
                if (defalutValueString.Length % 2 == 0)
                {
                    richTextBoxTip.Text = "默认值长度不是整数!!!defalutValueString.Length=" + (defalutValueString.Length / 2.0);
                    return;
                }
                if (userValueString.Length % 2 == 0)
                {
                    richTextBoxTip.Text = "用户值长度不是整数!!!defalutValueString.Length=" + (userValueString.Length / 2.0);
                    return;
                }
            }
            else
            {
                int len = int.Parse(lenString);
                
                if ((defalutValueString.Length / 2) != len)
                {
                    richTextBoxTip.Text = "默认值长度不对.定义长度是=" + len + " ,你输入长度是=" +
                                          (defalutValueString.Length / 2);
                    return;
                }
                if ((userValueString.Length / 2) != len)
                {
                    richTextBoxTip.Text = "用户值长度不对.定义长度是=" + len + " ,你输入长度是=" + (userValueString.Length / 2.0);
                    return;
                }
            }

            WvBean wb = new WvBean();
            wb.index = int.Parse(tbIndexString);
            wb.myId = myIdString;
            wb.idInfo = tbInfo.Text.Trim();
            wb.len = lenString;

            string modeString = checkBoxRead.Checked ? "R" : "";
            modeString += checkBoxWrite.Checked ? "W" : "";

            wb.mode = modeString;
            wb.desp = tbDesp.Text.Trim();
            wb.defalutValue = defalutValueString;
            wb.userValue = userValueString;
            wb.remark = tbRemark.Text.Trim();
            int res = -1;
            if (addOrUpdate)
            {
                //添加
                try
                {
                    res = AccessDbLoader.getInstance().insert2Db(wb);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    richTextBoxTip.Text = "异常:" + exception.Message;
                }
            }
            else
            {
                //更新
                wb.id = int.Parse(labelId.Text.Trim());
                try
                {
                    res = AccessDbLoader.getInstance().update2Db(wb);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    richTextBoxTip.Text = "异常:" + exception.Message;
                }
            }
            if (res > 0)
            {
                HandlerResult("成功"); //执行委托实例  
                this.Close();
            }
        }


        private void buttonCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tbUv_TextChanged(object sender, EventArgs e)
        {
            if (!tbLen.Text.Trim().Equals("-1"))
            {
                labelUserLen.Text = tbUv.Text.Trim().Length / 2.0 + " / " + tbLen.Text.Trim();
            }
            else
            {
                labelUserLen.Text = tbUv.Text.Trim().Length / 2.0 + "";
            }
        }

        private void tbDv_TextChanged(object sender, EventArgs e)
        {
            if (!tbLen.Text.Trim().Equals("-1"))
            {
                labelDefaultlen.Text = tbDv.Text.Trim().Length / 2.0 + " / " + tbLen.Text.Trim();
            }
            else
            {
                labelDefaultlen.Text = tbDv.Text.Trim().Length / 2.0 + "";
            }
        }

        private void tbIndex_TextChanged(object sender, EventArgs e)
        {
            string input = tbIndex.Text.Trim();
            if (!input.Equals(""))
            {
                tbId.Text = Utils.stringWith0(Utils.tenToHex(Utils.validInput(input)), 4);
            }
            else
            {
                tbId.Text = "";
            }
        }

        private void tbIndex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }


        private void hex_Valid_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = (int) (e.KeyChar);

            if (ascii != 8 && !(ascii >= 48 && ascii <= 57) && !(ascii >= 65 && ascii <= 70) &&
                !(ascii >= 97 && ascii <= 102))
            {
                e.Handled = true;
            }
        }

        private void tbLen_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = (int) (e.KeyChar);

            if (ascii != 8 && !(ascii >= 48 && ascii <= 57) && ascii != 45)
            {
                e.Handled = true;
            }
        }
    }
}