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 GatewayTool.Models;

namespace GatewayTool.UserControls
{
    public partial class UcDeviceList : UserControl
    {
        public event EventHandler<ListViewItem> ExecItemClicked;

        /// <summary>
        /// 模块名称（标题）
        /// </summary>
        public string ModuleName
        {
            get => lbDeviceList.Text;
            set => lbDeviceList.Text = value;
        }

        /// <summary>
        /// 序号
        /// </summary>
        public string ID
        {
            get => this.listView1.Columns[0].Text;
            set => this.listView1.Columns[0].Text = value;
        }

        /// <summary>
        /// 设备编号
        /// </summary>
        public string SN
        {
            get => this.listView1.Columns[1].Text;
            set => this.listView1.Columns[1].Text = value;
        }

        /// <summary>
        /// 数据来源
        /// </summary>
        public string Source
        {
            get => this.listView1.Columns[2].Text;
            set => this.listView1.Columns[2].Text = value;
        }

        /// <summary>
        /// 固件版本
        /// </summary>
        public string FirmwareVersion
        {
            get => this.listView1.Columns[3].Text;
            set => this.listView1.Columns[3].Text = value;
        }

        //自动调整ListView的列宽的方法
        public void AutoResizeColumnWidth(ListView lv)
        {
            int count = lv.Columns.Count;
            int MaxWidth = 0;
            Graphics graphics = lv.CreateGraphics();
            int width;
            lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            for (int i = 0; i < count; i++)
            {
                string str = lv.Columns[i].Text;
                MaxWidth = lv.Columns[i].Width;

                foreach (ListViewItem item in lv.Items)
                {
                    str = item.SubItems[i].Text;
                    width = (int)graphics.MeasureString(str, lv.Font).Width;
                    if (width > MaxWidth)
                    {
                        MaxWidth = width;
                    }
                }
            }
        }

        public UcDeviceList()
        {
            InitializeComponent();
            InitNetDeviceListUI();
        }

        /// <summary>
        /// 初始化设备列表UI
        /// </summary>
        private void InitNetDeviceListUI()
        {
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "序号";
            ch.Width = 40;
            ch.TextAlign = HorizontalAlignment.Center;
            this.listView1.Columns.Add(ch);

            ColumnHeader ch2 = new ColumnHeader();
            ch2.Text = "设备编号";
            ch2.Width = 80;
            ch2.TextAlign = HorizontalAlignment.Center;
            this.listView1.Columns.Add(ch2);

            ColumnHeader ch3 = new ColumnHeader();
            ch3.Text = "数据来源";
            ch3.Width = 100;
            ch3.TextAlign = HorizontalAlignment.Center;
            this.listView1.Columns.Add(ch3);

            ColumnHeader ch4 = new ColumnHeader();
            ch4.Text = "固件版本";
            ch4.Width = 126;
            ch4.TextAlign = HorizontalAlignment.Center;
            this.listView1.Columns.Add(ch4);
        }

        /// <summary>
        /// 更新设备列表UI
        /// </summary>
        /// <param name="deviceList">设备列表</param>
        public void UpdateDeviceListUI(List<GatewayBase> deviceList)
        {
            this.listView1.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度
            GatewayBase device = null;
            int count = deviceList.Count;
            this.listView1.Items.Clear();
            for (int i = 0; i < count; i++)   //添加数据  
            {
                device = deviceList[i];
                ListViewItem lv = new ListViewItem();
                lv.Text = (i + 1).ToString();
                lv.SubItems.Add(device.SniffInfo.SN);
                lv.SubItems.Add(device.SniffInfo.Source);
                lv.SubItems.Add(device.SniffInfo.Version);
                this.listView1.Items.Add(lv);
            }
            this.listView1.EndUpdate();  //结束数据处理，UI界面一次性绘制。 
        }

        private void listView1_SizeChanged(object sender, EventArgs e)
        {
            //AutoResizeColumnWidth(this.listView1);
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            ExecItemClicked?.Invoke(this, listView1.SelectedItems[0]); //输出选中条目
        }
    }
}