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 DebugReleaseBoxUI : UserControl,IDisplayUI
    {
        public DebugReleaseBoxUI()
        {
            InitializeComponent();
        }

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

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridUI1.GetCurrentColValue("Mark");
            textBox2.Text = dataGridUI1.GetCurrentColValue("Route");
            textBox3.Text = dataGridUI1.GetCurrentColValue("AgvId");
        }

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

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



        private void button1_Click(object sender, EventArgs e)
        {
            CallBoxOpereate(DeviceControl.Instance.ReleaseBoxLightOn);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CallBoxOpereate(DeviceControl.Instance.ReleaseBoxLightOff);
        }

        private void CallBoxOpereate(Action<int, int, int> action)
        {
            try
            {
                int mark = int.Parse(textBox1.Text);
                int route = int.Parse(textBox2.Text);
                int agvId = int.Parse(textBox3.Text);

                action(mark, route, agvId);
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteException("UI", ex);
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
