using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.BackgroundJobs;
using Abp.Domain.Repositories;
using YGNT.QuestionLibrary.Core.Questions;
using YGNT.QuestionLibrary.Core.Tasks.Domain.Dto;
using YGNT.QuestionLibrary.Core.Tasks.Domain.Jobs;
using Z.EntityFramework.Plus;

namespace YGNT.QuestionLibrary.Core.Tasks.Domain
{
    public class TaskManager : ITaskManager
    {
        private readonly IBackgroundJobManager _backgroundJobManager;
        private readonly IRepository<QuestionConvertTask, long> _questionConvertTaskRepository;
        private readonly IRepository<QuestionOrigin, long> _questionOriginRepository;
        private readonly IRepository<QuestionReplaceTask, long> _questionReplaceTaskRepository;

        public TaskManager(IBackgroundJobManager backgroundJobManager, IRepository<QuestionConvertTask, long> questionConvertTaskRepository, IRepository<QuestionOrigin, long> questionOriginRepository, IRepository<QuestionReplaceTask, long> questionReplaceTaskRepository)
        {
            _backgroundJobManager = backgroundJobManager;
            _questionConvertTaskRepository = questionConvertTaskRepository;
            _questionOriginRepository = questionOriginRepository;
            _questionReplaceTaskRepository = questionReplaceTaskRepository;
        }

        /// <summary>
        /// 执行试题解析
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public async Task EnqueueQuestionResolveAsync(long taskId)
        {
            await _backgroundJobManager.EnqueueAsync<QuestionResolveTaskJob, long>(taskId);
        }

        /// <summary>
        /// 执行试卷生成
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="paperProduce"></param>
        /// <returns></returns>
        public async Task EnqueuePaperProduceAsync(long taskId, PaperProduceDto paperProduce)
        {
            await _backgroundJobManager.EnqueueAsync<PaperProduceTaskJob, Tuple<long, PaperProduceDto>>(new Tuple<long, PaperProduceDto>(taskId, paperProduce));
        }

        /// <summary>
        /// 执行试题转换
        /// </summary>
        /// <param name="questionOrigin"></param>
        /// <returns></returns>
        public async Task EnqueueQuestionConvertAsync(QuestionOrigin questionOrigin)
        {
            questionOrigin.QuestionConvertState = Enum.QuestionConvertState.转换中;
            var task = new QuestionConvertTask()
            {
                QuestionOriginId = questionOrigin.Id
            };
            //清理过去的试题转换任务
            await _questionConvertTaskRepository.GetAll()
                .Where(x => x.QuestionOriginId == questionOrigin.Id)
                .DeleteAsync();
            await _questionConvertTaskRepository.InsertAndGetIdAsync(task);
            await _backgroundJobManager.EnqueueAsync<QuestionConvertTaskJob, long>(task.Id);
        }

        /// <summary>
        /// 执行试题替换
        /// </summary>
        /// <param name="question"></param>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public async Task EnqueueQuestionReplaceAsync(Question question, Guid fileId)
        {
            if (question.QuestionOrigin == null)
                question.QuestionOrigin = await _questionOriginRepository.GetAsync(question.QuestionOriginId);
            question.QuestionOrigin.QuestionConvertState = Enum.QuestionConvertState.转换中;
            var questionReplaceTask = new QuestionReplaceTask()
            {
                FileId = fileId,
                QuestionId = question.Id
            };
            await _questionReplaceTaskRepository.GetAll()
                .Where(x => x.QuestionId == question.Id)
                .UpdateAsync(x => new QuestionReplaceTask() { IsDeleted = true });
            await _questionReplaceTaskRepository.InsertAndGetIdAsync(questionReplaceTask);
            await _backgroundJobManager.EnqueueAsync<QuestionReplaceTaskJob, long>(questionReplaceTask.Id);
        }
    }
}