using Newtonsoft.Json;
using Pms.DataLibrary.Models;
using Pms.DataLibrary.Order;
using Pms.Models;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Common.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Service
{
    public class CallbackService
    {
        public string TaskCancelNotice(object dataObject)
        {
            string dataObjectStr = dataObject.ToString();

            var taskInfo = JsonConvert.DeserializeObject<TaskInfo>(dataObjectStr);
            CLog.Instance.TaskLog.WirteInfoCaller($"{taskInfo.TaskID} 任务取消（API通知）");

            return string.Empty;
        }

        public string SendTransparentData(object dataObject)
        {
            string dataObjectStr = dataObject.ToString();
            var data = JsonConvert.DeserializeObject<RequestTransparentData>(dataObjectStr);

            //string dataHexString = data.Data.ToHexString();
            //CLog.Instance.TaskLog.WirteDebugCaller($"收到透传数据 {dataHexString}");

            CommunicationVcu.Instance.Received(data.Data);
            return string.Empty;
        }

        public string TaskStepStart(object dataObject)
        {
            string dataObjectStr = dataObject.ToString();

            var stepData = JsonConvert.DeserializeObject<StepData>(dataObjectStr);
            CLog.Instance.TaskLog.WirteInfoCaller($"{stepData.TaskID} {stepData.StepID} 任务步骤开始（API通知）");

            if (stepData.StepID == 1)
            {
                var t = PmsTaskService.Instance.GetTaskState(stepData.TaskID);
                if (t != null)
                {
                    t.method = "start";
                    bool result = CustomerApi.AgvCallback(t, out string content);
                    //CLog.Instance.TaskLog.WriteInfo($"{stepData.TaskID} {stepData.StepID} 任务状态上报{result.ToChineseString()}");
                }
            }

            return string.Empty;
        }

        public string TaskStepEnd(object dataObject)
        {
            string dataObjectStr = dataObject.ToString();

            var stepData = JsonConvert.DeserializeObject<StepData>(dataObjectStr);
            CLog.Instance.TaskLog.WirteInfoCaller($"{stepData.TaskID} {stepData.StepID} 任务步骤结束（API通知）");

            return string.Empty;
        }
    }
}
