using Exam.Application.Interface.Exam;
using Exam.Share;
using Exam.Share.Dtos.ExamManage;
using Exam.Share.ViewResponses.Exam;
using Hotline.Application.Exam.Constants.ApiRoutes;
using Hotline.Share.Dtos.ExamManages;
using Hotline.Share.Requests.Exam;
using Hotline.Share.ViewResponses.Exam;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using Hotline.Application.Exam.Interface.ExamManages;
using Exam.Infrastructure.Data.Entity;
using Hotline.Share.Dtos.Order;
using Hotline.Tools;
using MapsterMapper;

namespace Hotline.Api.Controllers.Exam
{
    public class UserExamController : BaseController
    {
        private readonly IUserExamService _userExamService;
        private readonly IExamManageService _examManageService;
        private readonly IMapper _mapper;

        public UserExamController(IUserExamService userExamService,IExamManageService examManageService,IMapper mapper)
        {
            this._userExamService = userExamService;
            this._examManageService = examManageService;
            this._mapper = mapper;
        }

        /// <summary>
        /// 获取我的考试结果
        /// </summary>
        /// <param name="userExamPagedRequest"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.GetPagedList)]
        public async Task<UserExamResultPageViewResponse> GetPagedList([FromBody] UserExamPagedRequest userExamPagedRequest)
        {
            await _examManageService.UpdateExamStatus(new EntityQueryRequest(), HttpContext.RequestAborted);

            return await _userExamService.GetPagedListAsync(userExamPagedRequest) as UserExamResultPageViewResponse;
        }

        /// <summary>
        /// 阅卷列表
        /// </summary>
        /// <param name="gradingPagedRequest"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.GetGradingPagedList)]
        public async Task<GradingResultPageViewResponse> GetGradingPagedList([FromBody] GradingPagedRequest gradingPagedRequest)
        {
            return await _userExamService.GetGradingResultPagedList(gradingPagedRequest);
        }


        /// <summary>
        /// 开始考试
        /// </summary>
        /// <param name="startUserExamDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.Start)]
        public async Task<StartExamViewResponse> Start([FromBody] StartUserExamDto startUserExamDto)
        {
            return await _userExamService.StartUserExamAsync(startUserExamDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取试题类型分组
        /// </summary>
        /// <param name="examQuestionGroupRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.GetExamQuestionGroup)]
        public async Task<List<ExamQuestionViewResponse>> GetExamQuestionGroup([FromQuery]ExamQuestionGroupRequest examQuestionGroupRequest)
        {
            return await _userExamService.GetExamQuestionViewResponses(examQuestionGroupRequest);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="examUserQueryRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.GetUserList)]
        public async Task<List<ExamUserViewResponse>> GetUserList([FromQuery] ExamUserQueryRequest examUserQueryRequest)
        {
            return await _userExamService.GetUserListAsync(examUserQueryRequest);
        }

        /// <summary>
        /// 获取试题详情
        /// </summary>
        /// <param name="examQuestionRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.GetExamQuestion)]
        public async Task<ExamQuestionDto> GetExamQuestion([FromQuery]ExamQuestionRequest examQuestionRequest)
        {
            return await _userExamService.GetExamQuestionDto(examQuestionRequest);
        }

        /// <summary>
        /// 考试
        /// </summary>
        /// <param name="addUserExamItemDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.Exam)]
        public async Task<StartExamViewResponse> Exam([FromBody] UpdateUserExamItemDto updateUserExamItemDto)
        {
            return await _userExamService.ExamAsync(updateUserExamItemDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 提交考试
        /// </summary>
        /// <param name="submitExamDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.Submit)]
        public async Task Submit([FromBody] SubmitExamDto submitExamDto)
        {
            await _userExamService.SubmitAsync(submitExamDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 阅卷
        /// </summary>
        /// <param name="gradingExtamItemDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.Grading)]
        public async Task<GradingExamQuestionDto> Grading([FromBody] GradingExamItemDto gradingExtamItemDto)
        {
          return  await _userExamService.GradingAsync(gradingExtamItemDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 批量阅卷
        /// </summary>
        /// <param name="batchGradingExamItemDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.BatchGrading)]
        public async Task BatchGrading([FromBody]BatchGradingExamItemDto batchGradingExamItemDto)
        {
            await _userExamService.BatchGradingAsync(batchGradingExamItemDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 完成阅卷
        /// </summary>
        /// <param name="gradingExtamDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.CompleteGrading)]
        public async Task CompleteGrading([FromBody] GradingExamDto gradingExtamDto)
        {
            await _userExamService.CompleteGradingAsync(gradingExtamDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取阅卷试题
        /// </summary>
        /// <param name="gradingExamRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.GetGradingQuestions)]
        public async Task<List<GradingExamQuestionDto>> GetGradingQuestions([FromQuery] GradingExamRequest gradingExamRequest)
        {
            return await _userExamService.GetGradingExamQuestion(gradingExamRequest);
        }

        /// <summary>
        /// 获取阅卷试题
        /// </summary>
        /// <param name="examQuestionGroupRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.GetGradingQuestionViewResponces)]
        public async Task<List<GradingQuestionViewResponce>> GetGradingQuestionViewResponces([FromQuery] ExamQuestionGroupRequest examQuestionGroupRequest)
        {
            return await _userExamService.GetGradingQuestionViewResponces(examQuestionGroupRequest);
        }

        /// <summary>
        /// 获取阅卷试题
        /// </summary>
        /// <param name="viewGradingExamRequest"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.ViewGradingExamQuestion)]
        public async Task<GradingExamQuestionDto> ViewGradingExamQuestion([FromQuery] ViewGradingExamRequest viewGradingExamRequest)
        {
            return await _userExamService.ViewGradingExamQuestion(viewGradingExamRequest);
        }

        /// <summary>
        /// 获取未考试的考生信息
        /// </summary>
        /// <param name="unExamUserReportPagedRequest"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.GetUnExamUsers)]
        public async Task<UnExamUserPageViewResponse> GetUnExamUsers([FromBody] UnExamUserReportPagedRequest unExamUserReportPagedRequest)
        {
            return await _userExamService.GetUnExamUsersPaged(unExamUserReportPagedRequest);
        }

        /// <summary>
        /// 导出未考试的考生信息
        /// </summary>
        /// <param name="exportExcelDto"></param>
        /// <returns></returns>
        [HttpPost(UserExamApiRoute.ExportUnExamUsers)]
        public async Task<FileStreamResult> ExportUnExamUsers([FromBody] ExportExcelDto<UnExamUserReportPagedRequest> exportExcelDto)
        {
            if (exportExcelDto.IsExportAll)
            {
                exportExcelDto.QueryDto.IsPaged = false;
            }
            var result = await _userExamService.GetUnExamUsersPaged(exportExcelDto.QueryDto);

            var items = result.Items;

            var viewResponses = _mapper.Map<ICollection<UnExamUserViewResponse>>(items);

            dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<UnExamUserViewResponse>(exportExcelDto.ColumnInfos??new List<ColumnInfo>());

            var dtos = viewResponses
                .Select(stu => _mapper.Map(stu, typeof(UnExamUserViewResponse), dynamicClass))
                .Cast<object>()
                .ToList();

            var stream = ExcelHelper.CreateStream(dtos);

            return ExcelStreamResult(stream, "未考试的考生信息");

        }

        /// <summary>
        /// 成绩统计分析
        /// </summary>
        /// <param name="userExamResultReportPagedRequest"></param>
        /// <returns></returns>

        [HttpPost(UserExamApiRoute.GetUserExamResults)]
        public async Task<UserExamResultPageViewResponse> GetUserExamResults([FromBody] UserExamResultReportPagedRequest userExamResultReportPagedRequest) {

            return await _userExamService.GetUserExamResults(userExamResultReportPagedRequest); 
        }


        /// <summary>
        /// 导出成绩统计分析
        /// </summary>
        /// <param name="exportExcelDto"></param>
        /// <returns></returns>

        [HttpPost(UserExamApiRoute.ExportUserExamResults)]
        public async Task<FileStreamResult> ExportUserExamResults([FromBody] ExportExcelDto<UserExamResultReportPagedRequest> exportExcelDto)
        {
            if (exportExcelDto.IsExportAll)
            {
                exportExcelDto.QueryDto.IsPaged = false;
            }

            var result = await _userExamService.GetUserExamResults(exportExcelDto.QueryDto);

            var items = result.Items;

            dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<UserExamResultViewResponse>(exportExcelDto.ColumnInfos ?? new List<ColumnInfo>());

            var dtos = items
                .Select(stu => _mapper.Map(stu, typeof(UserExamResultViewResponse), dynamicClass))
                .Cast<object>()
                .ToList();

            var stream = ExcelHelper.CreateStream(dtos);

            return ExcelStreamResult(stream, "成绩统计分析");
        }



        /// <summary>
        /// 查看考试试题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="questionId"></param>
        /// <returns></returns>
        [HttpGet(UserExamApiRoute.View)]
        public async Task<ViewExamQuestionDto> View([FromQuery] string id,string questionId)
        {
            return await _userExamService.View(id, questionId);
        }
    }
}
