package cn.com.qmth.stmms.ms.marking.assembler;

import cn.com.qmth.stmms.ms.commons.config.ImageConfig;
import cn.com.qmth.stmms.ms.commons.config.SystemConfig;
import cn.com.qmth.stmms.ms.commons.constant.SystemConstant;
import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
import cn.com.qmth.stmms.ms.core.cache.ParamCache;
import cn.com.qmth.stmms.ms.core.domain.ChangeLevel;
import cn.com.qmth.stmms.ms.core.domain.Paper;
import cn.com.qmth.stmms.ms.core.domain.Student;
import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;
import cn.com.qmth.stmms.ms.core.repository.StudentRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import cn.com.qmth.stmms.ms.marking.dto.ChangeLevelDTO;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class ChangeLevelAssembler {

    @Autowired
    private SystemConfig systemConfig;

    @Autowired
    ImageConfig imageConfig;

    @Autowired
    StudentRepo studentRepo;

    @Autowired
    private PaperRepo paperRepo;

    public ChangeLevelDTO toDTO(ChangeLevel changeLevel) {

        ChangeLevelDTO changeLevelDTO = null;
        if (changeLevel != null) {
            Paper paper = paperRepo.findOne(changeLevel.getPaperId());
            changeLevelDTO = new ChangeLevelDTO();
            changeLevelDTO.setId(changeLevel.getId());
            changeLevelDTO.setRejected(paper.isRejected());
            changeLevelDTO.setUpdatedOn(changeLevel.getCreateDate());
            changeLevelDTO.setResult(paper.getLevel());
            changeLevelDTO.setSn(paper.getSecretNumber());
            changeLevelDTO.setOriginLevel(changeLevel.getOriginalLevel());
            changeLevelDTO.setRedoLevel(changeLevel.getSuggestLevel());
            String imgSrc;
            String thumbSrc;
            Long workId = changeLevel.getWorkId();
            String examNumber = paper.getExamNumber();
            String areaCode = paper.getAreaCode();
            Subject subject = paper.getSubject();
            String fileName = paper.getExamNumber();
            Student student = studentRepo.findByWorkIdAndExamNumberAndTest(workId, examNumber, String.valueOf(TrialEnum.DEFAULT.getId()));
            //0：随机码 1：考号
            if(ParamCache.paramMap.get(paper.getWorkId()).getNameRule() == 1){
//                fileName = DigestUtils.md5Hex(paper.getSubject().name() + fileName);
                fileName = MD5Util.getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
            }
            if (ParamCache.paramMap.get(workId).getImageEncrypt() == 1) {
                imgSrc = systemConfig.getImageUrl(workId, subject.ordinal(), student.getId(), SystemConstant.IMAGE);
                thumbSrc = systemConfig.getThumbUrl(workId, subject.ordinal(), student.getId(), SystemConstant.THUMB);
            } else {
                imgSrc = systemConfig.getImageUrl(workId, subject.toString(), areaCode, fileName);
                thumbSrc = systemConfig.getThumbUrl(workId, subject.toString(), areaCode, fileName);
            }
            changeLevelDTO.setThumbSrc(thumbSrc);
            changeLevelDTO.setImgSrc(imgSrc);
            changeLevelDTO.setSample(paper.isSample());
            changeLevelDTO.setMarkByLeader(paper.isMarkByLeader());
            changeLevelDTO.setPaperId(changeLevel.getPaperId());
            changeLevelDTO.setAuditStatus(changeLevel.getAuditStatus());
            changeLevelDTO.setExamNumber(examNumber);
        }
        return changeLevelDTO;
    }

}