using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;
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 ProjectManagementSystem.UI
{
    public partial class DebugRollerUI : UserControl,IDisplayUI
    {
        public DebugRollerUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            dataGridUI1.dataGridView1.CellClick += DataGridView1_CellClick;
            return true;
        }

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridUI1.GetCurrentColValue("DeviceId");

        }

        public void UpdateDisplay()
        {
            if (!this.Visible) return;

            var dataList = DeviceControl.Instance.Communication.InfoRollerDictionary.Values.ToList();
            dataGridUI1.ShowDataGrid(dataList);
        }



        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int deviceId = int.Parse(textBox1.Text);
                //if (!DeviceControl.Instance.Communication.InfoRollerDictionary.ContainsKey(deviceId))
                //{
                //    MessageBox.Show($"当前没有设备ID{deviceId}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    return;
                //}
                button2_Click(null, null);
                ushort code = ushort.Parse(comboBox2.Text);

                DeviceControl.Instance.RollerOperate(deviceId, code);
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteException("UI", ex);
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int deviceId = int.Parse(textBox1.Text);
            //if (!DeviceControl.Instance.Communication.InfoRollerDictionary.ContainsKey(deviceId))
            //{
            //    MessageBox.Show($"当前没有设备ID{deviceId}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

            DeviceControl.Instance.RollerOperateClear(deviceId);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ushort newOutput = 0;
            foreach (var item in panel2.Controls)
            {
                var checkBox = item as CheckBox;
                if (checkBox != null)
                {
                    int index = int.Parse(checkBox.Text.Substring(7));
                    if (checkBox.Checked)
                    {
                        newOutput = (ushort)(newOutput | (0x01 << index));
                    }
                    else
                    {
                        newOutput = (ushort)(newOutput & (ushort.MaxValue - (1 << index)));
                    }
                }
            }

            comboBox2.Text = newOutput.ToString();
        }
    }
}
