using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotNettyFrom.config;
using DotNettyFrom.db;

namespace DotNettyFrom.dialog
{
    public delegate void OnHistoryResult(string type, string result);

    public partial class HistoryDialog : Form
    {
        private int _type;
        private string _tableName;
        public event OnHistoryResult HistoryHandler;

        private int _SelectIndex = -1;

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

        private void HistoryDialog_Load(object sender, EventArgs e)
        {
            switch (_type)
            {
                case SystemConfig.IpHisType:
                    IpType();
                    break;
                case SystemConfig.AddrHisType:
                    AddrType();
                    break;
                case SystemConfig.CcidHisType:
                    CcidType();
                    break;
                case SystemConfig.SendHisType:
                    SendType();
                    break;
                default:
                    this.Close();
                    break;
            }

            DataTable dt = DbHelper.GetInstance().SelectHistory(@"select * from " + _tableName + " order by ID desc");
            DataGridViewHis.DataSource = dt;
        }

        private void SendType()
        {
            _tableName = DBConfig.SendHis;
            Text = "发送数据 - 历史记录";
            LbCCIDTran.Visible = false;
            TbCCid.Visible = false;
            BtnTran.Visible = false;
            BtnRandom.Visible = false;
            LbTagName.Visible = false;
            NumCount.Visible = false;
        }

        private void CcidType()
        {
            _tableName = DBConfig.CcidHis;
            Text = "CCID - 历史记录";
            LbTagName.Text = "CCID长度:";
            NumCount.Value = 40;
        }

        private void AddrType()
        {
            _tableName = DBConfig.AddrHis;
            Text = "设备地址 - 历史记录";
            LbTagName.Text = "设备地址长度:";
            NumCount.Value = 12;

            LbCCIDTran.Visible = false;
            TbCCid.Visible = false;
            BtnTran.Visible = false;
        }

        private void IpType()
        {
            _tableName = DBConfig.IpHis;
            Text = "IP - 历史记录";

            LbCCIDTran.Visible = false;
            TbCCid.Visible = false;
            BtnTran.Visible = false;
            BtnRandom.Visible = false;
            LbTagName.Visible = false;
            NumCount.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
//            DbHelper.GetInstance().InsertHistory(_type, "aaaaaaaaaaaaaaa");
//            DbHelper.GetInstance().DeleteHistoryById(_type, 26);
//            DbHelper.GetInstance().DeleteAllHistory(_type);
        }

        private void DataGridViewHis_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
        }

        private void DataGridViewHis_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _SelectIndex = e.RowIndex;
            }
        }

        private void MenuItem_delete_line_Click(object sender, EventArgs e)
        {
            int row = DataGridViewHis.RowCount;
            if (_SelectIndex >= 0 && _SelectIndex < row)
            {
                object value = DataGridViewHis[0, _SelectIndex].Value;
                DbHelper.GetInstance().DeleteHistoryById(_type, int.Parse(value.ToString()));
                DataTable dt = DbHelper.GetInstance().SelectHistory(@"select * from " + _tableName + " order by ID desc");
                DataGridViewHis.DataSource = dt;
            }
        }
    }
}