using DataSharing.CityStateDataExchange;
using DataSharing.SendTask;
using DataSharing.SendTask.OtherPlatforms;
using DataSharing.Share.Enums;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Order;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace DataSharing.ZiGong
{
    /// <summary>
    /// 执行推送任务
    /// </summary>
    public class ZiGongSendTaskDataService : ISendTaskDataService
    {
        private readonly ILogger<ZiGongSendTaskDataService> _logger;
        private readonly ISharingConfigurationManager _sharingConfigurationManager;
        private readonly ICityStateDataExchangeService _cityStateDataExchangeService;
        private readonly IInitPushDataService _initPushDataService;
        private readonly ISmartCitySystemPusherProviderService _smartCitySystemPusherProviderService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="sharingConfigurationManager"></param>
        /// <param name="dcjtService"></param>
        public ZiGongSendTaskDataService(ILogger<ZiGongSendTaskDataService> logger,
         ISharingConfigurationManager sharingConfigurationManager,
         ICityStateDataExchangeService cityStateDataExchangeService,
         IInitPushDataService initPushDataService,
         ISmartCitySystemPusherProviderService smartCitySystemPusherProviderService)
        {
            _logger = logger;
            _sharingConfigurationManager = sharingConfigurationManager;
            _cityStateDataExchangeService = cityStateDataExchangeService;
            _initPushDataService = initPushDataService;
            _smartCitySystemPusherProviderService = smartCitySystemPusherProviderService;
        }

        /// <summary>
        /// 执行推送任务
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task SendTask(DsWaitSendTaskOtherPlatforms dto, CancellationToken cancellationToken)
        {
            if (dto is null)
                return;
            _logger.LogInformation("这里是自贡的服务--------------------------------------");

            switch (dto.PlatformSource)
            {
                case Share.Enums.EPlatformSource.Police110:
                    break;
                case Share.Enums.EPlatformSource.SmartCitySystem:
                    //智慧城市系统
                    await _smartCitySystemPusherProviderService.SendDataPusher(dto, cancellationToken);
                    break;
                //case Share.Enums.EPlatformSource.TianQue://天阙
                //    await _tianQuePusherProviderService.SendDataPusher(dto, cancellationToken);
                //    break;
                //case Share.Enums.EPlatformSource.CityDataExchange:
                //    await _dataExchangePusherProviderService.SendDataExchangeDataPusher(dto, cancellationToken);
                //    break;
                default:
                    break;
            }
        }

        /// <summary>
        ///自贡其他推送
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="order"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task OtherSendTask(string Key, OrderDto order, CancellationToken cancellationToken)
        {
            try
            {
                //智慧城市系统
                if (Key == "zgdh" && order.AcceptTypeCode == "35")
                {
                    SmartCitySystem smartCitySystem = new SmartCitySystem()
                    {
                        No = order.No,
                        AcceptType = order.AcceptType,
                        Title = order.Title,
                        CreationTime = order.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        Status = order.StatusText,
                        Content = order.Content,
                        Visits = null
                    };
                    var request = JsonSerializer.Serialize(smartCitySystem, new JsonSerializerOptions
                    {
                        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                    });
                    if (_sharingConfigurationManager.IsCity(ConfigurationConsts.ZiGong))
                    {
                        ConfigurationSmartCitySystem config = _sharingConfigurationManager.GetZiGongConfig().SmartCitySystem;
                        await _initPushDataService.InitDsSendTaskOtherPlatforms("SmartCitySystem", config.PushDataUrl, order.No,
                        request, request, EPlatformSource.SmartCitySystem, cancellationToken: cancellationToken);
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        ///撤单操作
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task RevokeOrderAsync(PublishSpecialDto dto, CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        /// <summary>
        /// 推送办理结果
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task AddHandleOpinion(OrderFlowDto dto, CancellationToken cancellationToken)
        {
            //市州互转推送办结结果
            if (dto.Order.Source == ESource.CityDataExchangeYB || dto.Order.Source == ESource.CityDataExchangeLz)
                await _cityStateDataExchangeService.UndertakAdviceAsync(dto.Order, cancellationToken);
        }

        /// <summary>
        /// 市州互转
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task TranspondCityAsync(OrderDto dto, CancellationToken cancellationToken)
        {
            //数据推送
            await _cityStateDataExchangeService.InitCityDataAsync(dto, cancellationToken);
        }

        /// <summary>
        /// 推送办理结果（泸州市政府门户网站）
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task AddHandleOpinionByCityPortal(OrderFlowDto dto, CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}
