using ServiceCenter;
using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.WebApi;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;
using WCS.WorkEngineering.WebApi.Models.WCS.Request;
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 WmsApi
    {
        private static string _WMSUrl = null!;
        private static string _wareHouseId = null!;

        /// <summary>
        ///  WMS URL
        /// </summary>
        public static string WMSUrl
        {
            get
            {
                _WMSUrl ??= RedisHub.Default.Check("WMSUrl");
                if (string.IsNullOrEmpty(_WMSUrl))
                {
                    throw new KnownException($"请在Redis配置WMSUrl", LogLevelEnum.High);
                }
                return _WMSUrl;
            }
        }

        /// <summary>
        /// 仓库编号
        /// </summary>
        public static string wareHouseId = ServiceHub.WarehouseName;

        /// <summary>
        ///  上传重量等信息
        /// </summary>
        /// <param name="taskCode">任务号</param>
        /// <param name="weight">rfid</param>
        /// <returns></returns>
        public static SRes WcsUploadInfo(int taskCode, decimal weight) => WcsUploadInfo(taskCode, weight, "");

        /// <summary>
        ///  上传重量等信息
        /// </summary>
        /// <param name="taskCode">任务号</param>
        /// <param name="RFID">重量</param>
        /// <returns></returns>
        public static SRes WcsUploadInfo(int taskCode, string RFID) => WcsUploadInfo(taskCode, 0, RFID);

        /// <summary>
        ///   上传重量等信息
        /// </summary>
        /// <param name="taskCode">任务号</param>
        /// <param name="weight">重量</param>
        /// <param name="RFID">rfid</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes WcsUploadInfo(int taskCode, decimal weight, string RFID)
        {
            var res = APICaller.CallApi2<SRes>(WMSUrl + "/api/Hj/WcsUploadInfo", new WcsUploadInfoRequest
            {
                TaskCode = taskCode,
                Weight = weight,
                RFID = RFID
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  获取巷道
        /// </summary>
        /// <param name="wcsTaskNum">WMS任务ID</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static SRes<string> GetTunnelPriorityList(int wcsTaskNum)
        {
            var res = APICaller.CallApi2<SRes<string>>(WMSUrl + "/api/Hj/GetTunnelPriorityList", new GetTunnelPriorityListRequest
            {
                TaskNum = wcsTaskNum,
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  分配货位
        /// </summary>
        /// <param name="wcsTaskNum">WMS任务ID</param>
        /// <param name="tunnel">货位</param>
        /// <param name="device">设备号</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static SRes<ApplyStockInLocResponse> GetLocalIn(int wcsTaskNum, string tunnel, string device)
        {
            var res = APICaller.CallApi2<SRes<ApplyStockInLocResponse>>(WMSUrl + "/api/Hj/ApplyStockInLoc", new ApplyStockInLocRequest
            {
                TaskNum = wcsTaskNum,
                TunnelNum = tunnel.GetLastDigit(),
                PickUpEquipmentNo = device.ToString(),
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  堆垛机出库任务执行完成
        /// </summary>
        /// <param name="taskNum"></param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes<int> SrmPickOutCompleted(int taskNum)
        {
            var res = APICaller.CallApi2<SRes<int>>(WMSUrl + "/api/Hj/SrmPickOutCompleted", new SrmPickOutCompletedRequest
            {
                TaskNum = taskNum,
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  一楼空工字轮申请出库
        /// </summary>
        /// <param name="outEndPostion">出库位置</param>
        /// <returns></returns>
        public static SRes ApplyStockOutTask(string outEndPostion) => ApplyStockOutTask(outEndPostion, 2);

        /// <summary>
        ///  申请出库任务
        /// </summary>
        /// <param name="outEndPostion">出库位置</param>
        /// <param name="outType">出库类型</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes ApplyStockOutTask(string outEndPostion, int outType)
        {
            var request = new ApplyStockOutTaskRequest
            {
                OutEndPostion = outEndPostion,
                OutType = outType,
                WarehouseCode = wareHouseId,
            };
            switch (outEndPostion)
            {
                case "1012":
                    request.Tunnel = "1";
                    break;

                case "1014":
                    request.Tunnel = "2";
                    break;

                case "1016":
                    request.Tunnel = "3";
                    break;
                case "1118" or "1131" or "1132" or "1133" or "1134" or "1135" or "1136" or "1137" or "1139":
                    request.Tunnel = "4";
                    break;
            }
            var res = APICaller.CallApi2<SRes>(WMSUrl + "/api/Hj/ApplyStockOutTask", request);
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  获取各巷道剩余空轮数量
        /// </summary>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes<GetTunnelEmptyConResponse> GetTunnelEmptyConCount()
        {
            var res = APICaller.CallApi2<SRes<GetTunnelEmptyConResponse>>(WMSUrl + "/api/Hj/GetTunnelEmptyConCount", new GetTunnelEmptyConCountRequest { });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  完成任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes<int> CompleteTask(int taskNo)
        {
            var res = APICaller.CallApi2<SRes<int>>(WMSUrl + "/api/Hj/CompleteTask", new CompleteTaskRequest
            {
                TaskNum = taskNo,
                OperationType = Models.WMS.Request.CompleteTask.自动完成,
                WCSUpdateName = "WCS"
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  二深位获取移库任务
        /// </summary>
        /// <param name="taskNo">任务</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes<AddWcsMoveTaskResponse> AddWcsMoveTask(int taskNo)
        {
            var res = APICaller.CallApi2<SRes<AddWcsMoveTaskResponse>>(WMSUrl + "/api/Hj/AddWcsMoveTask", new CompleteTaskRequest
            {
                TaskNum = taskNo,
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        /// 向WMS获取入库任务 一次单卷
        /// </summary>
        /// <param name="barcode">产品条码</param>
        /// <param name="devCode">设备条码</param>
        /// <param name="getTunnel"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static I_WCS_GetInTaskResponse I_WCS_GetInTask(string barcode, string devCode, bool getTunnel = false)
        {
            var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(WMSUrl + "/api/Task/I_WCS_GetInTask", new List<I_WCS_GetInTaskRequest>()
            {
                new I_WCS_GetInTaskRequest(){
                     ContainerBarCode = barcode,
                     WareHouseId = wareHouseId,
                     EquipmentNo = devCode,
                     Memo1 = getTunnel ? "1" : "" //1:分巷道  2:分货位
                }
            });
            if (!res.ResType) throw new KnownException(res.ResMessage, LogLevelEnum.High);

            return res;
        }

        /// <summary>
        ///  处理任务验证接口
        /// </summary>
        /// <param name="taskNo">需要处理的任务进行验证</param>
        /// <param name="state">目标状态</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes HandleTaskVerify(List<int> taskNo, int state)
        {
            var res = APICaller.CallApi2<SRes>(WMSUrl + "/api/Hj/CancelTaskVerify", new CancelTaskVerifyRequest
            {
                TaskNo = taskNo,
                State = state
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }

        /// <summary>
        ///  处理任务接口
        /// </summary>
        /// <param name="taskNo">处理</param>
        /// <param name="state">目标状态</param>
        /// <returns></returns>
        /// <exception cref="KnownException"></exception>
        public static SRes CarryTaskInfo(List<int> taskNo, int state)
        {
            var res = APICaller.CallApi2<SRes>(WMSUrl + "/api/Hj/CarryTaskInfo", new CancelTaskVerifyRequest
            {
                TaskNo = taskNo,
                State = state
            });
            if (res.ResCode != ResponseStatusCodeEnum.Sucess)
            {
                throw new KnownException(res.ResMsg, LogLevelEnum.High);
            }
            return res;
        }
    }
}
