using AutoMapper;
using EasyCaching.Core;
using EntityFrameworkCore.Triggered;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using System.Linq.Expressions;

namespace IRaCIS.Core.Application.Triggers
{

    public class VisitTaskIbeforeTrigger : IBeforeSaveTrigger<VisitTask>, IAfterSaveTrigger<VisitTask>
    {

        private readonly IRepository<VisitTask> _visitTaskRepository;
        private readonly IRepository<ReadingJudgeInfo> _readingJudgeInfoRepository;

        public VisitTaskIbeforeTrigger(

            IRepository<VisitTask> visitTaskRepository, IRepository<ReadingJudgeInfo> readingJudgeInfoRepository)
        {

            _visitTaskRepository = visitTaskRepository;
            _readingJudgeInfoRepository = readingJudgeInfoRepository;
        }

        /// <summary>
        /// 因为维护状态先后顺序导致  裁判任务关联的 任务上的JudgeVisitTaskId==nulll  在这里需要重新设置下
        /// 
        /// 比如： 申请裁判任务重阅，事务里面本来设置了任务上的裁判id,但是因为下面的逻辑，导致设置的值又被清理了，只能重新设置下  
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task AfterSave(ITriggerContext<VisitTask> context, CancellationToken cancellationToken)
        {
            var visitTask = context.Entity;
            //裁判任务维护，当裁判任务受到影响的时候，需要清理任务上关联的裁判任务id
            if (context.ChangeType == ChangeType.Added && visitTask.ReadingCategory == ReadingCategory.Judge && visitTask.TaskState == TaskState.Effect)
            {

                var find = _readingJudgeInfoRepository.Where(t => t.JudgeTaskId == visitTask.Id).FirstOrDefault();

                if (find != null)
                {
                    var ids = new Guid[] { find.TaskIdOne, find.TaskIdTwo };
                    await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => ids.Contains(t.Id), u => new VisitTask() { JudgeVisitTaskId = visitTask.Id });
                }

            }
        }

        /// <summary>
        /// 比如  两个任务产生了裁判，然后其中一个人申请了重阅，影响了裁判，需要清理之前任务的上裁判id
        /// 
        /// 
        /// 因为申请重阅，退回，里面分有序，无序，情况太多，所以不在那块逻辑修改，不然得加多个地方处理，在这里统一处理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task BeforeSave(ITriggerContext<VisitTask> context, CancellationToken cancellationToken)
        {
            var visitTask = context.Entity;
            //裁判任务维护，当裁判任务受到影响的时候，需要清理任务上关联的裁判任务id
            if (context.ChangeType == ChangeType.Modified)
            {
                if (visitTask.ReadingCategory == ReadingCategory.Judge && (visitTask.TaskState == TaskState.Adbandon || visitTask.TaskState == TaskState.HaveReturned))
                {
                    var find = _readingJudgeInfoRepository.Where(t => t.JudgeTaskId == visitTask.Id).FirstOrDefault();

                    if (find != null)
                    {
                        var ids = new Guid[] { find.TaskIdOne, find.TaskIdTwo };
                        await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => ids.Contains(t.Id), u => new VisitTask() { JudgeVisitTaskId = null });
                    }
                }
            }
        }
    }
}