using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.WebApi;
using WCS.WorkEngineering.WebApi.Models.AGV;
using WCS.WorkEngineering.WebApi.Models.AGV.Request;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;
using WCS.WorkEngineering.WebApi.Models.WMS.Request;
using WCS.WorkEngineering.WebApi.Models.WMS.Response;

namespace WCS.WorkEngineering.WebApi.Controllers
{
    /// <summary>
    ///  AGV接口
    /// </summary>
    public static class AgvApi
    {
        private static string _AgvUrl = null!;

        /// <summary>
        /// AGV地址
        /// </summary>
        public static string AgvUrl
        {
            get
            {
                _AgvUrl ??= RedisHub.Default.Check("AgvUrl");
                if (string.IsNullOrEmpty(_AgvUrl))
                {
                    throw new KnownException($"请在Redis配置AgvUrl", LogLevelEnum.High);
                }
                return _AgvUrl;
            }
        }

        #region 任务单生成

        /// <summary>
        ///  满轮入库
        /// </summary>
        /// <param name="ctnrCode">RFID</param>
        /// <param name="position">取货机台</param>
        /// <param name="taskCode">WMS任务号</param>
        /// <param name="priority">优先级</param>
        /// <returns></returns>
        public static GenAgvSchedulingTaskResponse 满轮入库(string ctnrCode, string position, string taskCode, string priority)
        {
            if (position == "1017")
            {
                position = "40";
            }
            return GenAgvSchedulingTask("iwms_third", ctnrCode, "4", new List<positionCodeClass>()
            {
                new positionCodeClass(){ //取货机台
                    positionCode=position,
                    type="00"
                },
                new positionCodeClass(){ //巷道分配点
                    positionCode="HJLK",
                    type="00"
                },
                new positionCodeClass(){ //预分配放货点
                    positionCode="1015",
                    type="00"
                }
            }, priority, taskCode, "ZTGT03", "1");
        }

        ///// <summary>
        /////  一楼满轮出库
        ///// </summary>
        ///// <param name="ctnrCode">RFID</param>
        ///// <param name="position">取货机台</param>
        ///// <param name="taskCode">WMS任务号</param>
        ///// <param name="priority">优先级</param>
        ///// <returns></returns>
        //public static GenAgvSchedulingTaskResponse 一楼满轮入库(string ctnrCode, string position, string taskCode, string priority)
        //{
        //    return GenAgvSchedulingTask("iwms_third", ctnrCode, "4", new List<positionCodeClass>()
        //    {
        //        new positionCodeClass(){ //取货机台
        //            positionCode=position,
        //            type="00"
        //        },
        //        new positionCodeClass(){ //巷道分配点
        //            positionCode="HJLK",
        //            type="00"
        //        },
        //        new positionCodeClass(){ //预分配放货点
        //            positionCode="1015",
        //            type="00"
        //        }
        //    }, priority, taskCode, "ZTGT03", "1");
        //}

        /// <summary>
        ///  机台补空
        /// </summary>
        /// <param name="position">补空机台</param>
        /// <param name="taskCode">WMS任务号</param>
        /// <param name="priority">优先级</param>
        /// <returns></returns>
        public static GenAgvSchedulingTaskResponse 机台补空(string position, string taskCode, string priority)
        {
            return GenAgvSchedulingTask("iWMS", "", "4", new List<positionCodeClass>()
            {
                new positionCodeClass(){ //合金库空区域
                    positionCode="21",
                    type="04"
                },
                new positionCodeClass(){ //目标机台
                    positionCode=position,
                    type="00"
                }
            }, priority, taskCode, "ZTGT03", "0");
        }

        /// <summary>
        ///  缓存架补空
        /// </summary>
        /// <param name="taskCode">任务号</param>
        /// <param name="nextPositionCode">取货地址</param>
        /// <returns></returns>
        public static ContinueTaskResponse 缓存架补空(string taskCode, string nextPositionCode)
        {
            return ContinueTask(taskCode, nextPositionCode);
        }

        /// <summary>
        ///  一楼满轮出库
        /// </summary>
        /// <param name="ctnrCode">RFID</param>
        /// <param name="position1">取货站台</param>
        /// <param name="position2">放货机台</param>
        /// <param name="taskCode">WMS任务号</param>
        /// <param name="priority">优先级</param>
        /// <returns></returns>
        public static GenAgvSchedulingTaskResponse 一楼满轮出库(string ctnrCode, string position1, string position2, string taskCode, string priority)
        {
            if (position1 == "1018")
            {
                position1 = "41";
            }
            return GenAgvSchedulingTask("iWMS", ctnrCode, "1", new List<positionCodeClass>()
            {
                new positionCodeClass(){ //取货站台
                    positionCode=position1,
                    type="00"
                },
                new positionCodeClass(){ //放货机台
                    positionCode=position2,
                    type="00"
                }
            }, priority, taskCode, "ZTGT21", "-1");
        }

        /// <summary>
        ///  一楼空轮出库下发AGV
        /// </summary>
        /// <param name="ctnrCode">RFID</param>
        /// <param name="position1">取货站台</param>
        /// <param name="position2">放货机台</param>
        /// <param name="taskCode">WMS任务号</param>
        /// <param name="priority">优先级</param>
        /// <returns></returns>
        public static GenAgvSchedulingTaskResponse 一楼空轮出库下发AGV(string ctnrCode, string position1, string position2, string taskCode, string priority)
        {
            if (position1 == "1018")
            {
                position1 = "41";
            }
            return GenAgvSchedulingTask2("iwms", ctnrCode, "4", new List<positionCodeClass>()
            {
                new positionCodeClass(){ //取货站台
                    positionCode=position1,
                    type="00"
                },
                new positionCodeClass(){ //放货机台
                    positionCode="500",
                    type="04"
                }
            }, priority, taskCode, "ZTGT22", "-1");
        }


        /// <summary>
        ///  Agv任务单生成接口
        /// </summary>
        /// <param name="clienCode">客户端编号</param>
        /// <param name="ctnrCode">容器编号</param>
        /// <param name="ctnrTyp">容器类型</param>
        /// <param name="positionCodePath">路径</param>
        /// <param name="priority">优先级</param>
        /// <param name="taskCode">任务单号</param>
        /// <param name="taskType">任务类型</param>
        /// <param name="hjTaskTy">合金任务模板</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static GenAgvSchedulingTaskResponse GenAgvSchedulingTask(string clienCode, string ctnrCode, string ctnrTyp, List<positionCodeClass> positionCodePath, string priority, string taskCode, string taskType, string hjTaskTy)
        {
            var res = APICaller.CallApi2<GenAgvSchedulingTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/genAgvSchedulingTask", new GenAgvSchedulingTaskRequest
            {
                clientCode = clienCode,
                ctnrCode = ctnrCode,
                ctnrTyp = ctnrTyp,
                interfaceName = "genAgvSchedulingTask",
                positionCodePath = positionCodePath,
                priority = priority,
                reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                taskCode = taskCode,
                taskTyp = taskType,
                hjTaskTy = hjTaskTy,
                tokenCode = "56898661ea976b748f328cefa6960434",
            });
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }

        public static GenAgvSchedulingTaskResponse GenAgvSchedulingTask2(string clienCode, string ctnrCode, string ctnrTyp, List<positionCodeClass> positionCodePath, string priority, string taskCode, string taskType, string hjTaskTy)
        {
            var res = APICaller.CallApi2<GenAgvSchedulingTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/genAgvSchedulingTask", new GenAgvSchedulingTaskRequest
            {
                clientCode = clienCode,
                ctnrCode = ctnrCode,
                ctnrTyp = ctnrTyp,
                interfaceName = "genAgvSchedulingTask",
                positionCodePath = positionCodePath,
                reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                taskCode = taskCode,
                taskTyp = taskType,
                reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                tokenCode = "56898661ea976b748f328cefa6960434",
            });
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException("一楼空轮下任务，海康接口报错："+res.message, LogLevelEnum.High);
            }
            return res;
        }


        #endregion 任务单生成

        #region 继续执行任务

        /// <summary>
        ///  继续执行任务
        /// </summary>
        /// <param name="taskCode">AGV任务号</param>
        /// <param name="nextPositionCode">下一个地址</param>
        /// <returns>接口返回结果</returns>
        /// <exception cref="KnownException"></exception>
        public static ContinueTaskResponse ContinueTask(string taskCode, string nextPositionCode)
        {
            if (nextPositionCode == "1017")
            {
                nextPositionCode = "40";
            }
            else if (nextPositionCode == "1018")
            {
                nextPositionCode = "41";
            }
            var res = APICaller.CallApi2<ContinueTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/continueTask", new ContinueTaskRequest
            {
                reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                taskCode = taskCode,
                nextPositionCode = new positionCodeClass()
                {
                    positionCode = nextPositionCode,
                    type = "00"
                }
            });
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }

        #endregion 
        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="AGVtaskCode">AGV任务号</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static CancelTaskResponse CancelAgvTask(string AGVtaskCode)
        {
            var res = APICaller.CallApi2<CancelTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/cancelTask", new CancelTaskRequest
            {
                reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                taskCode = AGVtaskCode,
                forceCancel = "0",
            });
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }
        /// <summary>
        /// 一键抹除
        /// </summary>
        /// <param name="AGVtaskCode">AGV任务号</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static CancelTaskResponse DelAgvTask(string AGVtaskCode, string reqCode)
        {
            var res = APICaller.CallApi2<CancelTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/oneKeyCleanTaskCode", new DelTaskRequest
            {
                reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                cleanTaskCode = AGVtaskCode,
                cancelReqCode = reqCode,
            });
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }
    }
}
