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

namespace XYY.Service.Standard.TrackingService
{
    public interface _17TrackingService
    {
        Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook);
    }

    public class I17TrackingService : _17TrackingService
    {

        private string GetStatusString(int eventCode)
        {
            switch (eventCode)
            {
                case 0:
                    return "notfound";
                case 10:
                case 20:
                    return "transit";
                case 30:
                    return "pickup";
                case 35:
                    return "undelivered";
                case 40:
                    return "delivered";
                case 50:
                    return "exception";
                default:
                    return "exception";
            }
        }

        public async Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook)
        {
            if (hook is null)
            {
                throw new ArgumentNullException(nameof(hook));
            }

            if (hook.Event == "TRACKING_UPDATED")
            {
                try
                {
                    var r = Newtonsoft.Json.JsonConvert.DeserializeObject<_17Data>(hook.Data);
                    List<Z1Item> z1Items = new List<Z1Item>();
                    if (r.track.z1 != null && r.track.z1.Count > 0)
                    {
                        z1Items.AddRange(r.track.z1);
                    }
                    if (r.track.z2 != null && r.track.z2.Count > 0)
                    {
                        z1Items.AddRange(r.track.z2);
                    }
                    if (r.track.z9 != null && r.track.z9.Count > 0)
                    {
                        z1Items.AddRange(r.track.z9);
                    }
                    PostTrackingRequest postTrackingRequest = new PostTrackingRequest()
                    {
                        LastMessage = r.track.z0.d + " " + r.track.z0.z,
                        TargetTime = DateTime.Parse(r.track.z0.a),
                        LastGetTime = DateTime.Now,
                        Status = GetStatusString(r.track.e),
                        TrackingNumber = r.number,
                        List = z1Items.Select(x => new PostTrackingRequestItem
                        {
                            Event = x.z,
                            Location = string.IsNullOrEmpty(x.d) ? x.c : x.d,
                            TragetTime = DateTime.Parse(x.a)
                        }).ToList()
                    };

                    var apiSet = NodeHelper.Convert(postTrackingRequest);

                    if (apiSet.success)
                    {
                        var d = (PostTrackingRequest)apiSet.data;
                        if (postTrackingRequest.Status == "delivered"
                            && postTrackingRequest.TargetTime != null && d.DeliveryTime != null)
                        {
                            postTrackingRequest.SignTime = postTrackingRequest.TargetTime.Value;
                            postTrackingRequest.SignDay = (postTrackingRequest.SignTime - d.DeliveryTime).Value.Days;

                        }
                        postTrackingRequest.DeliveryTime = d.DeliveryTime;
                        postTrackingRequest.OnlineTime = d.OnlineTime;
                        postTrackingRequest.OnlineDay = d.OnlineDay;
                        postTrackingRequest.FailTime = d.FailTime;
                        postTrackingRequest.FailMessage = d.FailMessage;
                        postTrackingRequest.FailCNMessage = d.FailCNMessage;
                        //postTrackingRequest.SignDay = d.SignDay;
                        //postTrackingRequest.SignTime = d.SignTime;
                        postTrackingRequest.PreInternetAccessDay = d.PreInternetAccessDay;
                        postTrackingRequest.PreInternetAccessTime = d.PreInternetAccessTime;
                        postTrackingRequest.Delivered = d.Delivered;
                        return ApiJsonModel<PostTrackingRequest>.OK(postTrackingRequest);
                    }
                    else
                    {
                        return apiSet;
                    }

                }
                catch (Exception ex)
                {
                    return ApiJsonModel<PostTrackingRequest>.Error("JSON 序列化失败" + ex.Message, null);
                }
            }
            else
            {
                return ApiJsonModel<PostTrackingRequest>.OK(null);
            }
        }
    }
}
