package com.ruoyi.web.controller.front;

import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.content.VistorContextHolder;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResult2;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.ExamType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dz.domain.DzSubject;
import com.ruoyi.dz.domain.DzTeacher;
import com.ruoyi.dz.service.IDzControlService;
import com.ruoyi.dz.service.IDzSubjectService;
import com.ruoyi.dz.service.IDzTeacherService;
import com.ruoyi.learn.domain.LearnTest;
import com.ruoyi.learn.service.ILearnTestService;
import com.ruoyi.web.domain.TestRecordCond;
import com.ruoyi.web.service.LearnStatService;
import com.ruoyi.web.service.LearnTeacherService;
import com.ruoyi.web.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/front/teacher")
@Api(tags = "前台-学习-老师")
public class FrontTeacherController extends BaseController {
    private final IDzControlService dzControlService;
    private final IDzSubjectService dzSubjectService;
    private final ILearnTestService learnTestService;
    private final LearnTeacherService learnTeacherService;
    private final StudentService studentService;
    private final LearnStatService learnStatService;
    private final IDzTeacherService dzTeacherService;

    public FrontTeacherController(IDzControlService dzControlService, IDzSubjectService dzSubjectService, ILearnTestService learnTestService, LearnTeacherService learnTeacherService, StudentService studentService, LearnStatService learnStatService, IDzTeacherService dzTeacherService) {
        this.dzControlService = dzControlService;
        this.dzSubjectService = dzSubjectService;
        this.learnTestService = learnTestService;
        this.learnTeacherService = learnTeacherService;
        this.studentService = studentService;
        this.learnStatService = learnStatService;
        this.dzTeacherService = dzTeacherService;
    }

    @ApiOperation("10 查询班级列表")
    @GetMapping("classes")
    public AjaxResult getClasses()
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(learnTeacherService.getClasses(sysUser.getUserTypeId()));
    }

    @ApiOperation("10 查询批次列表")
    @GetMapping("batchs")
    public AjaxResult getBatchs()
    {
        List<LearnTest> list = learnTestService.selectLearnTestList(new LearnTest());
        return AjaxResult.success(list);
    }

    @ApiOperation("记录-模拟卷")
    @GetMapping("record/simulated")
    public AjaxResult getRecordSimulated()
    {
        return AjaxResult.success(learnStatService.getSimulatedClassRecord(SecurityUtils.getLoginUser().getUser().getUserTypeId()));
    }

    @ApiOperation("记录-模拟卷班级")
    @GetMapping("record/simulated/{classId}")
    public AjaxResult getRecordSimulatedClass(@ApiParam("班级id") @PathVariable("classId") Long classId)
    {
        return AjaxResult.success(learnStatService.getSimulatedStudentRecord(classId));
    }

    @ApiOperation("记录-模拟卷科目")
    @GetMapping("record/simulated/subject/{studentId}")
    public AjaxResult getRecordSimulatedSubject(@ApiParam("学生ID") @PathVariable("studentId") Long studentId)
    {
        return AjaxResult.success(learnStatService.getSimulatedSubjectRecord(studentId));
    }

    @ApiOperation("记录-模拟卷科目")
    @GetMapping("record/simulated/knowledge")
    public AjaxResult getRecordSimulatedKnowledge(@ApiParam("考生ID") @RequestParam("examineeId") Long examineeId)
    {
        return AjaxResult.success(studentService.getSimulateStat(examineeId));
    }

    @ApiOperation("记录-测试卷")
    @GetMapping("record/test/cond")
    public R<TestRecordCond> getRecordTestCond()
    {
        DzTeacher dzTeacher = dzTeacherService.selectDzTeacherByTeacherId(SecurityUtils.getLoginUser().getUser().getUserTypeId());
        TestRecordCond cond = JSONObject.parseObject(dzTeacher.getBuildInfo(), TestRecordCond.class);
        return R.ok(null != cond ? cond : new TestRecordCond());
    }

    @ApiOperation("记录-测试卷")
    @GetMapping("record/test")
    public AjaxResult getRecordTest(TestRecordCond cond)
    {
        cond.setTeacherId(SecurityUtils.getLoginUser().getUser().getUserTypeId());
        return AjaxResult.success(learnStatService.getTestRecord(cond));
    }

    @ApiOperation("记录-测试卷详情")
    @GetMapping("record/test/detail")
    public AjaxResult getRecordTestDetail(TestRecordCond cond)
    {
        cond.setTeacherId(SecurityUtils.getLoginUser().getUser().getUserTypeId());
        return AjaxResult.success(learnStatService.getTestStudentRecord(cond));
    }

    @ApiOperation("记录-计划刷题 有班级显示学生")
    @GetMapping("record/planStudy")
    public TableDataInfo getRecordStudy(@ApiParam("班级") @RequestParam Long classId, @ApiParam("排序字段total/time/rate") @RequestParam String sortField, @ApiParam("升序") @RequestParam Boolean asc)
    {
        return getDataTable(learnStatService.getClassRecordPlanStudy(classId, sortField, asc));
    }

    @ApiOperation("记录-计划刷题明细")
    @GetMapping("record/planStudy/{recordId}")
    public AjaxResult getRecordPlanStudyDetail(@ApiParam("年份") @RequestParam Integer year, @ApiParam("月份") @RequestParam(required = false) Long month, @ApiParam("记录sid") @PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(learnStatService.getRecordPlanStudy(recordId, year, month));
    }

    @ApiOperation("知识点")
    @GetMapping("record/knowledge")
    public AjaxResult getRecordKnowledge(@ApiParam("班级id") @RequestParam Long classId)
    {
        return AjaxResult.success(learnStatService.getClassRecordKnowledge(classId));
    }

    @ApiOperation("知识点")
    @GetMapping("record/knowledge/{recordId}")
    public TableDataInfo getRecordKnowledgeDetail(@ApiParam("记录sid") @PathVariable("recordId") Long recordId)
    {
        return getDataTable(learnStatService.getRecordKnowledge(recordId, null));
    }

    @ApiOperation("视频记录")
    @GetMapping("record/video")
    public TableDataInfo getRecordVideo(@ApiParam("班级id") Long classId, @ApiParam("排序") boolean asc)
    {
        return getDataTable(learnStatService.getClassRecordVideo(classId, asc));
    }

    @ApiOperation("视频记录")
    @GetMapping("record/video/{recordId}")
    public AjaxResult getRecordVideoDetail(@ApiParam("记录sid") @PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(learnStatService.getRecordVideo(recordId));
    }

    @GetMapping("/subjects")
    @ApiOperation("科目列表")
    public AjaxResult2<List<DzSubject>> subjects()
    {
        SysUser vistor = VistorContextHolder.getContext();
        DzSubject sCond = new DzSubject();
        sCond.setExamTypes(vistor.getExamType().name());
        sCond.setLocations(vistor.getLocation());
        List<DzSubject> list = dzSubjectService.selectDzSubjectList(sCond);
        return AjaxResult2.success(list);
    }
}
