using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using Newtonsoft.Json;
using ProjectManagementSystem.Common.Function;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Common.Models;
using ProjectManagementSystem.Common.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.WebApi
{
    public class CustomerApi
    {
        private static string url = AppSetting.TryGetValue<string>("CustomerAddress");
        private static RestApi<ResponseResult> restApi = new RestApi<ResponseResult>();

        private static RestApi<ResponseData> lesApi = new RestApi<ResponseData>();
        private static ApiCallFailedManager apiCallFailedManager = new ApiCallFailedManager();

        public static bool ReportAgvARrrived(string taskId, string station, string type)
        {
            try
            {
                var body = new RequestReportAgvARrrived();
                body.TaskId = taskId;
                body.Station = station;
                body.Type = type;

                var res = restApi.Post(url, null, body, out string content);
                CLog.Instance.SystemLog.WriteDebug($"Post {url}{Environment.NewLine}  {JsonConvert.SerializeObject(body)}{Environment.NewLine}  {content}");

                return res.Result == "0";
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteError(ex.Message);
            }

            return false;
        }

        public static bool AgvCallback(RequestAgvCallback body, out string content)
        {
            //return LesApi("agvCallback", body, out content);

            var dataList = apiCallFailedManager.GetList();

            if (dataList.Count > 0)
            {
                InsertApiData(body);

                content = "有未回传的数据";
                return false;
            }

            bool result = LesApi("agvCallback", body, out content);
            if (!result)
            {
                InsertApiData(body);
            }

            return result;
        }

        private static void InsertApiData(RequestAgvCallback body)
        {
            ApiCallFailed data = new ApiCallFailed();
            data.Id = body.reqCode;
            data.Status = 0;
            data.CreateTime = DateTime.Now;
            data.ApiName = "agvCallback";
            data.Body = JsonConvert.SerializeObject(body);

            apiCallFailedManager.Insert(data);
        }

        public static bool AgvAndonRequestApi(RequestAgvAndonRequestApi body, out string content)
        {
            return LesApi("agvAndonRequestApi", body, out content);
        }

        public static bool AgvEmptyBoxReqApi(RequestAgvEmptyBoxReqApi body, out string content)
        {
            return LesApi("agvEmptyBoxReqApi", body, out content);
        }

        public static bool LesApi(string apiName, object body, out string content)
        {
            content = null;

            try
            {
                string apiUrl = AppSetting.TryGetValue<string>($"{apiName}Url");
                if(string.IsNullOrEmpty(apiUrl))
                {
                    apiUrl = $@"http://{url}:8070/agvCallbackService/{apiName}";
                }

                var res = lesApi.Post(apiUrl, null, body, out content);
                CLog.Instance.SystemLog.WriteDebug($"Post {apiUrl}{Environment.NewLine}  {(body is string ? body : JsonConvert.SerializeObject(body))}{Environment.NewLine}  {content}");

                return res.code == "0";
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteError(ex.Message);

                content = ex.Message;
            }

            return false;
        }
    }
}
