using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Device.CommandCallback;
using ProjectManagementSystem.Device.Extenions;
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 DebugRailAgvUI : UserControl, IDisplayUI
    {
        public DebugRailAgvUI()
        {
            InitializeComponent();
        }

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

            this.comboBoxStatus.ValueMember = "Value";
            this.comboBoxStatus.DisplayMember = "Display";
            this.comboBoxStatus.DataSource = EnumInfoAgvStatus.None.EnumToDataTable("Value", "Display");
            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");
            comboBoxStatus.SelectedValue = int.Parse(dataGridUI1.GetCurrentColValue("Status"));
        }

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

            var dataList = DeviceControl.Instance.Communication.InfoAgvDictionary.Values.OrderBy(d => d.AgvId).ToList();
            dataGridUI1.ShowDataGrid(dataList);
        }

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

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

        private void RailAgvOpereate(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);
            }
        }

        private void RailAgvSimulate(EnumInfoAgvStatus status)
        {
            try
            {
                int mark = int.Parse(textBox1.Text);
                int route = int.Parse(textBox2.Text);
                int agvId = int.Parse(textBox3.Text);

                comboBoxStatus.SelectedValue = status;

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

        private void RailAgvSimulate(int agvId, int mark, int route, EnumInfoAgvStatus status)
        {
            try
            {
                bool isContainsKey = DeviceControl.Instance.Communication.InfoAgvDictionary.ContainsKey(agvId);
                var info = isContainsKey ? DeviceControl.Instance.Communication.InfoAgvDictionary[agvId] : new InfoAgv();
                info.AgvId = agvId;
                info.Mark = mark;
                info.Route = route;
                info.Status = (int)status;
                info.StatusText = info.Status.GetAgvStatusText();
                info.LastUpdateTime = DateTime.Now;

                DeviceControl.Instance.Communication.InfoAgvDictionary.AddOrUpdate(info.AgvId, info, (key, values) => info);
            }
            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)
        {
            RailAgvSimulate((EnumInfoAgvStatus)comboBoxStatus.SelectedValue);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            RailAgvSimulate(EnumInfoAgvStatus.停止点);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            RailAgvSimulate(EnumInfoAgvStatus.结束点);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            RailAgvSimulate(EnumInfoAgvStatus.运行);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                RailAgvSimulate(i, i, i, (EnumInfoAgvStatus)comboBoxStatus.SelectedValue);
            }
        }
    }
}
