package com.qxgmat.controller.api;


import com.github.pagehelper.Page;
import com.nuliji.tools.*;
import com.qxgmat.data.constants.enums.logic.ExerciseLogic;
import com.qxgmat.data.constants.enums.module.PayModule;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.data.relation.entity.UserExercisePaperRelation;
import com.qxgmat.data.relation.entity.UserHomeworkPreviewRelation;
import com.qxgmat.dto.extend.UserExercisePaperExtendDto;
import com.qxgmat.dto.extend.UserHomeworkPreviewExtendDto;
import com.qxgmat.dto.request.*;
import com.qxgmat.dto.response.UserClassDetailDto;
import com.qxgmat.dto.response.UserExerciseGroupDto;
import com.qxgmat.dto.response.UserQuestionDetailDto;
import com.qxgmat.help.ShiroHelp;
import com.qxgmat.service.ExercisePaperService;
import com.qxgmat.service.HomeworkPreviewService;
import com.qxgmat.service.UserPaperService;
import com.qxgmat.service.inline.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/question")
@Api(tags = "题目", description = "题目接口")
public class QuestionController {

    @Autowired
    private ShiroHelp shiroHelp;

    @Autowired
    private QuestionNoService questionNoService;

    @Autowired
    private UserPaperService userPaperService;

    @Autowired
    private UserReportService userReportService;

    @Autowired
    private UserQuestionService userQuestionService;

    @Autowired
    private HomeworkPreviewService homeworkPreviewService;

    @Autowired
    private ExercisePaperService exercisePaperService;

    @Autowired
    private UserClassService userClassService;

    @Autowired
    private UserPayService userPayService;

    @RequestMapping(value = "/class/process", method = RequestMethod.GET)
    @ApiOperation(value = "获取课程进度", notes = "获取所有课程及状态进度", httpMethod = "GET")
    public Response<List<UserClassDetailDto>> classProcess()  {
        User user = (User) shiroHelp.getLoginUser();
        List<UserClass> userClassList = userClassService.getByUser(user.getId());
        List<UserClassDetailDto> dtos = Transform.convert(userClassList, UserClassDetailDto.class);

        // 获取每个科目的最后3次作业
        Map<Object, Collection<UserHomeworkPreviewRelation>> previewMap = homeworkPreviewService.groupByCategory(user.getId(), 3);
        Transform.combine(dtos, previewMap, UserClassDetailDto.class, "category", "previews", UserHomeworkPreviewExtendDto.class);

        // 获取课程状态
        List<UserPay> pays = userPayService.listUnUse(user.getId(), PayModule.CLASS);
        Collection ids = Transform.getIds(userClassList, UserClass.class, "category");
        for(UserPay pay : pays){
            Integer category = Integer.valueOf(pay.getModuleExtend());
            if (!ids.contains(category)){
                UserClassDetailDto dto = new UserClassDetailDto();
                dto.setCategory(category);
                dto.setPayed(true);
                dtos.add(dto);
            }
        }

        return ResponseHelp.success(dtos);
    }

    @RequestMapping(value = "/preview/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取预习作业列表", notes = "获取预习作业列表", httpMethod = "GET")
    public Response<PageMessage<UserHomeworkPreviewExtendDto>> listPreview(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Number category,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Boolean finish
    )  {
        User user = (User) shiroHelp.getLoginUser();
        PageResult<UserHomeworkPreviewRelation> p = homeworkPreviewService.list(page, size, category, user.getId(), endTime, finish);

        List<UserHomeworkPreviewExtendDto> pr = Transform.convert(p, UserHomeworkPreviewExtendDto.class);

        // 获取试卷统计信息
        Map map = Transform.getMap(p, UserHomeworkPreviewRelation.class, "id", "preview");
        Map<Integer, Integer[]> questionNoIdsMap = new HashMap<>();
        for(Object value : map.keySet()){
            Integer key = (Integer) value;
            HomeworkPreview preview = (HomeworkPreview) map.get(key);
            questionNoIdsMap.put(key, preview.getQuestionNoIds());
        }
        Map statMap = questionNoService.statPaperMap(questionNoIdsMap);
        Transform.combine(pr, statMap, UserHomeworkPreviewExtendDto.class, "id", "stat");

        return ResponseHelp.success(pr, page, size, p.getTotal());
    }

    @RequestMapping(value = "/exercise/process", method = RequestMethod.GET)
    @ApiOperation(value = "练习进度", httpMethod = "GET")
    public Response<List<UserExerciseGroupDto>> exerciseProcess(
            @RequestParam(required = true) Integer structId, // 第三层,查询第4层,以及第三层汇总
            HttpSession session) {
        Page<UserExerciseGroupDto> p=null;

        return ResponseHelp.success(p);
    }

    @RequestMapping(value = "/exercise/place", method = RequestMethod.GET)
    @ApiOperation(value = "练习组卷考点分组条件", httpMethod = "GET")
    public Response<List<String>> exercisePlace(HttpSession session) {
        return ResponseHelp.success(exercisePaperService.groupPlace());
    }

    @RequestMapping(value = "/exercise/paper", method = RequestMethod.GET)
    @ApiOperation(value = "练习组卷列表", httpMethod = "GET")
    public Response<PageMessage<UserExercisePaperExtendDto>> listExercisePaper(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = true) Integer structId,
            @RequestParam(required = true) String logic,
            @RequestParam(required = false, name = "logic_extend") String logicExtend,
            @RequestParam(required = false)  Integer times,
            HttpSession session) {
        User user = (User) shiroHelp.getLoginUser();
        PageResult<UserExercisePaperRelation> p = exercisePaperService.list(page, size, structId, user.getId(), ExerciseLogic.ValueOf(logic), logicExtend, times);

        List<UserExercisePaperExtendDto> pr = Transform.convert(p, UserExercisePaperExtendDto.class);

        // 获取试卷统计信息
        Map map = Transform.getMap(p, UserExercisePaperRelation.class, "id", "paper");
        Map<Integer, Integer[]> questionNoIdsMap = new HashMap<>();
        for(Object value : map.keySet()){
            Integer key = (Integer) value;
            ExercisePaper paper = (ExercisePaper) map.get(key);
            questionNoIdsMap.put(key, paper.getQuestionNoIds());
        }
        Map statMap = questionNoService.statPaperMap(questionNoIdsMap);
        Transform.combine(pr, statMap, UserHomeworkPreviewExtendDto.class, "id", "stat");

        return ResponseHelp.success(pr, page, size, p.getTotal());
    }

    @RequestMapping(value = "/examination/process", method = RequestMethod.GET)
    @ApiOperation(value = "模考进度", httpMethod = "GET")
    public Response<PageMessage<ExercisePaper>> examinationProcess(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            HttpSession session) {
        Page<ExercisePaper> p=null;

        return ResponseHelp.success(p, page, size, p.getTotal());
    }

    @RequestMapping(value = "/examination/paper", method = RequestMethod.GET)
    @ApiOperation(value = "模考组卷列表", httpMethod = "GET")
    public Response<PageMessage<ExercisePaper>> examinationPaperList(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            HttpSession session) {
        Page<ExercisePaper> p = null;

        return ResponseHelp.success(p, page, size, p.getTotal());
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取题目详情", notes = "获取题目详情", httpMethod = "GET")
    public Response<UserQuestionDetailDto> detail(
            @RequestParam(required = false) String userQuestionId
    )  {
        User user = (User) shiroHelp.getLoginUser();
        return ResponseHelp.success(null);
    }

    @RequestMapping(value = "/exercise/start", method = RequestMethod.POST)
    @ApiOperation(value = "开始: 练习", notes = "提交考试设置", httpMethod = "POST")
    public Response<Boolean> start(@RequestBody @Validated ExerciseStartDto dto)  {
        User user = (User) shiroHelp.getLoginUser();

        return ResponseHelp.success(null);
    }

    @RequestMapping(value = "/preview/start", method = RequestMethod.POST)
    @ApiOperation(value = "开始: 预习作业", notes = "提交考试设置", httpMethod = "POST")
    public Response<User> start(@RequestBody @Validated PreviewStartDto dto)  {
        User user = (User) shiroHelp.getLoginUser();
        return ResponseHelp.success(null);
    }

    @RequestMapping(value = "/next", method = RequestMethod.POST)
    @ApiOperation(value = "获取下一题", notes = "获取下一题", httpMethod = "POST")
    public Response<User> next(@RequestBody @Validated QuestionNextDto dto)  {
        User user = (User) shiroHelp.getLoginUser();
        return ResponseHelp.success(null);
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "提交题目答案", notes = "提交题目", httpMethod = "POST")
    public Response<User> submit(@RequestBody @Validated QuestionSubmitDto dto)  {
        User user = (User) shiroHelp.getLoginUser();

        return ResponseHelp.success(null);
    }

    @RequestMapping(value = "/finish", method = RequestMethod.POST)
    @ApiOperation(value = "完成考试", notes = "完成考试", httpMethod = "POST")
    public Response<User> finish(@RequestBody @Validated QuestionFinishDto dto)  {
        User user = (User) shiroHelp.getLoginUser();
        return ResponseHelp.success(null);
    }
}