using Exam.Infrastructure.Data.Entity;
using Exam.Share.ViewResponses.Question;
using Hotline.Application.Exam.Constants.ApiRoutes;
using Hotline.Application.Exam.Interface.Questions;
using Hotline.Application.ExportExcel;
using Hotline.Exams.Questions;
using Hotline.Repository.SqlSugar.Exam.Interface;
using Hotline.Share.Dtos.Questions;
using Hotline.Share.Requests.Question;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.Exam
{
    public class QuestionController : BaseController
    {
        private readonly IQuestionService _questionService;
        private readonly IMapper _mapper;
        private readonly IExportApplication _exportApplication;

        public QuestionController(IQuestionService questionService,IMapper mapper, IExportApplication exportApplication)
        {
            _questionService = questionService;
            this._mapper = mapper;
            this._exportApplication = exportApplication;
        }

        /// <summary>
        /// 新增题库
        /// </summary>
        /// <param name="questionDto"></param>
        /// <returns></returns>
        [HttpPost(QuestionApiRoute.Add)]
        public async Task Add([FromBody] AddQuestionDto questionDto)
        {
            await _questionService.AddAsync(questionDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 修改题库
        /// </summary>
        /// <param name="questionDto"></param>
        /// <returns></returns>
        [HttpPut(QuestionApiRoute.Update)]
        public async Task Update([FromBody] UpdateQuestionDto questionDto)
        {
            await _questionService.UpdateAsync(questionDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 删除题库
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <returns></returns>
        [HttpDelete(QuestionApiRoute.Delete)]
        public async Task Delete([FromBody] EntityQueryRequest entityQueryRequest)
        {
            await _questionService.DeleteAsync(entityQueryRequest, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取题库分页列表
        /// </summary>
        /// <param name="questionPagedRequest"></param>
        /// <returns></returns>
        [HttpPost(QuestionApiRoute.GetPagedList)]
        public async Task<QuestionPageViewResponse> GetPagedList([FromBody] QuestionPagedRequest questionPagedRequest)
        {
            var questionPageViewResponse = await _questionService.GetPagedListAsync(questionPagedRequest);

            return questionPageViewResponse as QuestionPageViewResponse;
        }

        /// <summary>
        /// 获取题库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(QuestionApiRoute.Get)]
        public async Task<QuestionDto> Get(string id)
        {
            var questionDto = await _questionService.GetAsync(new EntityQueryRequest
            {
                Id = id
            });

            return questionDto;
        }

        /// <summary>
        /// 获取题库
        /// </summary>
        /// <param name="questionRequest"></param>
        /// <returns></returns>
        [HttpPost(QuestionApiRoute.GetList)]
        public async Task<(int,List<QuestionViewResponse>)> GetList([FromBody]QuestionRequest questionRequest)
        {
            var questionPagedRequest = _mapper.Map<QuestionRequest, QuestionPagedRequest>(questionRequest);

            return await _questionService.GetListAsync(questionPagedRequest);
        }

        /// <summary>
        /// 更新开启状态
        /// </summary>
        /// <param name="statusActionRequests"></param>
        /// <returns></returns>
        [HttpPost(QuestionApiRoute.UpdateStatus)]
        public async Task UpdateStatus([FromBody] List<StatusActionRequest> statusActionRequests)
        {
            await _questionService.UpdateStatus(statusActionRequests, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.ImportExcel)]
        public async Task ImportExcel(IFormFile file)
        {
            await _questionService.ImportExcel(file, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 下载Excel模版
        /// </summary>
        /// <returns></returns>
        [HttpGet(ExamManageApiRoute.Download)]
        public FileStreamResult Download()
        {
            var list = new List<QuestionExcel>();
            return _exportApplication.ExportData(list, "Template.xlsx");
        }
    }
}
