using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using YGNT.Infrastructure.Extension;
using YGNT.Infrastructure.Helper;
using YGNT.Infrastructure.Model;
using YGNT.QuestionLibrary.Core.Models;
using YGNT.QuestionLibrary.Core.Papers;
using YGNT.QuestionLibrary.Core.Papers.Domain;
using YGNT.QuestionLibrary.Papers.Dto;

namespace YGNT.QuestionLibrary.Papers
{
    /// <summary>
    /// 试卷试题
    /// </summary>
    public class PaperQuestionAppService : IPaperQuestionAppService
    {
        private readonly IRepository<Paper> _paperRepository;
        private readonly IRepository<PaperQuestion, long> _paperQuestionRepository;
        private readonly IPaperManager _paperManager;
        private readonly IUriHelper _urlHelper;

        public PaperQuestionAppService(IRepository<Paper> paperRepository, IPaperManager paperManager, IRepository<PaperQuestion, long> paperQuestionRepository, IUriHelper urlHelper)
        {
            _paperRepository = paperRepository;
            _paperManager = paperManager;
            _paperQuestionRepository = paperQuestionRepository;
            _urlHelper = urlHelper;
        }

        /// <summary>
        /// 获取试卷所有试题
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<List<PaperQuestionDto>> GetListAsync([Required] int paperId)
        {
            var uri = _urlHelper.GetRequestHostUri();
            var result = await _paperQuestionRepository.GetAll()
                .Where(x => x.PaperId == paperId)
                .OrderBy(x => x.QuestionTypeSort)
                .ThenBy(x => x.Sort)
                .Select(x => x.Adapt(new PaperQuestionDto()
                {
                    ShortName = x.Question.ShortName,
                    DisciplineQuestionType = x.DisciplineQuestionType.Name,
                    QuestionCategory = x.DisciplineQuestionType.QuestionType.QuestionCategory,
                    DisciplineId = x.Question.DisciplineId,
                    Discipline = x.Question.Discipline.Name,
                    Html = x.Question.QuestionOrigin.Html,
                    Answer = x.Question.QuestionOrigin.Answer,
                    Analysis = x.Question.QuestionOrigin.Analysis
                })).ToListAsync();
            result.ForEach(x =>
            {
                x.Analysis = x.Analysis.ReplaceSrc();
                x.Answer = x.Answer.ReplaceSrc();
                x.Html = x.Html.ReplaceSrc();
            });
            return result;
        }

        /// <summary>
        /// 给试卷添加试题
        /// </summary>
        /// <param name="paperId">试题Id</param>
        /// <param name="createList"></param>
        /// <returns></returns>
        public async Task<List<PaperQuestionDto>> AppendAsync([Required] int paperId, List<AppendPaperQuestionDto> createList)
        {
            var result = new List<PaperQuestionDto>();
            var paper = await _paperRepository.GetAsync(paperId);
            foreach (var create in createList)
            {
                await _paperManager.AddQuestion(paper,
                      create.QuestionId,
                      create.DisciplineQuestionTypeId,
                      create.SingleScore);
            }
            return result;
        }

        /// <summary>
        /// 试题排序
        /// </summary>
        /// <param name="paperQuestionId"></param>
        /// <param name="sortType"></param>
        /// <returns>sort</returns>
        [HttpPut]
        public async Task<int> SortAsync([Required] long paperQuestionId, SortType sortType)
        {
            var paperQuestion = await _paperQuestionRepository.GetAsync(paperQuestionId);
            await _paperManager.MoveQuestionAsync(paperQuestion, sortType);
            return paperQuestion.Sort;
        }

        /// <summary>
        /// 删除试题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync([Required] long id)
        {
            await _paperQuestionRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 删除试题(批量)
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task DeleteListAsync([Required] List<long> idList)
        {
            foreach (var id in idList)
            {
                await DeleteAsync(id);
            }
        }
    }
}