/*************************************************************************************
 *
 * 文 件 名:   SendDataToAgv
 * 描    述:   向Agv发送数据
 * 备    注:
 * 创 建 者：  Du
 * 创建时间：  2022/8/2 9:41:19
*************************************************************************************/

using Newtonsoft.Json;
using RGD.HaiRouAPI.HaiRouModel;
using RGD.HaiRouAPI.HaiRouModel.Inv;
using System;
using System.Collections.Generic;

namespace RGD.HaiRouAPI
{
    public class SendDataToAgv
    {
        private static string agvip = "http://192.168.0.50:9046";

        /// <summary>
        /// 获取机器人状态
        /// </summary>
        /// <param name="robotCode">agv编码</param>
        /// <returns></returns>
        public static robot getRobotStatue(string robotCode)
        {
            var url = agvip + "/robot/query";
            List<string> robotCodeList = new List<string>();
            robotCodeList.Add(robotCode);
            RobotStatue rs = new RobotStatue();
            rs.RobotsCodes = robotCodeList;
            var jsonmodel = JsonConvert.SerializeObject(rs);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "getRobotStatue");
            robot robot = JsonConvert.DeserializeObject<robot>(content);
            return robot;
        }

        /// <summary>
        /// 发送任务到AGV
        /// </summary>
        /// <param name="taskType">任务类型</param>
        /// <param name="startPos">起始位置</param>
        /// <param name="EndPos">终点位置</param>
        /// <param name="TaskIndex">调度任务号</param>
        /// <param name="containerCode">容器编码</param>
        /// <returns></returns>
        public static ApplyResult SendTaskToAgv(string taskType, string startPos, string EndPos, string TaskIndex, string containerCode)
        {
            var url = agvip + "/task/create";
            TaskApply ta = new TaskApply();

            List<HaiRouModel.task> lt = new List<HaiRouModel.task>();
            HaiRouModel.task tasks = new HaiRouModel.task();

            List<taskDescribe> ltd = new List<taskDescribe>();

            tasks.taskCode = TaskIndex;
            if (taskType == "1")
            {
                ta.taskType = "tote_inbound";
                InTaskDescribe itd = new InTaskDescribe();
                itd.containerCode = containerCode != "" ? containerCode : "999999999";
                itd.containerType = "CT_KUBOT_STANDARD";
                itd.fromLocationCode = startPos;
                itd.locationCode = EndPos;

                tasks.taskDescribe = itd;
                lt.Add(tasks);
                ta.tasks = lt;
            }
            else if (taskType == "2")
            {
                ta.taskType = "tote_outbound";
                OutTaskDescribe otd = new OutTaskDescribe();
                otd.containerCode = containerCode != "" ? containerCode : "999999999";
                otd.fromLocationCode = startPos;
                otd.toLocationCode = EndPos;
                ;
                tasks.taskDescribe = otd;
                lt.Add(tasks);
                ta.tasks = lt;
            }
            var jsonmodel = JsonConvert.SerializeObject(ta);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "SendTaskToAgv");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        /// <summary>
        /// 发送任务列表到AGV,只用于出库模式
        /// </summary>
        /// <param name="taskType">任务类型</param>
        /// <param name="startPos">起始位置</param>
        /// <param name="EndPos">终点位置</param>
        /// <param name="TaskIndex">调度任务号</param>
        /// <param name="containerCode">容器编码</param>
        /// <returns></returns>
        public static ApplyResult SendTasksToAgv(List<HaiRouModel.task> tasks)
        {
            var url = agvip + "/task/create";
            TaskApply ta = new TaskApply();
            ta.taskType = "tote_outbound";
            ta.tasks = tasks;

            var jsonmodel = JsonConvert.SerializeObject(ta);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "SendTaskToAgv");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        public static ApplyResult SendTasksToAgv(CreateActionTask tasks)
        {
            var url = "http://192.168.0.50:9000/wms/createActionTask";
            var jsonmodel = JsonConvert.SerializeObject(tasks);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "SendTaskToAgv4Inv");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        /// <summary>
        /// 任务取消
        /// </summary>
        /// <param name="taskIndex">调度任务号</param>
        /// <returns></returns>
        public static ApplyResult SendCancelToAgv(string taskIndex)
        {
            var url = agvip + "/task/cancel";
            TaskCancel tc = new TaskCancel();
            List<string> code = new List<string>();
            code.Add(taskIndex);
            tc.taskCodes = code;
            var jsonmodel = JsonConvert.SerializeObject(tc);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "SendCancelToAgv");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        /*public static ApplyResult TestSendTaskToAgv(string taskType, string startPos, string EndPos, string TaskIndex, string containerCode)
        {
            var url = agvip + "/task/create";
            TaskApply ta = new TaskApply();

            List<task> lt = new List<task>();
            task tasks = new task();

            //List<taskDescribe> ltd = new List<taskDescribe>();

            tasks.taskCode = TaskIndex;
            if (taskType == "1")
            {
                ta.taskType = "tote_inbound";
                InTaskDescribe itd = new InTaskDescribe();
                itd.containerCode = containerCode;
                itd.containerType = "CT_KUBOT_STANDARD";
                itd.fromLocationCode = startPos;
                itd.locationCode = EndPos;
                //ltd.Add(itd);
                tasks.taskDescribe = itd;
                lt.Add(tasks);
                ta.tasks = lt;
            }
            else if (taskType == "2")
            {
                ta.taskType = "tote_outbound";
                OutTaskDescribe otd = new OutTaskDescribe();
                otd.containerCode = containerCode != "" ? containerCode : "999999999";
                otd.fromLocationCode = startPos;
                otd.toLocationCode = EndPos;
                //ltd.Add(otd);
                tasks.taskDescribe = otd;
                lt.Add(tasks);
                ta.tasks = lt;
            }
            var jsonmodel = JsonConvert.SerializeObject(ta);
            var content = HttpServer.sendJsonToService(url, jsonmodel);
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }*/

        public static ContainerArrivedRes TestBoxArrive(string fromcode, string containercode)
        {
            var url = agvip + "/conveyor/containerArrived";
            ContainerArrived ca = new ContainerArrived();
            ca.slotCode = fromcode;
            ca.containerCode = containercode;
            var jsonmodel = JsonConvert.SerializeObject(ca);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "boxArrive");
            return JsonConvert.DeserializeObject<ContainerArrivedRes>(content);
        }

        public static ApplyResult LetGo()
        {
            var url = "http://192.168.0.50:9000/station/letRobotGo";
            LetRobotGo lrg = new LetRobotGo();
            lrg.robotCode = "kubot-1";
            var jsonmodel = JsonConvert.SerializeObject(lrg);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "letGo");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        public static ApplyResult TestSingleLoadTask()
        {
            ApplyResult containercreate = SendDataToAgv.ContainerCrteate("343810010264");
            ApplyResult boxarrive = SendDataToAgv.TestBoxArrive("1-1", "343810010264");
            if (boxarrive.code != "0")
            {
                ApplyResult arr = new ApplyResult();
                arr.code = "200";
                arr.msg = "容器到达接口失败";
                return arr;
            }
            var url = "http://192.168.0.50:9000/wms/createActionTask";
            CreateActionTask cat = new CreateActionTask();
            cat.robotCode = "kubot-1";
            cat.taskGroupCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<HaiRouAPI.HaiRouModel.Inv.task> tasks4Test = new List<HaiRouAPI.HaiRouModel.Inv.task>();
            HaiRouAPI.HaiRouModel.Inv.task task = new HaiRouAPI.HaiRouModel.Inv.task();
            task.taskAction = "load";
            task.taskCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            task.isFinallyPaused = 0;
            task.containerCode = "343810010264";
            task.locationCode = "12004";
            tasks4Test.Add(task);
            cat.tasks = tasks4Test;
            var jsonmodel = JsonConvert.SerializeObject(cat);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "test");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        public static ApplyResult TestSingleUnloadTask()
        {
            var url = "http://192.168.0.50:9000/wms/createActionTask";
            CreateActionTask cat = new CreateActionTask();
            cat.robotCode = "kubot-1";
            cat.taskGroupCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<HaiRouAPI.HaiRouModel.Inv.task> tasks4Test = new List<HaiRouAPI.HaiRouModel.Inv.task>();
            HaiRouAPI.HaiRouModel.Inv.task task = new HaiRouModel.Inv.task();
            task.taskAction = "unload";
            task.taskCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            task.isFinallyPaused = 0;
            task.containerCode = "343810010264";
            task.locationCode = "04-08-01";
            tasks4Test.Add(task);
            cat.tasks = tasks4Test;
            var jsonmodel = JsonConvert.SerializeObject(cat);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "test");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }

        public static ApplyResult ContainerCrteate(string containercode)
        {
            var url = "http://192.168.0.50:9000/container/add";
            ContainerAdds cas = new ContainerAdds();
            List<containerAdds> ca = new List<containerAdds>();
            containerAdds a = new containerAdds();
            a.containerCode = containercode;
            a.containerTypeCode = "CT_KUBOT_STANDARD";
            ca.Add(a);
            cas.containerAdds = ca;
            var jsonmodel = JsonConvert.SerializeObject(cas);
            var content = HttpServer.sendJsonToService(url, jsonmodel, "test");
            return JsonConvert.DeserializeObject<ApplyResult>(content);
        }


    }
}