using DataSharing.Province;
using DataSharing.SendTask;
using DataSharing.Share.Dtos.YiBin.Enterprise;
using DataSharing.Share.Dtos.ZiGong.TianQue;
using DataSharing.Share.Enums;
using Hotline.Share.Dtos.Order;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace DataSharing.ZiGong.TianQue
{
    public class TianQueService : ITianQueService
    {
        private readonly IMapper _mapper;
        private readonly ILogger<TianQueService> _logger;
        private readonly IInitPushDataService _initPushDataService;
        private readonly IChannelConfigurationManager _channelConfigurationManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="initPushDataService"></param>
        /// <param name="channelConfigurationManager"></param>
        public TianQueService(IMapper mapper,
            ILogger<TianQueService> logger,
            IInitPushDataService initPushDataService,
            IChannelConfigurationManager channelConfigurationManager)
        {
            _mapper = mapper;
            _logger = logger;
            _initPushDataService = initPushDataService;
            _channelConfigurationManager = channelConfigurationManager;
        }

        /// <summary>
        /// 随手怕天阙--推送工单信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task InitPushOrderAsync(OrderDto dto, CancellationToken cancellationToken)
        {
            List<PersonInfoDto> personInfoDtos =
            [
                new PersonInfoDto()
                {
                    ReflectUserName = dto.IsSecret == false ? dto.FromName : "保密",
                    ReflectPhone = dto.IsSecret == false ? dto.Contact : "12345",
                    ReflectCardId = ""
                },
            ];
            List<FilesDto> filesDtos = [];
            if (dto.FileJson != null && dto.FileJson.Count > 0)
            {
                var businessFile = _channelConfigurationManager.GetConfigurationBusinessFile();
                var baseAddress = businessFile.BaseUrl;
                if (baseAddress.EndsWith('/'))
                    baseAddress = baseAddress.Remove(baseAddress.Length - 1, 1);
                foreach (var item in dto.FileJson)
                {
                    filesDtos.Add(new FilesDto()
                    {
                        FileName = item.FileName,
                        FileContent = baseAddress + item.Path
                    });
                }
            }

            PushOrderDto pushOrderDto = new PushOrderDto()
            {
                ReplyCode = dto.No,
                Lon = dto.Longitude + "",
                Lat = dto.Latitude + "",
                TypeName = dto.AcceptType,
                OccurDate = dto.IncidentTime.HasValue == true ? dto.IncidentTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                DetailAddress = dto.FullAddress,
                Topic = dto.Title,
                DetailContent = dto.Content,
                Prod = true,
                RootCategoryInfo = "",
                CategoryInfo = "",
                DeadLine = "",
                IsFrequentReporting = "",
                Files = filesDtos,
                PersonList = personInfoDtos,
            };

            //写入待推送数据
            var request = JsonSerializer.Serialize(pushOrderDto, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions);
            await _initPushDataService.InitDsSendTaskOtherPlatforms("InitTianQuePushOrderAsync", YbEnterpriseDefaults.TianQuePushOrder, dto.No,
                request, request, EPlatformSource.TianQue, cancellationToken: cancellationToken);
        }
    }
}
