using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.UI;
using Microsoft.EntityFrameworkCore;
using YGNT.Infrastructure.Extension;
using YGNT.QuestionLibrary.Core.Disciplines.Dto;
using YGNT.QuestionLibrary.Core.Questions;

namespace YGNT.QuestionLibrary.Core.Disciplines.Domain
{
    public class DisciplineQuestionTypeManager : IDisciplineQuestionTypeManager
    {
        private readonly IRepository<DisciplineQuestionType> _disciplineQuestionTypeRepository;
        private readonly IRepository<Question, long> _questionRepository;

        public DisciplineQuestionTypeManager(IRepository<DisciplineQuestionType> disciplineQuestionTypeRepository, IRepository<Question, long> questionRepository)
        {
            _disciplineQuestionTypeRepository = disciplineQuestionTypeRepository;
            _questionRepository = questionRepository;
        }

        /// <summary>
        /// 检查细分题型名称是否被占用
        /// </summary>
        /// <param name="name"></param>
        /// <param name="disciplineId"></param>
        /// <returns></returns>
        public async Task CheckNameAsync(string name, int disciplineId)
        {
            var isExist = await _disciplineQuestionTypeRepository.AnyAsync(x => x.DisciplineId == disciplineId && name == x.Name);
            if (isExist)
                throw new UserFriendlyException($"细分题型名称:{name}已被占用.");
        }


        /// <summary>
        /// 获取学科下细分题型
        /// </summary>
        /// <param name="disciplineId"></param>
        /// <returns></returns>
        public async Task<List<DisciplineQuestionTypeDto>> GetDisciplineQuestionTypesAsync(int disciplineId)
        {
            var questionType = await _disciplineQuestionTypeRepository.GetAll()
                  .Where(x => x.DisciplineId == disciplineId)
                  .Select(x => new DisciplineQuestionTypeDto()
                  {
                      Name = x.Name,
                      QuestionCategory = x.QuestionType.QuestionCategory,
                      QuestionTypeId = x.QuestionTypeId
                  }).ToListAsync();
            return questionType;
        }

        /// <summary>
        /// 获取学科下细分题型
        /// </summary>
        /// <param name="disciplineId"></param>
        /// <returns></returns>
        public List<DisciplineQuestionTypeDto> GetDisciplineQuestionTypes(int disciplineId)
        {
            var questionType = _disciplineQuestionTypeRepository.GetAll()
                .Where(x => x.DisciplineId == disciplineId)
                .Select(x => new DisciplineQuestionTypeDto()
                {
                    Name = x.Name,
                    QuestionCategory = x.QuestionType.QuestionCategory,
                    QuestionTypeId = x.QuestionTypeId
                }).ToList();
            return questionType;
        }

        /// <summary>
        /// 删除题型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            var disciplineQuestionType = await _disciplineQuestionTypeRepository.GetAsync(id);
            await DeleteAsync(disciplineQuestionType);
        }

        /// <summary>
        /// 删除题型
        /// </summary>
        /// <param name="disciplineQuestionType"></param>
        /// <returns></returns>
        public async Task DeleteAsync(DisciplineQuestionType disciplineQuestionType)
        {
            var isUsed = await _questionRepository.GetAll()
                .AnyAsync(x => x.DisciplineQuestionTypeId == disciplineQuestionType.Id);
            if (isUsed) throw new UserFriendlyException($"题型:{disciplineQuestionType.Name}已被使用,禁止删除");
            await _disciplineQuestionTypeRepository.DeleteAsync(disciplineQuestionType);
        }
    }
}