using DataSharing.Province;
using DataSharing.RawData;
using DataSharing.SendTask;
using DataSharing.Share.Dtos.CityStateDataExchange;
using DataSharing.Share.Enums;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Order;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace DataSharing.CityStateDataExchange
{
    /// <summary>
    /// 市州工单互转
    /// </summary>
    public class CityStateDataExchangeService : ICityStateDataExchangeService
    {
        private readonly IMapper _mapper;
        private readonly ILogger<CityStateDataExchangeService> _logger;
        private readonly IInitPushDataService _initPushDataService;
        private readonly IRepository<DsUserTokenInfo> _dsUserTokenInfoRepository;
        private readonly ISharingConfigurationManager _sharingConfigurationManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="initPushDataService"></param>
        /// <param name="dsUserTokenInfoRepository"></param>
        /// <param name="sharingConfigurationManager"></param>
        public CityStateDataExchangeService(IMapper mapper,
            ILogger<CityStateDataExchangeService> logger,
            IInitPushDataService initPushDataService,
            IRepository<DsUserTokenInfo> dsUserTokenInfoRepository,
            ISharingConfigurationManager sharingConfigurationManager)
        {
            _mapper = mapper;
            _logger = logger;
            _initPushDataService = initPushDataService;
            _dsUserTokenInfoRepository = dsUserTokenInfoRepository;
            _sharingConfigurationManager = sharingConfigurationManager;
        }

        /// <summary>
        /// 市州工单互转
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task InitCityDataAsync(OrderDto dto, CancellationToken cancellationToken)
        {

            ConfigurationCityHandOver config = new();
            if (_sharingConfigurationManager.IsCity(ConfigurationConsts.YiBin))
            {
                dto.Source = ESource.CityDataExchangeYB;
                config = _sharingConfigurationManager.GetYiBinConfig().CityHandOver;
            }
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.ZiGong))
            {
                dto.Source = ESource.CityDataExchangeZG;
                config = _sharingConfigurationManager.GetZiGongConfig().CityHandOver;
            }
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.LuZhou))
            {
                dto.Source = ESource.CityDataExchangeLz;
                config = _sharingConfigurationManager.GetLuZhouConfig().CityHandOver;
            }

            var userInfo = await _dsUserTokenInfoRepository.GetAsync(p => p.PlatformSource == dto.Source.ToString() && p.State == "1", cancellationToken);
            if (userInfo is null)
                throw UserFriendlyException.SameMessage("未配置接收市州!");

            string strSource = "";
            if (dto.TranspondCityValue == "TranspondCityZG12345")
            {
                strSource = "CityDataExchangeZG";
            }
            else if (dto.TranspondCityValue == "TranspondCityLZ12345")
            {
                strSource = "CityDataExchangeLz";
            }
            else if (dto.TranspondCityValue == "TranspondCityYB12345")
            {
                strSource = "CityDataExchangeYB";
            }

            var request = JsonSerializer.Serialize(dto);
            await _initPushDataService.InitDsSendTaskOtherPlatforms(strSource, config.PushOrder, dto.No,
                request, request, EPlatformSource.CityDataExchange, cancellationToken: cancellationToken);

        }

        /// <summary>
        /// 办理意见推送
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task UndertakAdviceAsync(OrderDto dto, CancellationToken cancellationToken)
        {
            var userInfo = await _dsUserTokenInfoRepository.GetAsync(p => p.PlatformSource == dto.Source.ToString() && p.State == "1", cancellationToken);
            if (userInfo is null)
                throw UserFriendlyException.SameMessage("未配置接收市州!");

            ConfigurationCityHandOver config = new();
            if (_sharingConfigurationManager.IsCity(ConfigurationConsts.YiBin))
                config = _sharingConfigurationManager.GetYiBinConfig().CityHandOver;
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.ZiGong))
                config = _sharingConfigurationManager.GetZiGongConfig().CityHandOver;
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.LuZhou))
                config = _sharingConfigurationManager.GetLuZhouConfig().CityHandOver;

            if (config == null)
                throw UserFriendlyException.SameMessage("未配置结果接口!");
            if (_sharingConfigurationManager.IsCity(ConfigurationConsts.YiBin))
            {
                dto.TranspondCityName = "宜宾12345";
            }
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.ZiGong))
            {
                dto.TranspondCityName = "自贡12345";
            }
            else if (_sharingConfigurationManager.IsCity(ConfigurationConsts.LuZhou))
            {
                dto.TranspondCityName = "泸州12345";
            }
            CityExchangeAddOpinionDto addOpinionDto = new CityExchangeAddOpinionDto
            {
                OrderId=dto.ExternalId,
                TranspondCityName=dto.TranspondCityName,
                ActualOpinion=dto.ActualOpinion,
                FileOpinion = dto.FileOpinion
            };

            var request = JsonSerializer.Serialize(addOpinionDto);
            await _initPushDataService.InitDsSendTaskOtherPlatforms(userInfo.PlatformSource, config.PushOpinion, dto.No,
                request, request, EPlatformSource.CityDataExchange, cancellationToken: cancellationToken);

        }

    }
}
