using Newtonsoft.Json;
using Pms.Models;
using ProjectManagementSystem.WebApiClient.Service;
using ProjectManagementSystem.Common.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.WebApi
{
    /// <summary>
    /// 用于支持PMSV4所有接口信息（需要自行反编译Pms.WebApi.dll查看具体的接口数据）
    /// 数据模型对象依赖Pms.Models.dll
    /// </summary>
    public class PmsApi
    {
        private static RestApi<WebMgrResponse> restApi = new RestApi<WebMgrResponse>();

        /// <summary>
        /// Get请求
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="apiName">接口名</param>
        /// <param name="pars">Params参数，可null</param>
        /// <param name="body">Body对象，可null</param>
        /// <param name="data">返回数据对象</param>
        /// <returns>调用成功返回True（code == 20000）</returns>
        public static bool Get<T>(EPmsApi apiName, object pars, object body, out T data)
        {
            data = default(T);
            try
            {
                string url = Common.Function.AppSetting.GetPmsApiUrl(apiName.ToString());
                var res = restApi.Get(url, pars, body, out string content);

                if (res.data != null)
                {
                    var webMgrResponse = JsonConvert.DeserializeObject<WebMgrResponse>(content);
                    data = JsonConvert.DeserializeObject<T>(webMgrResponse.data.ToString());
                }

                return res.code == 20000;
            }
            catch (Exception ex)
            {
                CLog.Instance.GetLog("System").WriteException("Exception", ex);
            }

            return false;
        }

        /// <summary>
        /// POST请求
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="apiName">接口名</param>
        /// <param name="pars">Params参数，可null</param>
        /// <param name="body">Body对象，可null</param>
        /// <param name="data">返回数据对象</param>
        /// <returns>调用成功返回True（code == 20000）</returns>
        public static bool Post<T>(EPmsApi apiName, object pars, object body, out T data)
        {
            data = default(T);
            try
            {
                string url = Common.Function.AppSetting.GetPmsApiUrl(apiName.ToString());
                var res = restApi.Post(url, pars, body, out string content);

                if (res.data != null)
                {
                    var webMgrResponse = JsonConvert.DeserializeObject<WebMgrResponse>(content);
                    data = JsonConvert.DeserializeObject<T>(webMgrResponse.data.ToString());
                }

                return res.code == 20000;
            }
            catch (Exception ex)
            {
                CLog.Instance.GetLog("System").WriteException("Exception", ex);
            }

            return false;
        }

        /// <summary>
        /// 通过反射获取PMS定义的所有接口名
        /// </summary>
        /// <returns></returns>
        private static string GetPmsWebApiNames()
        {
            //文件路径AGVS目录中的Pms.WebApi.dll
            string filePath = @"D:\yangyuyuan\AGVS\AGVS-1.8.9.0\Plugins\PMS\Pms.WebApi.dll";
            string className = "Pms.WebApi.Controllers.AgvController";

            Assembly asm = Assembly.LoadFrom(filePath);
            Type t = asm.GetType(className);
            MethodInfo[] methods = t.GetMethods();
            var names = methods.Select(d => d.Name).ToArray();

            StringBuilder sb = new StringBuilder();
            foreach (var item in names)
            {
                sb.Append(item);
                sb.AppendLine(",");
            }
            return sb.ToString();
        }
    }

    public enum EPmsApi
    {
        GetPmsVersion,
        GetPmsBehavior,
        SetPmsBehaviorFinish,
        GetTaskgroupList,
        UpdateTaskgroup,
        GetCustombehaviorList,
        UpdateTaskgroupName,
        DeleteTaskgroupName,
        RemoveTaskgroup,
        DissolveTaskgroup,
        DeleteTaskStep,
        TaskEnd,
        TaskStepSkip,
        TaskAddStep,
        TaskChangeRely,
        AddWarehouseGroup,
        UpdateWarehouseGroupByGroupId,
        AppendWarehouseGroupList,
        DeleteWarehouseGroup,
        UpdateWarehouseGroup,
        UpdateWarehouseGroupList,
        GetWarehouseGroupList,
        DeleteWarehouseGroupByType,
        GetallAgvOperateList,
        GetwarehouseGroupType,
        GetEquipmentInfo,
        OrderAdd,
        OrderCreate,
        OrderCancel,
        TaskStepIsCanRun,
        TaskChangeAGV,
        TaskChangeAssignAGV,
        TaskAppend,
        TaskAdd,
        TaskCancel,
        GetOrderInfo,
        GetTaskData,
        GetTask,
        GetTaskList,
        GetStepList,
        GetTaskCount,
        GetHistoryOrderData,
        GetHistoryOrderDataList,
        GetHistoryTask,
        GetHistoryTaskList,
        GetHistoryTaskData,
        GetHistoryTaskDataList,
        GetTaskDataList,
        GetHistoryStepList,
        ThreadschedulingPause,
        ThreadschedulingResume,
        OrderPause,
        GetCarrier,
        GetAllCarrier,
        OrderResume,
        TaskPause,
        TaskStopReceipt,
        TaskContinueReceipt,
        TaskResume,
        GetOrderData,
        GetOrderDataList,
        GetTaskBookList,
        AddTaskBook,
        UpdateTaskBook,
        DeleteTaskBook,
        GetTemplateInfo,
        AddTemplate,
        UpdateTemplate,
        UpdateTemplateList,
        DeleteTemplate,
        UpdateStepWarehouse,
        UpdateStepAgvBehavior,
        SetProperties,
        AddWarehouse,
        UpdateWarehouse,
        DeleteWarehouse,
        GetWarehouseListInfo,
        GetAllWarehouseName,
        GetAllTemplateList,
        AddOperate,
        UpdateOperate,
        DeleteOperate,
        GetOperateList,
        AGVExecuteBookTask,
        GetStations,
        GetAgvIds
    }
}

