using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.UI;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using YGNT.Infrastructure.Model;
using YGNT.QuestionLibrary.Core.BaseData;
using YGNT.QuestionLibrary.Core.BaseData.Domain;
using YGNT.QuestionLibrary.Core.Models;
using YGNT.QuestionLibrary.Core.Questions.Domain;
using YGNT.QuestionLibrary.QuestionTypes.Dto;

namespace YGNT.QuestionLibrary.QuestionTypes
{
    /// <summary>
    /// 系统试题类型管理
    /// </summary>
    public class QuestionTypeAppService : IQuestionTypeAppService
    {

        private readonly IRepository<QuestionType> _questionTypeRepository;
        private readonly IQuestionTypeManager _questionTypeManager;

        public QuestionTypeAppService(IRepository<QuestionType> questionTypeRepository, IQuestionTypeManager questionTypeManager)
        {
            _questionTypeRepository = questionTypeRepository;
            _questionTypeManager = questionTypeManager;
        }

        /// <summary>
        /// 获取试题类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<QuestionTypeDto>> GetQuestionTypeListAsync()
        {
            var result = await _questionTypeRepository.GetAll()
                .OrderBy(x => x.Sort)
                .ThenBy(x => x.Name)
                .Select(x => x.Adapt<QuestionTypeDto>())
                .ToListAsync();
            return result;
        }


        /// <summary>
        /// 创建试题类型
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        public async Task<int> CreateQuestionTypeAsync(CreateQuestionTypeDto create)
        {
            var questionType = await _questionTypeManager.CreateAsync(create.Name,
                create.QuestionCategory,
                create.Sort);
            return questionType.Id;
        }


        /// <summary>
        /// 修改题型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public async Task UpdateQuestionTypeAsync([Required] int id, UpdateQuestionTypeDto update)
        {
            var questionType = await _questionTypeRepository.GetAsync(id);
            await _questionTypeManager.UpdateAsync(questionType, update.Name, update.QuestionCategory, update.Sort);
        }

        /// <summary>
        /// 题型排序
        /// </summary>
        /// <param name="sort"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task SortAsync(SortDto<int> sort)
        {
            var questionType = await _questionTypeRepository.GetAsync(sort.Id);
            await _questionTypeManager.SortAsync(questionType, sort.SortType);
        }


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