package cn.com.qmth.examcloud.core.questions.service.export;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.ExportTemplateType;
import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.DownloadPaperDto;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailExp;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperExp;
import cn.com.qmth.examcloud.core.questions.service.util.ExportPaperUtil;

/**
 * @author chenken
 * @date 2017年7月7日 上午11:29:49
 * @company QMTH
 * @description 华中科技大学导出、上传文件service
 */
@Service("hzkjExportPaperService")
public class HzkjExportPaperService extends ExportPaperAbstractService {

    @Override
    public void downloadPaper(DownloadPaperDto dto) throws Exception {
        PaperExp paperExp = initPaperExp(dto);
        if (paperExp != null) {
        	setExportQuestionTitle(paperExp.getPaperDetails(), true);
            String paperfileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_"
                    + ExamFileType.PAPER.getName() + DOCX_SUFFIX;
            File directory = new File(TEMP_FILE_EXP + File.separator + dto.getZipFileName());
            ExportPaperUtil.createPaperDocFile(dto.getRootOrgId(),paperExp, directory,paperfileName, ExportTemplateType.PAPER_EXPORT);
        }
    }



    @Override
    public void setExportQuestionTitle(List<PaperDetailExp> details, boolean isWord) {
        if (CollectionUtils.isEmpty(details)) {
            return;
        }

        final String style = "<em class=\"e_num\">%s</em>";

        for (PaperDetailExp detail : details) {
            String perQuesScore = checkPaperDetailUnitScore(detail);
            if (perQuesScore != null) {
                perQuesScore = CommonUtils.clearZeroPoint(perQuesScore);
                if (!isWord) {
                    perQuesScore = String.format(style, perQuesScore);
                }
                perQuesScore = String.format("每小题%s分，", perQuesScore);
            } else {
                perQuesScore = "";// 每题分数不一致则不注明
            }

            QuesStructType type = detail.getPaperDetailUnits().get(0).getQuestionType();
            String detailScore = CommonUtils.formatValue(detail.getScore());
            String unitCount = String.valueOf(detail.getUnitCount());

            if (!isWord) {
                detailScore = String.format(style, detailScore);
                unitCount = String.format(style, unitCount);
            }
            String title = String.format("（本大题共%s小题，%s共%s分）", unitCount, perQuesScore, detailScore);

            if (type == QuesStructType.SINGLE_ANSWER_QUESTION) {
                detail.setTitleDetail("在每小题列出的备选项中只有一个符合题目要求的，请将其选出并将“答题卡”的相应代码涂黑，错涂、多涂或未涂均无分");
                detail.setName("单项选择题");
            } else if (type == QuesStructType.MULTIPLE_ANSWER_QUESTION) {
                detail.setTitleDetail("在每小题列出的备选项中有一个或多个符合题目要求的，请将符合项选出并将“答题卡”的相应代码涂黑");
                detail.setName("多项选择题");
            } else if (type == QuesStructType.BOOL_ANSWER_QUESTION) {
                detail.setTitleDetail("正确的填涂√、错误填涂×。错涂、多涂或未涂均无分");
                detail.setName("判断题");
            } else {
                detail.setName(type.getName());
            }

            detail.setTitle(title);
        }
    }

}