using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Data.Model.Standard.Tracking;
using XYY.Core.Standard.Mvc;

namespace XYY.Service.Standard.TrackingService
{
    public static class NodeHelper
    {
        public static ApiJsonModel<PostTrackingRequest> Convert(PostTrackingRequest postTrackingRequest)
        {
            string url = "http://wms.xingyunyi.cn/api/TrackingMore/ConvertTracking";
#if DEBUG
            url = "http://localhost:22707/api/TrackingMore/ConvertTracking";
#endif
            string josnMessage = Newtonsoft.Json.JsonConvert.SerializeObject(postTrackingRequest);
            RestSharp.RestClient client = new RestSharp.RestClient(url);
            //5秒超时
            client.Timeout = 5000;
            RestSharp.RestRequest request = new RestSharp.RestRequest(method: RestSharp.Method.POST);
            var r = new
            {
                TrackingNumber = postTrackingRequest.TrackingNumber,
                List = postTrackingRequest.List.Select(x => new
                {
                    StatusString = x.Event,
                    TriggerTime = x.TragetTime,
                    Location = x.Location
                })
            };

            request.AddJsonBody(r);

            var response = client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var d = Newtonsoft.Json.JsonConvert.DeserializeObject<PostTrackingRequest>(response.Content);
                return ApiJsonModel<PostTrackingRequest>.OK(d);
            }
            else
            {
                return ApiJsonModel<PostTrackingRequest>.Error((response.ErrorMessage ?? response.Content), null);
            }
        }


        public static int ConvertIntStatus(string status)
        {
            if (status == "delivered")
            {
                return 7;
            }
            else if (status == "pickup")
            {
                return 4;
            }
            else if (status == "exception")
            {
                return 6;
            }
            else if (status == "undelivered")
            {
                return 10;
            }
            else
            {
                return 4;
            }
        }
    }
}
