using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Mapster;
using Microsoft.EntityFrameworkCore;
using YGNT.QuestionLibrary.Core.Disciplines;
using YGNT.QuestionLibrary.Core.Disciplines.Domain;
using YGNT.QuestionLibrary.Core.IRepositories;
using YGNT.QuestionLibrary.QuestionTypes.Dto;

namespace YGNT.QuestionLibrary.QuestionTypes
{
    /// <summary>
    /// 学科细分题型
    /// </summary>
    public class DisciplineQuestionTypeAppService : IDisciplineQuestionTypeAppService
    {
        private readonly IDisciplineQuestionTypeRepository _disciplineQuestionTypeRepository;
        private readonly IDisciplineQuestionTypeManager _disciplineQuestionTypeManager;

        public DisciplineQuestionTypeAppService(IDisciplineQuestionTypeRepository disciplineQuestionTypeRepository, IDisciplineQuestionTypeManager disciplineQuestionTypeManager)
        {
            _disciplineQuestionTypeRepository = disciplineQuestionTypeRepository;
            _disciplineQuestionTypeManager = disciplineQuestionTypeManager;
        }

        /// <summary>
        /// 获取学科细分题型
        /// </summary>
        /// <param name="disciplineId">学科id</param>
        /// <returns></returns>
        public async Task<List<DisciplineQuestionTypeDto>> GetListAsync([Required] int disciplineId)
        {
            return await _disciplineQuestionTypeRepository.GetAll()
                .Where(x => x.DisciplineId == disciplineId)
                .OrderBy(x => x.QuestionType.Sort)
                .ThenBy(x => x.Name)
                .Select(x => x.Adapt(new DisciplineQuestionTypeDto()
                {
                    Discipline = x.Discipline.Name,
                    QuestionType = x.QuestionType.Name,
                    QuestionCategory = x.QuestionType.QuestionCategory
                })).ToListAsync();
        }

        /// <summary>
        /// 新建学科细分题型
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        public async Task<int> CreateAsync(CreateDisciplineQuestionTypeDto create)
        {
            var questionType = create.Adapt<DisciplineQuestionType>();
            await _disciplineQuestionTypeManager.CheckNameAsync(questionType.Name, questionType.DisciplineId);
            return await _disciplineQuestionTypeRepository.InsertAndGetIdAsync(questionType);
        }

        /// <summary>
        /// 修改学科细分题型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public async Task UpdateAsync([Required] int id, UpdateDisciplineQuestionTypeDto update)
        {
            var questionType = await _disciplineQuestionTypeRepository.GetAsync(id);
            update.Adapt(questionType);
            await _disciplineQuestionTypeManager.CheckNameAsync(questionType.Name, questionType.DisciplineId);
            await _disciplineQuestionTypeRepository.UpdateAsync(questionType);
        }

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