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

namespace XYY.Service.Standard.ChannelApi.JD
{
    public partial class Api
    {

        public WaybillDTO CreateWayBillDto(string customerFopCode, string sellerId, string storeId, Order_Order order, List<Order_OrderGoods> orderGoods, Logistics_Channel channel,string orderNumber)
        {
            if (order.ReceiverCountryCode == "US")
            {
                new USAddressCheck().CheckUSOrder(order, false);
            }
            

            List<WaybillPackDTO> waybillPacks = CreateWayBillPacks(order, orderGoods, orderNumber);
            List<PerformanceSpDTO> perfSpDtos = CreatePerformanceSpList(channel);

            string street = order.ReceiverStreet.Trim();
            string house = order.House?.Trim();
            if (!string.IsNullOrWhiteSpace(house) && !street.StartsWith(house) && !street.EndsWith(house))
            {
                street = house + " " + street;
            }
            string[] addresses = street.ToArray(35);
            if (addresses.Length > 2)
                throw new Exception("地址超长");
            string address1 = addresses[0];//max len 80,ascii only
            string address2 = addresses.Length > 1 ? addresses[1] : null;

            //京东渠道要求省州必填，但英国无省州，如果为空则以城市代替
            string state = (order.ReceiverCountryCode == "GB" && string.IsNullOrWhiteSpace(order.ReceiverState)) ? order.ReceiverCity : order.ReceiverState;
            WaybillDTO waybillDTO = new WaybillDTO();
            waybillDTO.customerCode = customerFopCode;
            waybillDTO.sellerId = sellerId;
            waybillDTO.storeId = storeId;
            waybillDTO.invokeType = "1";
            waybillDTO.customerWaybillNo = orderNumber;
            waybillDTO.orderCode = order.CustomerOrderNo;
            waybillDTO.billType = 31;
            waybillDTO.salesPlatform = "LMJ";
            waybillDTO.packageCount = 1;
            waybillDTO.isDiscard = 1;
            waybillDTO.senderName = channel.SenderName;
            waybillDTO.senderCompanyNameEn = "LMJ";
            waybillDTO.senderCountry = "CN";
            waybillDTO.senderProvince = channel.SenderState;
            waybillDTO.senderCity = channel.SenderCity;
            waybillDTO.senderCounty = "BAIYUN";
            waybillDTO.senderTown = "BAIYUN";
            waybillDTO.senderAddress = channel.SenderStreet;
            waybillDTO.senderPhone = channel.SenderPhone;
            waybillDTO.senderTel = channel.SenderPhone;
            //waybillDTO.senderMail = "gaozhuo@liangmijiu.cn";
            waybillDTO.senderZipCode = channel.SenderZipCode;
            waybillDTO.receiverName = order.ReceiverName;
            waybillDTO.receiverCountry = order.ReceiverCountryCode;
            waybillDTO.receiverProvince = state;
            waybillDTO.receiverCity = order.ReceiverCity;
            waybillDTO.receiverAddress = address1;
            waybillDTO.receiverAddress2 = address2;
            waybillDTO.receiverPhone = order.ReceiverPhone;
            waybillDTO.receiverTel = order.ReceiverPhone;
            waybillDTO.receiverMail = order.ReceiverEmail;
            waybillDTO.receiverZipCode = order.ReceiverZipCode;
            waybillDTO.houseNumber = order.House;
            waybillDTO.waybillPackDTOList = waybillPacks;
            waybillDTO.performanceSpDTOList = perfSpDtos;

            return waybillDTO;
        }

        private List<PerformanceSpDTO> CreatePerformanceSpList(Logistics_Channel channel)
        {
            string serviceCode = channel.ServiceChannelCode;
            string serviceName = channel.ServiceChannelName;
            List<PerformanceSpDTO> performanceSpDTOs = new List<PerformanceSpDTO>();
            PerformanceSpDTO spDto = new PerformanceSpDTO
            {
                code = serviceCode,
                name = serviceName,
            };
            performanceSpDTOs.Add(spDto);
            return performanceSpDTOs;
        }

        private List<WaybillPackDTO> CreateWayBillPacks(Order_Order order, List<Order_OrderGoods> goods,string orderNumber)
        {
            List<WaybillPackDTO> waybillPacks = new List<WaybillPackDTO>();

            decimal weight = (order.OPWeight <= 0 ? order.CustomerWeight : order.OPWeight) / 1000m;


            List<WaybillWareDTO> waybillWareList = CreateWaybillWareList(order, goods, orderNumber);
            WaybillPackDTO pack = new WaybillPackDTO
            {

                buWeight = weight,
                weightUnit = 2,
                customerPackCode = orderNumber,
                waybillWareList = waybillWareList
            };
            waybillPacks.Add(pack);
            return waybillPacks;

        }

        private List<WaybillWareDTO> CreateWaybillWareList(Order_Order order, List<Order_OrderGoods> goods,string orderNumber)
        {
            List<WaybillWareDTO> waybillWares = new List<WaybillWareDTO>();

            foreach (var g in goods)
            {
                WaybillWareDTO ware = new WaybillWareDTO
                {
                    customerPackCode = orderNumber,
                    goodsCode = g.GoodsNumber,
                    goodsName = g.CnName,
                    goodsNameEn = g.EnName,
                    hsCode = g.HsCode,
                    sourceCountry = "CN",
                    goodsCount = g.Quantity,
                    worth = g.DeclareFee,
                    currency = g.DeclareCurrency,

                };
                waybillWares.Add(ware);
            }
            return waybillWares;
        }

        private RequestInfo CreateRequestInfo()
        {
            RequestInfo requestInfo = new RequestInfo
            {
                customerCode = customerCode,
                gatewayUser = pin,
                @operator = @operator,
                sourceSystem = sourceSystem,
                systemType = 10
            };
            return requestInfo;
        }


        private WaybillPrintDTO CreateWaybillPrintDTO(string customerCode, string pin, string transferNumber)
        {
            WaybillPrintDTO dto = new WaybillPrintDTO
            {
                //code = serviceOrderNumber,
                customerCode = customerCode,
                customerWaybillNo = transferNumber,
                pin = pin,
                printAllCarrierBill = true
            };
            return dto;
        }
    }
}
