using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Model.Standard.OrderAddressCheck;

namespace XYY.Service.Standard.AddressCheck
{
    //如果好用，请收藏地址，帮忙分享。
    public class ZipCode
    {
        /// <summary>
        /// 
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Zip5 { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string City { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string State { get; set; }
    }

    public class CityStateLookupResponse
    {
        /// <summary>
        /// 
        /// </summary>
        public ZipCode ZipCode { get; set; }
    }

    public class LockupRoot
    {
        /// <summary>
        /// 
        /// </summary>
        public CityStateLookupResponse CityStateLookupResponse { get; set; }
    }

    public class UspsCityDto
    {
        public string resultStatus { get; set; }
        public string zip5 { get; set; }
        public string defaultCity { get; set; }
        public string defaultState { get; set; }
        public string defaultRecordType { get; set; }
        public List<string> citiesList { get; set; }
        public List<string> nonAcceptList { get; set; }

    }
    public class USPUAddressService : IAdderssService
    {
        public int ChannelId => throw new NotImplementedException();

        public string CountryCode => "US";

        public async Task<RecommendedAddress> GetRecommendedAddresses(OrderAddress address)
        {
            var RecommendedAddress = new RecommendedAddress
            {
                CityRecommendedAddress = new List<RecommendedAddressItem>(),
                ZipCodeRecommendedAddress = new List<RecommendedAddressItem>()
            };

            if (string.IsNullOrEmpty(address.ZipCode))
                return null;
            string url = $"http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRequest USERID=\"151XINGY7933\"><ZipCode ID= \"0\"><Zip5>{address.ZipCode}</Zip5></ZipCode></CityStateLookupRequest>";
            RestClient client = new RestClient(url);
            RestRequest req = new RestRequest(Method.POST);
            req.AddParameter("zip", address.ZipCode);
            client.Execute(req);

            var resp = await client.ExecuteAsync(req);
            if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string content = resp.Content;
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(CityStateLookupResponse));
                var result = (CityStateLookupResponse)xmlSerializer.Deserialize(new System.IO.StringReader(content));

                RecommendedAddress.ZipCodeRecommendedAddress = new List<RecommendedAddressItem>
                {
                    new RecommendedAddressItem
                    {
                        ZipCode =  address.ZipCode,
                        City = result.ZipCode.City,
                        State = result.ZipCode.State
                    }
                };
                return RecommendedAddress;
            }
            else
                return RecommendedAddress;


        }

        private RecommendedAddressItem ConvertAddress(FRSLAreas area)
        {
            return new RecommendedAddressItem { City = area.AreaName, ZipCode = area.ZipCode };
        }

        public Task<AddressGroup> Verify(OrderAddress address)
        {
            throw new NotImplementedException();
        }
    }
}
