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.MarkLogOperType;
import cn.com.qmth.stmms.ms.core.domain.MarkStage;
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.domain.task.MarkTask;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.repository.MarkLogRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import cn.com.qmth.stmms.ms.core.repository.StudentRepo;
import cn.com.qmth.stmms.ms.marking.dto.MarkTaskDTO;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * Created by zhengmin on 2016/10/12.
 */
@Component
public class MarkTaskAssembler {

    @Autowired
    private SystemConfig systemConfig;

    @Autowired
    ImageConfig imageConfig;

    @Autowired
    StudentRepo studentRepo;

    @Autowired
    MarkLogRepo markLogRepo;

    @Autowired
    MarkUserRepo markUserRepo;

    public MarkTaskDTO toDTO(MarkTask markTask) {

        MarkTaskDTO markTaskDTO = null;
        if (markTask != null) {
            markTaskDTO = new MarkTaskDTO();
            markTaskDTO.setId(markTask.getId());
            markTaskDTO.setRejected(markTask.isRejected());
            markTaskDTO.setMarkerId(markTask.getMarkerId());
            MarkUser markUser = markUserRepo.findOne(markTask.getMarkerId());
            markTaskDTO.setLoginName(markUser.getLoginName());
            markTaskDTO.setMarker(markTask.getMarkerName());
            markTaskDTO.setUpdatedOn(markTask.getUpdatedOn());
            markTaskDTO.setResult(markTask.getResult());
            Paper paper = markTask.getPaper();
            if (markTask.getStage() == MarkStage.LEVEL) {
                markTaskDTO.setLevel(markTask.getResult());
                markTaskDTO.setOriginLevel(markTask.getOriginLevel() == null ? "":markTask.getOriginLevel());
            } else {
                markTaskDTO.setLevel(paper.getLevel());
            }
            markTaskDTO.setSn(markTask.getSecretNumber());
            markTaskDTO.setRedoLevel(paper.getRedoLevel());
            String imgSrc = null;
            String thumbSrc = null;
            String fileName = paper.getExamNumber();
            Student student = studentRepo.findByWorkIdAndExamNumberAndTest(paper.getWorkId(), paper.getExamNumber(), 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(paper.getWorkId(), paper.getSubject().ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
            }
            if (ParamCache.paramMap.get(paper.getWorkId()).getImageEncrypt() == 1) {
                imgSrc = systemConfig.getImageUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.IMAGE);
                thumbSrc = systemConfig.getThumbUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.THUMB);
            } else {
                imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
                thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
            }
//            String imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
//            String thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
            markTaskDTO.setThumbSrc(thumbSrc);
            markTaskDTO.setImgSrc(imgSrc);
            markTaskDTO.setSample(paper.isSample());
            markTaskDTO.setMarkByLeader(paper.isMarkByLeader());
            markTaskDTO.setRandomSeqNew(markTask.getRandomSeqNew());
            markTaskDTO.setRandomSeq(markTask.getRandomSeq());
            markTaskDTO.setPaperId(paper.getId());
            markTaskDTO.setSerialNumber(markTask.getSerialNumber());
            if(Objects.nonNull(markTask.getDisplayNumber())) {
                markTaskDTO.setDisplayNumber(markTask.getDisplayNumber() == 1);
            }
            markTaskDTO.setShift(paper.isShift());
            markTaskDTO.setShiftScore(paper.isShiftScore());

            markTaskDTO.setIsRejectedByLeader(paper.isRejectedByLeader() == null ? false : paper.isRejectedByLeader());
            //打回总次数
            int rejectedCount = markLogRepo.countByWorkIdAndPaperIdAndOperType(paper.getWorkId(), paper.getId(), MarkLogOperType.SYSTEM_CALLBACK_LEVEl.getId(), MarkLogOperType.CALLBACK_LEVEl.getId());
            markTaskDTO.setRejectedCount(rejectedCount);
        }
        return markTaskDTO;
    }

    public MarkTaskDTO toDTO(MarkTask markTask, boolean oldRejected) {
        MarkTaskDTO markTaskDTO = null;
        if (markTask != null) {
            markTaskDTO = new MarkTaskDTO();
            markTaskDTO.setId(markTask.getId());
            markTaskDTO.setRejected(markTask.isRejected());
            markTaskDTO.setOldRejected(oldRejected);
            markTaskDTO.setMarker(markTask.getMarkerName());
            markTaskDTO.setUpdatedOn(markTask.getUpdatedOn());
            markTaskDTO.setResult(markTask.getResult());
            Paper paper = markTask.getPaper();
            if (markTask.getStage() == MarkStage.LEVEL) {
                markTaskDTO.setLevel(markTask.getResult());
                markTaskDTO.setOriginLevel(markTask.getOriginLevel());
            } else {
                markTaskDTO.setLevel(paper.getLevel());
            }
            markTaskDTO.setSn(markTask.getSecretNumber());
            markTaskDTO.setRedoLevel(paper.getRedoLevel());
            String imgSrc = null;
            String thumbSrc = null;
            String fileName = paper.getExamNumber();
            Student student = studentRepo.findByWorkIdAndExamNumberAndTest(paper.getWorkId(), paper.getExamNumber(), 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(paper.getWorkId(), paper.getSubject().ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
            }
            if (ParamCache.paramMap.get(paper.getWorkId()).getImageEncrypt() == 1) {
                imgSrc = systemConfig.getImageUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.IMAGE);
                thumbSrc = systemConfig.getThumbUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.THUMB);
            } else {
                imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
                thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
            }
//            String imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
//            String thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
            markTaskDTO.setThumbSrc(thumbSrc);
            markTaskDTO.setImgSrc(imgSrc);
            markTaskDTO.setSample(paper.isSample());
            markTaskDTO.setMarkByLeader(paper.isMarkByLeader());
            markTaskDTO.setRandomSeqNew(markTask.getRandomSeqNew());
            markTaskDTO.setRandomSeq(markTask.getRandomSeq());
            markTaskDTO.setPaperId(paper.getId());
        }
        return markTaskDTO;
    }

    /**
     * 改档，改档打分
     * @param markTask
     * @return
     */
    public MarkTaskDTO toShiftDTO(MarkTask markTask) {

        MarkTaskDTO markTaskDTO = null;
        if (markTask != null) {
            markTaskDTO = new MarkTaskDTO();
            markTaskDTO.setId(markTask.getId());
            markTaskDTO.setRejected(markTask.isRejected());
            markTaskDTO.setMarkerId(markTask.getMarkerId());
            markTaskDTO.setMarker(markTask.getMarkerName());
            markTaskDTO.setUpdatedOn(markTask.getUpdatedOn());
            markTaskDTO.setResult(markTask.getResult());
            Paper paper = markTask.getPaper();
            if (paper.isShift()) {
                markTaskDTO.setLevel(markTask.getResult());
                markTaskDTO.setOriginLevel(markTask.getOriginLevel());
            }
            if(paper.isShiftScore()){
                markTaskDTO.setLevel(paper.getLevel());
            }
            markTaskDTO.setSn(markTask.getSecretNumber());
            markTaskDTO.setRedoLevel(paper.getRedoLevel());
            String imgSrc = null;
            String thumbSrc = null;
            String fileName = paper.getExamNumber();
            Student student = studentRepo.findByWorkIdAndExamNumberAndTest(paper.getWorkId(), paper.getExamNumber(), 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(paper.getWorkId(), paper.getSubject().ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
            }
            if (ParamCache.paramMap.get(paper.getWorkId()).getImageEncrypt() == 1) {
                imgSrc = systemConfig.getImageUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.IMAGE);
                thumbSrc = systemConfig.getThumbUrl(paper.getWorkId(), paper.getSubject().ordinal(), student.getId(), SystemConstant.THUMB);
            } else {
                imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
                thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), fileName);
            }
//            String imgSrc = systemConfig.getImageUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
//            String thumbSrc = systemConfig.getThumbUrl(markTask.getWorkId(), paper.getSubject().toString(), paper.getAreaCode(), paper.getExamNumber());
            markTaskDTO.setThumbSrc(thumbSrc);
            markTaskDTO.setImgSrc(imgSrc);
            markTaskDTO.setSample(paper.isSample());
            markTaskDTO.setMarkByLeader(paper.isMarkByLeader());
            markTaskDTO.setRandomSeqNew(markTask.getRandomSeqNew());
            markTaskDTO.setRandomSeq(markTask.getRandomSeq());
            markTaskDTO.setPaperId(paper.getId());
            markTaskDTO.setSerialNumber(markTask.getSerialNumber());
            if(Objects.nonNull(markTask.getDisplayNumber())) {
                markTaskDTO.setDisplayNumber(markTask.getDisplayNumber() == 1);
            }
            markTaskDTO.setShift(paper.isShift());
            markTaskDTO.setShiftScore(paper.isShiftScore());
        }
        return markTaskDTO;
    }
}