using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.gRpc.Order;
using XYY.Model.Standard.MpsOrder;
using XYY.TaskTrack.Standard;
using Microsoft.Extensions.Logging;

namespace XYY.WindowsService.ReshMQ.Works
{
    public interface IMoreThanOneTicketGrpc : IBaseConsumer<OrderMpsOrderSubModel>
    {

    }
    public class MoreThanOneTicketGrpc : IMoreThanOneTicketGrpc
    {
        OrderGrpcChannel orderGrpcChannel;

        public MoreThanOneTicketGrpc(OrderGrpcChannel orderGrpcChannel, ILogger<MoreThanOneTicketGrpc> logger)
        {
            this.orderGrpcChannel = orderGrpcChannel;
            _logger = logger;
        }

        private readonly ILogger<MoreThanOneTicketGrpc> _logger;
        public async Task Consume(OrderMpsOrderSubModel message)
        {
            try
            {

                var client = new MoreThanOneTicketOrders.MoreThanOneTicketOrdersClient(orderGrpcChannel.Channel);
                var or = new MTOROrderRequest
                {
                    ChannelId = message.Order.ChannelId,
                    ChannelName = message.Order.ChannelName,
                    City = message.Order.ReceiverCity,
                    CountryCode = message.Order.ReceiverCountry,
                    CustomerId = message.Order.CustomerId,
                    CustomerOrderNo = message.Order.CustomerOrderNumber,
                    ZipCode = message.Order.ReceiverZipCode,
                    DraftOrderId = message.Order.DraftOrderId,
                    MpsOrderId = message.Order.Id,
                    Name = message.Order.ReceiverName,
                    Phone = message.Order.ReceiverPhone ?? string.Empty,
                    State = message.Order.ReceiverState ?? string.Empty,
                    Stree = message.Order.ReceiverStreet1,
                    Weight = message.Order.Weight,
                    ReceiverCompany=message.Order.ReceiverCompanyName ?? string.Empty,
                };
                or.Boxs.AddRange(message.Order.Boxs.Select(x => new MTOROrderRequestBox
                {
                    BoxId = x.Id,
                    BoxNumber = x.BoxNumber,
                    Height =Convert.ToDouble(x.Height),
                    Length = Convert.ToDouble(x.Length),
                    Weight = Convert.ToDouble(x.Weight),
                    Width = Convert.ToDouble(x.Width)
                }));
                var result = await client.CreateAsync(or);
                _logger.LogInformation(result.LabelPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _logger.LogError("草稿订单转换失败${message} {request}", ex.Message, message);
            }

        }
    }
}
