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

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

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

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

            var dataList = PmsApi.GetAllCarrierProp();
            dataGridUI1.ShowDataGrid(dataList);
        }

        private void 暂停小车ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CarrierOpreate(sender, PmsApi.CarrierPause);
        }

        private void 恢复小车ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CarrierOpreate(sender, PmsApi.CarrierResume);
        }

        private void CarrierOpreate(object sender, Predicate<int> predicate)
        {
            string agvIdString = dataGridUI1.GetCurrentColValue("AgvID");
            if (!int.TryParse(agvIdString, out int agvId)) return;

            if (agvId <= 0) return;

            bool result = predicate(agvId);

            string text = $"{agvId}#，{sender}{result.ToChineseString()}";
            MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            CLog.Instance.TaskLog.WriteInfo("AGV信息界面（手动操作）", text);
        }
    }
}
