using Grpc.Core;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Core.Standard.Mvc;
using XYY.Data.Model.Standard.Tracking;
using XYY.Data.Model.Standard.Tracking._17;
using XYY.Service.Standard.TrackingService;
using XYY.Common.GRPC.Standard;
namespace XYY.gRpc.Tracking
{
    public class SeventeenService : SeventeenConvert.SeventeenConvertBase
    {
        private readonly ILogger<SeventeenService> _logger;
        private readonly _17TrackingService i17TrackingService;
        public SeventeenService(ILogger<SeventeenService> logger, _17TrackingService i17TrackingService)
        {
            _logger = logger;
            this.i17TrackingService = i17TrackingService;

        }


        public override async Task<SeventeenConvertRes> Convert(SeventeenConvertReq request, ServerCallContext context)
        {
            var result = await i17TrackingService.Convert(request.FromGrpc<_17TrackingMQData>());
            if (!result.success)
                _logger.LogError("api:{api} res:{res} req:{req}", context.Host + " " + context.Peer + " " + context.Method, result.message, request);
            var SeventeenConvertRes = new SeventeenConvertRes();

            SeventeenConvertRes.Code = (int)result.code;
            SeventeenConvertRes.Error = result.error ?? string.Empty;
            SeventeenConvertRes.Message = result.message ?? string.Empty;
            SeventeenConvertRes.Success = result.success;
            SeventeenConvertRes.Data = result.data != null ? result.data.ToGrpc<SeventeenConvertItem>() : null;
            
            if (result.data != null && result.data.List.Count > 0)
            {
                result.data.List.ForEach(x => SeventeenConvertRes.Data.List.Add(x.ToGrpc<SeventeenConvertItemLogs>()));
            }
            return SeventeenConvertRes;
        }


    }
}
