using DataSharing.RawData;
using DataSharing.SendTask;
using DataSharing.SendTask.OtherPlatforms;
using DataSharing.Share.Dtos.CityStateDataExchange;
using DataSharing.Share.Enums;
using DotNetCore.CAP;
using Microsoft.Extensions.Logging;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace DataSharing.CityStateDataExchange
{
    public class CityStateDataExchangePusherProviderService : ICityStateDataExchangePusherProviderService, IScopeDependency
    {
        private readonly ILogger<CityStateDataExchangePusherProviderService> _logger;
        private readonly CityStateDataExchangeClient _dataExchangeClient;
        private readonly IRepository<DsWaitSendTaskOtherPlatforms> _dsSendTaskRepository;
        private readonly ICapPublisher _capPublisher;
        private readonly IRepository<DsSendTaskDetailInfo> _dsSendTaskInfoRepository;
        private readonly IRepository<DsUserTokenInfo> _dsUserTokenInfoRepository;
        private readonly IRepository<DsSendTaskOtherPlatforms> _sendTaskOtherPlatformsRepository;
        private readonly IChannelConfigurationManager _channelConfigurationManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="dataExchangeClient"></param>
        /// <param name="dsSendTaskRepository"></param>
        /// <param name="capPublisher"></param>
        /// <param name="dsSendTaskInfoRepository"></param>
        /// <param name="dsUserTokenInfoRepository"></param>
        /// <param name="sendTaskOtherPlatformsRepository"></param>
        /// <param name="channelConfigurationManager"></param>
        public CityStateDataExchangePusherProviderService(ILogger<CityStateDataExchangePusherProviderService> logger,
             CityStateDataExchangeClient dataExchangeClient,
            IRepository<DsWaitSendTaskOtherPlatforms> dsSendTaskRepository,
            ICapPublisher capPublisher,
            IRepository<DsSendTaskDetailInfo> dsSendTaskInfoRepository,
            IRepository<DsUserTokenInfo> dsUserTokenInfoRepository,
            IRepository<DsSendTaskOtherPlatforms> sendTaskOtherPlatformsRepository,
            IChannelConfigurationManager channelConfigurationManager)
        {
            _logger = logger;
            _dataExchangeClient = dataExchangeClient;
            _dsSendTaskRepository = dsSendTaskRepository;
            _capPublisher = capPublisher;
            _dsSendTaskInfoRepository = dsSendTaskInfoRepository;
            _dsUserTokenInfoRepository = dsUserTokenInfoRepository;
            _sendTaskOtherPlatformsRepository = sendTaskOtherPlatformsRepository;
            _channelConfigurationManager = channelConfigurationManager;
        }

        /// <summary>
        /// 市州数据交换推送数据
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task SendDataExchangeDataPusher(DsWaitSendTaskOtherPlatforms dto, CancellationToken cancellationToken)
        {
            if (dto is null)
                return;
            string TaskType = dto.TaskType;

            var userInfo = await _dsUserTokenInfoRepository.GetAsync(p => p.PlatformSource == dto.TaskType, cancellationToken);
            if (userInfo is null || string.IsNullOrEmpty(userInfo.AddressUrl))
                return;

            var baseAddress = userInfo.AddressUrl;
            if (!baseAddress.EndsWith('/'))
                baseAddress += "/";
            CityStateDataExchangeResponseDto response = new();
            string error = "";
            try
            {
                response = await _dataExchangeClient.RequestAsync<CityStateDataExchangeResponseDto>(userInfo, baseAddress + dto.Path, dto.HttpMethod, dto.Request, cancellationToken);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            //写入推送明细表
            DsSendTaskDetailInfo dsSendTaskInfo = new()
            {
                TaskId = dto.Id,
                Result = System.Text.Json.JsonSerializer.Serialize(response),
                ProcessingServices = _channelConfigurationManager.GetConfigurationProcessingServices(),
                ResultErrorData = error
            };

            //如果推送成功修改数据状态
            if (response != null)
            {
                #region 处理待推送数据，如果返回成功则直接删除数据，返回失败更新数据
                //处理待推送数据，如果返回成功则直接删除数据，返回失败更新数据
                if (response.code == "1")
                {
                    await _dsSendTaskRepository.RemoveAsync(dto, cancellationToken: cancellationToken);
                }
                else
                {
                    //看看需不需要处理一些信息
                    dto.LastTime = DateTime.Now;
                    dto.SendTimes = dto.SendTimes + 1;

                    if (dto.FirstTime is null)
                        dto.FirstTime = DateTime.Now;

                    if (dto.SendTimes >= 7)
                        dto.State = EWaitSendTaskState.PushFail;
                    else
                        dto.State = EWaitSendTaskState.WaitPush;

                    await _dsSendTaskRepository.UpdateAsync(dto, cancellationToken);
                }
                #endregion

                #region 处理推送主表数据
                //处理推送主表数据
                var data = await _sendTaskOtherPlatformsRepository.GetAsync(p => p.Id == dto.Id, cancellationToken);
                if (data != null)
                {
                    data.LastTime = DateTime.Now;
                    data.SendTimes = data.SendTimes + 1;

                    if (data.FirstTime is null)
                        data.FirstTime = DateTime.Now;
                    if (response.code == "1")
                        data.IsSuccess = ESendTaskState.PushSuccess;
                    else
                        data.IsSuccess = ESendTaskState.PushFail;
                    await _sendTaskOtherPlatformsRepository.UpdateAsync(data, cancellationToken);
                }
                #endregion

                CityStateTranspondRawDataDto cityRawDataDto = new()
                {
                    OrderCode = dto.ProvinceNo,
                    SendTimes = dto.SendTimes,
                    Result = Newtonsoft.Json.JsonConvert.SerializeObject(response),
                    NewCode = response.acceptCode
                };

                if (response.code == "1")
                {
                    dsSendTaskInfo.IsSuccess = true;
                    cityRawDataDto.IsSuccess = true;
                }

                //推业务系统
                await _capPublisher.PublishAsync(Hotline.Share.Mq.EventNames.SharingOrderTranspondCity, cityRawDataDto, cancellationToken: cancellationToken);

            }
            await _dsSendTaskInfoRepository.AddAsync(dsSendTaskInfo, cancellationToken);
        }
    }
}
