package com.ruoyi.web.controller.front;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.content.VistorContextHolder;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResult2;
import com.ruoyi.dz.domain.DzControl;
import com.ruoyi.dz.domain.DzSubject;
import com.ruoyi.dz.service.IDzControlService;
import com.ruoyi.dz.service.IDzSubjectService;
import com.ruoyi.enums.PaperType;
import com.ruoyi.learn.domain.LearnPaper;
import com.ruoyi.learn.domain.LearnTest;
import com.ruoyi.learn.service.ILearnPaperQuestionService;
import com.ruoyi.learn.service.ILearnPaperService;
import com.ruoyi.learn.service.ILearnTestService;
import com.ruoyi.web.service.LearnTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/front/paper")
@Api(tags = "前台-学习-试卷")
public class FrontPaperController {
    private final IDzControlService dzControlService;
    private final IDzSubjectService dzSubjectService;
    private final ILearnTestService testService;
    private final ILearnPaperService paperService;
    private final LearnTeacherService learnTeacherService;
    private final ILearnPaperQuestionService paperQuestionService;

    public FrontPaperController(IDzControlService dzControlService, IDzSubjectService dzSubjectService, ILearnTestService testService, ILearnPaperService paperService, LearnTeacherService learnTeacherService, ILearnPaperQuestionService paperQuestionService) {
        this.dzControlService = dzControlService;
        this.dzSubjectService = dzSubjectService;
        this.testService = testService;
        this.paperService = paperService;
        this.learnTeacherService = learnTeacherService;
        this.paperQuestionService = paperQuestionService;
    }

    @ApiOperation("01 考试批次")
    @GetMapping(value = "batch")
    public List<LearnTest> getBatch() {
        DzControl dzControl = dzControlService.selectDzControl(VistorContextHolder.getContext());
        LearnTest cond = new LearnTest();
        cond.setYear(dzControl.getPlanYear());
        return testService.selectLearnTestList(cond);
    }

    @ApiOperation("02 考试科目")
    @GetMapping(value = "subject")
    public AjaxResult2<List<DzSubject>> getSubject() {
        DzSubject sCond = new DzSubject();
        List<DzSubject> list = dzSubjectService.selectDzSubjectList(sCond);
        return AjaxResult2.success(list);
    }

    @ApiOperation("03 知识点树")
    @GetMapping(value = "knownledge")
    public AjaxResult getKnownledge(@ApiParam("科目ID") Long subjectId) {
        return AjaxResult.success(learnTeacherService.getKnowledgeTree(subjectId, null));
    }

    @ApiOperation("04 取试卷")
    @GetMapping(value = "paper")
    public AjaxResult loadPaper(@ApiParam("考卷类型PaperType") PaperType type, @ApiParam("考卷标识") Long id) {
        LearnPaper paper = paperService.selectLearnPaperById(id);
        JSONObject root = JSONObject.from(paper);
        root.put("questions", paperQuestionService.selectLearnPaperQuestionById(id));
        return AjaxResult.success(root);
    }
}
