package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BoolValues;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.mxjb.domain.MxjbContants;
import com.ruoyi.system.service.ISysLogininforService;
import com.ruoyi.web.domain.PaperDto;
import com.ruoyi.web.domain.QuestionDto;
import com.ruoyi.web.service.AdminExaminationService;
import com.ruoyi.web.service.CacheService;
import com.ruoyi.web.service.MxjbPaperExamService;
import com.ruoyi.web.service.SyTestMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "03 考卷管理")
@RestController
@RequestMapping("front/examination/")
public class FrontExaminationController {
    private final AdminExaminationService adminExaminationService;
    private final SyTestMajorService syTestMajorService;
    private final MxjbPaperExamService mxjbPaperExamService;
    private final CacheService cacheService;
    @Autowired
    private SysLoginService sysLoginService;

    public FrontExaminationController(AdminExaminationService adminExaminationService, SyTestMajorService syTestMajorService, MxjbPaperExamService mxjbPaperExamService, CacheService cacheService) {
        this.adminExaminationService = adminExaminationService;
        this.syTestMajorService = syTestMajorService;
        this.mxjbPaperExamService = mxjbPaperExamService;
        this.cacheService = cacheService;
    }

    @ApiOperation("01 开卷")
    @GetMapping(value = "openExamineePaper")
    public AjaxResult openExamineePaper(@ApiParam("考卷类型0心里1-6IE") @RequestParam(required = false) Integer type,
                                        @ApiParam("科目id") @RequestParam(required = false) Long subjectId,
                                      @ApiParam("考卷标识") @RequestParam(required = false) String id,
                                      @ApiParam("知识点") @RequestParam(required = false) Long knowledgeId,
                                      @ApiParam("0默认1价值2职业3知识") @RequestParam Integer testType) {
        sysLoginService.isVip();
        // :1know_test-知识点,2auto-基础题库,3high_error-高频错题,4smart_test-智能刷题,5paper-真题,试卷 6必刷题
        Map<String, String> stateMap = cacheService.selectDictDataMapByType(MxjbContants.StateTypeExaminee);
        PaperDto paperDto;
        if(null != type && type > 0) {
            switch(type) {
                case 1:
                    paperDto = mxjbPaperExamService.loadPaperByType(SecurityUtils.getLoginUser().getUser(), subjectId, "知识点", "know_test", knowledgeId, true); break;
                case 2:
                    paperDto = mxjbPaperExamService.loadPaperByType(SecurityUtils.getLoginUser().getUser(), subjectId, "基础题库", "know_test", null, true); break;
                case 3:
                    paperDto = mxjbPaperExamService.loadPaperByType(SecurityUtils.getLoginUser().getUser(), subjectId, "高频错题", "know_test", null, true); break;
                case 4:
                    paperDto = mxjbPaperExamService.loadPaperByType(SecurityUtils.getLoginUser().getUser(), subjectId, "智能刷题", "know_test", null, true); break;
                case 6:
                    paperDto = mxjbPaperExamService.loadPaperByType(SecurityUtils.getLoginUser().getUser(), subjectId, "必刷题", "必刷题", null, true); break;
                default:
                    paperDto = mxjbPaperExamService.loadPaperById(Long.parseLong(id));
            }
        } else if (null != testType && testType > 0) {
            paperDto = syTestMajorService.loadPaperByTestMajorTypeId(testType);
        } else {
            return AjaxResult.error("不支持");
        }
        paperDto.setStateStr(stateMap.get(String.valueOf(paperDto.getState())));
        paperDto.calculateAllow();
        return AjaxResult.success(paperDto);
    }

    @ApiOperation("02 答题")
    @PostMapping(value = "commitExamineeQuestion")
    public AjaxResult commitExamineeQuestion(@RequestBody PaperDto paperDto) {
        return adminExaminationService.saveQuestion(paperDto);
    }

    @ApiOperation("02 交卷")
    @PostMapping(value = "commitExamineePaper")
    public AjaxResult commitExamineePaper(@RequestBody PaperDto paperDto) {
        if (MxjbContants.ExamineeTypeIeValue.equals(paperDto.getExamineeType())) {
            return mxjbPaperExamService.saveExamPaper(paperDto);
        }
        return syTestMajorService.saveTestPaper(paperDto);
    }

    @ApiOperation("04 阅卷打分")
    @PostMapping(value = "scoreExamineeQuestions")
    public AjaxResult scoreExamineeQuestions(@RequestBody PaperDto paperDto) {
        if (MxjbContants.ExamineeTypeIeValue.equals(paperDto.getExamineeType())) {
            return mxjbPaperExamService.scoreExamineeQuestion(paperDto, true);
        }
        return AjaxResult.error("不支持: " + paperDto.getExamineeType());
    }

    @ApiOperation("05 阅卷结束")
    @PostMapping(value = "scoreFinish")
    public AjaxResult scoreFinish(
            @ApiParam("考生答卷Id") @RequestParam Long examineeId,
            @ApiParam("考生类型 1是测评evaluation(default)，2是竞赛competitor 3 ai 5 homework 6 ie") @RequestParam(required = false, defaultValue = "1") Integer examineeType) {
        if (MxjbContants.ExamineeTypeIeValue.equals(examineeType)) {
            return mxjbPaperExamService.scoreFinish(examineeId);
        }
        return AjaxResult.error("不支持： " + examineeType);
    }

    @ApiOperation("06 查看题")
    @GetMapping(value = "openQuestion")
    public QuestionDto openQuestion(@ApiParam("考生答卷Id") @RequestParam Long examineeId, @ApiParam("考生试题ID") @RequestParam Long questionId,
                                    @ApiParam("考生类型 1是测评evaluation(default)，2是竞赛competitor 3 ai 5 homework 6 ie") @RequestParam(required = false, defaultValue = "1") Integer examineeType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (MxjbContants.ExamineeTypeIeValue.equals(examineeType)) {
            return mxjbPaperExamService.openQuestion(examineeId, loginUser.getUser().getUserId(), questionId);
        }
        return new QuestionDto();
    }
}
