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

namespace GWSocketClient
{
    public delegate void OnHistoryResult(string type, string result);

    public partial class HistoryDialog : Form
    {
        public event OnHistoryResult HistoryHandler;
        private string type;
        private string tableName;
        private List<HisBean> his = new List<HisBean>();

        public HistoryDialog(string type)
        {
            InitializeComponent();
            this.type = type;
        }


        private void HistoryDialog_Load(object sender, EventArgs e)
        {
            if ("labelCCIDHIS".Equals(type))
            {
                textBoxSourceCCID.Visible = true;
                labelCCIDLEN.Visible = true;
                buttonTransalteCCID.Visible = true;
                buttonRandom.Visible = true;
                this.Text = "CCID - 历史记录";
                his = AccessDbLoader.getInstance().getInputHistory(AccessDbLoader.CCID_HIS);
                tableName = AccessDbLoader.CCID_HIS;
            }
            else if ("labelAddHis".Equals(type))
            {
                buttonRandom.Visible = true;
                this.Text = "设备地址 - 历史记录";
                his = AccessDbLoader.getInstance().getInputHistory(AccessDbLoader.ADDR_HIS);
                tableName = AccessDbLoader.ADDR_HIS;
            }
            else if ("labelIPHIS".Equals(type))
            {
                this.Text = "IP - 历史记录";
                his = AccessDbLoader.getInstance().getInputHistory(AccessDbLoader.IP_HIS);
                tableName = AccessDbLoader.IP_HIS;
            }
            else if ("labelSendHIS".Equals(type))
            {
                this.Text = "发送指令 - 历史记录";
                his = AccessDbLoader.getInstance().getInputHistory(AccessDbLoader.SEND_HIS);
                tableName = AccessDbLoader.SEND_HIS;
            }

            listBoxHistory.Items.AddRange(his.ToArray());
        }

        private void buttonRandom_Click(object sender, EventArgs e)
        {
            if ("labelCCIDHIS".Equals(type))
            {
                HistoryHandler(type, randomCCID());
                this.Close();
            }
            else if ("labelAddHis".Equals(type))
            {
                string add = "00000";
                Random rd = new Random();
                for (int i = 0; i < 7; i++)
                {
                    add += Utils.tenToHex(rd.Next(0, 16));
                }
                HistoryHandler(type, add);
                this.Close();
            }
        }

        private void textBoxSourceCCID_TextChanged(object sender, EventArgs e)
        {
            string tt = textBoxSourceCCID.Text.Trim();
            if (tt.Length > 0)
            {
                labelCCIDLEN.Text = tt.Length + "";
            }
            else
            {
                labelCCIDLEN.Text = "0";
            }
        }

        private void buttonTransalteCCID_Click(object sender, EventArgs e)
        {
            string result = "";
            string tt = textBoxSourceCCID.Text.Trim();
            if (tt.Length > 0)
            {
                try
                {
                    result = Utils.transateCCID(tt);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.DialogResult = DialogResult.None;
                    return;
                }
            }
            else
            {
                result = randomCCID();
            }
            HistoryHandler(this.type, result);
        }

        private string randomCCID()
        {
            string res = "";
            Random rd = new Random();

            for (int i = 0; i < 20; i++)
            {
                res += rd.Next(30, 40);
            }

            return res;
        }

        private void listBoxHistory_DoubleClick(object sender, EventArgs e)
        {
            int index = listBoxHistory.SelectedIndex;
            if (index >= 0 && index < listBoxHistory.Items.Count)
            {
                string info = listBoxHistory.SelectedItem.ToString();
                HistoryHandler(type, info);
                this.Close();
            }
        }

        //右键   
        private void DelteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBoxHistory.SelectedIndex;
            if (index >= 0 && index < listBoxHistory.Items.Count)
            {
                HisBean hb = listBoxHistory.SelectedItem as HisBean;
                AccessDbLoader.getInstance().deleteByID(tableName, hb.ID);
                his.Clear();
                his = AccessDbLoader.getInstance().getInputHistory(AccessDbLoader.CCID_HIS);
                listBoxHistory.Items.Clear();
                listBoxHistory.Items.AddRange(his.ToArray());
            }
        }

        private void AllClearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AccessDbLoader.getInstance().deleteAllData(tableName);
            listBoxHistory.Items.Clear();
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBoxHistory.SelectedIndex;
            if (index >= 0 && index < listBoxHistory.Items.Count)
            {
                HisBean hb = listBoxHistory.SelectedItem as HisBean;
                Clipboard.SetDataObject(hb.data);
            }
        }
    }
}