using Pms.DataLibrary.Order;
using Pms.Models;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Common.WebApi;
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 CurrentTaskUI : UserControl, IDisplayUI
    {
        public CurrentTaskUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            return true;
        }

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

            var taskDataList = PmsApi.GetTaskList();
            if (taskDataList == null)
            {
                dataGridUI1.ShowDataGrid<TaskData>(null);
                dataGridUI2.ShowDataGrid<StepData>(null);
                return;
            }

            foreach (var item in taskDataList)
            {
                item.AllWarehouse = item.AllWarehouse.ToShortPosString();
            }

            dataGridUI1.ShowDataGrid(taskDataList);

            string taskId = dataGridUI1.GetCurrentColValue("TaskID");
            if (string.IsNullOrEmpty(taskId))
            {
                dataGridUI2.ShowDataGrid<StepData>(null);
                return;
            }

            var stepInfoList = PmsApi.GetStepList(taskId);
            if (stepInfoList == null) return;

            dataGridUI2.ShowDataGrid(stepInfoList.OrderBy(d => d.TemplateStepID).ToList());
        }

        private void 取消任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaskOpreate(sender, PmsApi.TaskCancel);
        }

        private void 终止任务不释放AGVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaskOpreate(sender, PmsApi.TaskEnd);
        }

        private void 暂停任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaskOpreate(sender, PmsApi.TaskPause);
        }

        private void 恢复任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaskOpreate(sender, PmsApi.TaskResume);
        }

        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 TaskOpreate(object sender, Predicate<string> predicate)
        {
            string taskId = dataGridUI1.GetCurrentColValue("TaskID");
            if (string.IsNullOrEmpty(taskId)) return;

            string allWarehouse = dataGridUI1.GetCurrentColValue("AllWarehouse");

            string text = $"确定要{sender}{taskId}？（{allWarehouse}）";
            var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dialogResult != DialogResult.OK)
            {
                return;
            }

            bool result = predicate(taskId);
            CLog.Instance.TaskLog.WriteInfo("实时任务界面（手动操作）", $"{taskId}，{sender}{result.ToChineseString()}");
        }

        private void CarrierOpreate(object sender, Predicate<int> predicate)
        {
            string taskId = dataGridUI1.GetCurrentColValue("TaskID");
            if (string.IsNullOrEmpty(taskId)) return;

            string agvIdString = dataGridUI1.GetCurrentColValue("Carrier");
            if (!int.TryParse(agvIdString, out int agvId)) return;

            if (agvId <= 0) return;

            bool result = predicate(agvId);
            CLog.Instance.TaskLog.WriteInfo("实时任务界面（手动操作）", $"{taskId}，{agvId}#，{sender}{result.ToChineseString()}");
        }


    }
}
