package com.ruoyi.web.service;

import com.alibaba.fastjson2.util.DateUtils;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.enums.ExamineeStatus;
import com.ruoyi.enums.PaperStatus;
import com.ruoyi.enums.PaperType;
import com.ruoyi.learn.domain.*;
import com.ruoyi.learn.mapper.LearnExamineeMapper;
import com.ruoyi.learn.mapper.LearnKnowledgeTreeMapper;
import com.ruoyi.learn.mapper.LearnPaperMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 考试服务
 */
@Service
public class ExamService {
    private final LearnPaperMapper paperMapper;
    private final LearnKnowledgeTreeMapper knowledgeTreeMapper;
    private final LearnExamineeMapper examineeMapper;
    private final PaperService paperService;

    public ExamService(LearnPaperMapper paperMapper, LearnKnowledgeTreeMapper knowledgeTreeMapper, LearnExamineeMapper examineeMapper, PaperService paperService) {
        this.paperMapper = paperMapper;
        this.knowledgeTreeMapper = knowledgeTreeMapper;
        this.examineeMapper = examineeMapper;
        this.paperService = paperService;
    }

    /**
     * 开卷 (卷, 实时组卷)
     * @return
     */
    public AnswerSheet openExaminee(PaperType paperType, Long relatedId) {
        if(PaperType.Practice.equals(paperType)) { // TODO 先连接学生的定向信息
            return openExaminee(relatedId, SecurityUtils.getUserId(), "");
        }
        return openExaminee(paperType, relatedId, SecurityUtils.getUserId(), "");
    }

    /**
     * 临时保存
     */
    public void updateAnswerSheet(AnswerSheet answer) {

    }

    /**
     * 交卷
     * @param answer
     */
    public void commitAnswerSheet(AnswerSheet answer) {

    }

    public AnswerSheet openExaminee(PaperType paperType, Long paperId, Long studentId, String directKey) {
        AnswerSheet answerSheet = new AnswerSheet();

        LearnPaper paper = paperMapper.selectLearnPaperById(paperId);

        LearnExaminee learnExaminee = new LearnExaminee();
        learnExaminee.setStudentId(studentId);
        learnExaminee.setPaperId(paperId);
        learnExaminee.setPaperType(paperType.getVal());
        learnExaminee.setState(ExamineeStatus.Exam.getVal());
        List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
        if (CollectionUtils.isNotEmpty(examineeList)) {
            learnExaminee = examineeList.get(0);
        } else {
            learnExaminee.setState(ExamineeStatus.Exam.getVal());
            learnExaminee.setBeginTime(new Date());
            examineeMapper.insertLearnExaminee(learnExaminee);
        }
        return answerSheet;
    }

    /**
     * 根据知识点生成一次性练习卷并开始做题
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AnswerSheet openExaminee(Long knowledgeId, Long studentId, String directKey) {
        AnswerSheet answerSheet = new AnswerSheet();

        LearnKnowledgeTree knowledgeTree = knowledgeTreeMapper.selectLearnKnowledgeTreeById(knowledgeId);
        LearnPaper paper = new LearnPaper();
        paper.setPaperType(PaperType.Practice.name());
        paper.setRelateId(knowledgeId);
        paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
        paper.setStatus(PaperStatus.TmpValid.getVal());
        paper.setDirectKey(StringUtils.trimToEmpty(directKey));
        List<LearnPaper> paperList = paperMapper.selectLearnPaperList(paper);
        if (CollectionUtils.isNotEmpty(paperList)) { // 有未做完的
            LearnExaminee learnExaminee = new LearnExaminee();
            learnExaminee.setStudentId(studentId);
            learnExaminee.setPaperId(paperList.get(0).getId());
            learnExaminee.setPaperType(PaperType.Practice.getVal());
            List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
            if (CollectionUtils.isNotEmpty(examineeList)) {
                examineeList.get(0);
                return answerSheet;
            }
            // 关闭试卷
            LearnPaper up = new LearnPaper();
            up.setId(learnExaminee.getPaperId());
            up.setStatus(PaperStatus.Valid.getVal());
            paperMapper.updateLearnPaper(up);
        }

        TestPaperVO.PaperDef paperDef = new TestPaperVO.PaperDef();
        paperDef.setTotal(15L);
        paperDef.setFillExclude(true);
        paperDef.setKnowIds(Lists.newArrayList(knowledgeId));
        List<TestPaperVO.TypeDef> typeDefList= Lists.newArrayList();
        typeDefList.add(new TestPaperVO.TypeDef("单选题", "单选题", 80, 1));
        typeDefList.add(new TestPaperVO.TypeDef("判断题", "判断题", 10, 2));
        paperDef.setTypes(typeDefList);
        List<LearnPaperQuestion> pqList = paperService.getQuestions(studentId,  paperDef);

        paper.setPaperSource(0);
        paper.setSubjectId(knowledgeTree.getSubjectId());
        paper.setPaperName(studentId + "-" + knowledgeTree.getName() + "-" + DateUtils.format(new Date(), "yyyyMMddHHmmss"));
        paperService.savePaper(paper, pqList);

        LearnExaminee learnExaminee = new LearnExaminee();
        learnExaminee.setStudentId(studentId);
        learnExaminee.setPaperType(PaperType.Practice.getVal());
        learnExaminee.setPaperId(paper.getId());
        learnExaminee.setState(ExamineeStatus.Exam.getVal());
        learnExaminee.setBeginTime(new Date());
        examineeMapper.insertLearnExaminee(learnExaminee);
        return answerSheet;
    }
}
