using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Model.Standard.Channel;
using XYY.Model.Standard.Order;
using XYY.Service.Standard.ChannelApi.Base;

namespace XYY.Service.Standard.ChannelApi.SKPB
{
    public class Api : IBaseApi
    {
        public string ServiceName => "SK-PB";

        public Task<List<BagDeclareErrosResult>> BagDeclare(BagDeclareRequest bagDeclareRequest)
        {
            throw new NotImplementedException();
        }

        public void CannelOrders(List<string> systemNO)
        {
            throw new NotImplementedException();
        }

        public void CannelOrdersByModel(logistics_ChannelCancel logistics_ChannelCancel)
        {
            string apiHost = logistics_ChannelCancel.ApiHost;
            string trackingNumber = logistics_ChannelCancel.TrackingNumber;
            string apiKey = logistics_ChannelCancel.ApiKey;
            string apiSecret = logistics_ChannelCancel.ApiSecret;
            //url = logistics_ChannelCancel.ApiHost;
            string path = $"Parcels/Void/?trackingNbr={trackingNumber}";
            var token = GetToken(apiKey,apiSecret,apiHost);
            RestClient client = new RestClient(apiHost);
            RestRequest req = new RestRequest(path, Method.GET);
            req.AddHeader("authorization", $"Bearer {token}");

            var resp = client.Execute<SekoApiResult<SekoSingle_CanelOrderResponse>>(req);
            if (resp.StatusCode == System.Net.HttpStatusCode.OK && resp.Data != null)
            {
                if (!resp.Data.isSuccess)
                {
                    string errMsg = resp.Data.message;
                    if (string.IsNullOrWhiteSpace(errMsg) && resp.Data.result?.result?.errors?.Any() == true)
                    {
                        errMsg = string.Join(",", resp.Data.result?.result?.errors);
                    }
                    throw new Exception($"取消订单时出错:{errMsg}");
                }
            }
            else
            {
                string msg = resp.Content;
                throw new Exception($"取消订单失败:{msg}");
            }
        }

        private string GetToken(string apiKey,string apiSecret,string apiHost)
        {

            string userName = apiKey;
            string password = apiSecret;


            RestClient client = new RestClient(apiHost);
            RestRequest req = new RestRequest("Tokens");
            req.Method = Method.POST;
            req.AddHeader("content-type", "application/x-www-form-urlencoded");
            req.AddParameter("username", userName, ParameterType.GetOrPost);
            req.AddParameter("password", password, ParameterType.GetOrPost);
            var tokenResult = client.Execute<SekoTokenInfo>(req);
            if (tokenResult.StatusCode == System.Net.HttpStatusCode.OK)
            {
                SekoTokenInfo tokenInfo = tokenResult.Data;
                if (tokenInfo == null)
                {
                    throw new Exception("API返回TOKEN无效");
                }
                else
                {
                    return tokenInfo.access_token;
                }
            }
            else
            {
                string msg = string.IsNullOrWhiteSpace(tokenResult.ErrorMessage) ? tokenResult.Content : tokenResult.ErrorMessage;
                throw new Exception($"请求TOKEN报错:{msg}");
            }
        }

        public Task<List<string>> Confims(List<OrderConfimRequest> order, Logistics_Channel channel)
        {
            throw new NotImplementedException();
        }

        public Task<UploadResult> DataUpload(Order_Order order, List<Order_OrderGoods> goods, Logistics_Channel channel, ExtendArg arg)
        {
            throw new NotImplementedException();
        }

        public Task<byte[]> DrawPdfLabel(UploadResult result, Logistics_Channel channel, Order_Order order, List<Order_OrderGoods> orderGoods)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetTrackingNumber(string SystemNo, string TrackingNumber, string ServiceOrderNo, Logistics_Channel channel)
        {
            throw new NotImplementedException();
        }
    }
}
