using DotNetCore.CAP;
using Hotline.Authentications;
using Hotline.Caching.Interfaces;
using Hotline.Caching.Services;
using Hotline.FlowEngine.Notifications;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.Orders;
using Hotline.Settings;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Mq;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.Interfaces;
using Hotline.Snapshot.Notifications;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.Application.Orders;
using Hotline.FlowEngine.Workflows;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using Microsoft.Extensions.Logging;

namespace Hotline.Application.Snapshot.Notifications;

public class SnapshotHandler : ICapSubscribe, IScopeDependency
{
    private readonly ISnapshotApplication _snapshotApplication;

    public SnapshotHandler(ISnapshotApplication snapshotApplication)
    {
        _snapshotApplication = snapshotApplication;
    }

    /// <summary>
    /// 推送成功后发送的延迟消息;
    /// 延迟检查网格员是否回复了工单;
    /// <param name="message"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [CapSubscribe(EventNames.GuiderSystemReplyDelay)]
    public async Task PostGuiderSystemDelayedNotificationHandler(PostGuiderSystemDelayed message, CancellationToken cancellationToken)
    {
        await _snapshotApplication.GuiderSystemReplyDelayAsync(message.OrderId, cancellationToken);
    }
}

/// <summary>
/// 工单归档后生成红包数据
/// </summary>
public class SnapshotOrderFiledNotificationHandler : INotificationHandler<SnapshotOrderFiledNotification>
{
    private readonly ISnapshotApplication _snapshotApplication;
    private readonly IOrderSnapshotRepository _orderSnapshotRepository;
    private readonly ILogger<SnapshotOrderFiledNotificationHandler> _logger;
    public SnapshotOrderFiledNotificationHandler(ISnapshotApplication snapshotApplication, ILogger<SnapshotOrderFiledNotificationHandler> logger, IOrderSnapshotRepository orderSnapshotRepository)
    {
        _snapshotApplication = snapshotApplication;
        _logger = logger;
        _orderSnapshotRepository = orderSnapshotRepository;
    }
    public async Task Handle(SnapshotOrderFiledNotification notification, CancellationToken cancellationToken)
    {
        try
        {
            if (_orderSnapshotRepository.HasOrder(notification.OrderId))
                await _snapshotApplication.AddRedPardAsync(notification.OrderId, cancellationToken);
        }
        catch (Exception e)
        {
            _logger.LogError(e, "工单归档后生成红包数据失败");
        }
    }
}

/// <summary>
/// 网格员办结事件处理
/// 把网格员信息回填到系统中, 和小程序账号绑定
/// 同步社区信息
/// </summary>
public class GuiderSystemFieldNotificationHandler : INotificationHandler<GuiderSystemFieldNotification>
{
    private readonly ISnapshotApplication _snapshotApplication;
    private readonly IOrderApplication _orderApplication;
    private readonly ILogger<GuiderSystemFieldNotificationHandler> _logger;

    public GuiderSystemFieldNotificationHandler(
        ISnapshotApplication snapshotApplication1,
        IOrderApplication orderApplication,
        ILogger<GuiderSystemFieldNotificationHandler> logger)
    {
        _snapshotApplication = snapshotApplication1;
        _orderApplication = orderApplication;
        _logger = logger;
    }

    public async Task Handle(GuiderSystemFieldNotification notification, CancellationToken cancellationToken)
    {
        try
        {
            await _snapshotApplication.SyncGuiderInfoAsync(notification.OrderSnapshot.Id, cancellationToken);
            await _snapshotApplication.SyncCommunityInfoAsync(notification.CommunityInfo, cancellationToken);

        }
        catch (Exception e)
        {
            _logger.LogError(e, "网格员办结同步数据失败");
        }

        //流程流转至派单组
        await _orderApplication.HandleFromWanggeyuanToMaskAsync(notification.OrderSnapshot.Id, cancellationToken);
    }
}

/// <summary>
/// 推送网格员系统
/// </summary>
public class SnapshotPushNotificationHandler : INotificationHandler<PostGuiderSystemNotification>
{
    private readonly ISnapshotApplication _snapshotApplication;

    public SnapshotPushNotificationHandler(ISnapshotApplication snapshotApplication)
    {
        _snapshotApplication = snapshotApplication;
    }

    public async Task Handle(PostGuiderSystemNotification notification, CancellationToken cancellationToken)
    {
        await _snapshotApplication.PostOrderGuiderSystemAsync(notification.OrderId, cancellationToken);
    }
}
