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

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.ImageUtils;
import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.ExportType;
import cn.com.qmth.examcloud.core.questions.base.word.DocxProcessUtil;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExamPaper;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExportStructure;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExtractConfig;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuestionTypeNum;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperExp;

import org.apache.commons.lang3.StringUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.stereotype.Service;

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

/**
 * @author chenken
 * @date 2017年7月7日 上午11:29:49
 * @company QMTH
 * @description 天津大学导出、上传文件service
 */
@Service("tjdxExportPaperService")
public class TjdxExportPaperService extends ExportPaperAbstractService {

    @Override
    public void downloadPaper(String paperId, String zipFileName, String examType) throws Exception {
        PaperExp paperExp = initPaperExp(paperId);
        if (paperExp != null) {
            String paperfileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_" + ExamFileType.PAPER.getName() + DOCX_SUFFIX;
            File file = new File(TEMP_FILE_EXP + File.separator + zipFileName + File.separator + paperfileName);
            List<WordprocessingMLPackage> wordPackages = getPkgList(paperId);
            if (examType.equals("offLine")) {
                //生成考试说明的word片段
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
                if (StringUtils.isBlank(paperExp.getExamRemark())) {
                    paperExp.setExamRemark("<p></p>");
                }
                String examRemark = ImageUtils.reSizeImg(paperExp.getExamRemark());
                paperExp.setExamRemarkWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(examRemark)));
                DocxProcessUtil.exportWordNew(paperExp, file, TJDX_OUTLINE_TEMPLATE_PAPER);
                if (DocxProcessUtil.hasImage(examRemark)) {
                    wordPackages.add(wordMLPackage);
                }
            } else {
                DocxProcessUtil.exportWordNew(paperExp, file, TJDX_TEMPLATE_PAPER);
            }
            DocxProcessUtil.processImage(zipFileName + File.separator + paperfileName, wordPackages);
            //下载音频
            super.downloadAudio(paperExp, zipFileName);
        }
    }

    @Override
    public void downloadPaperAnswer(String paperId, String zipFileName) throws Exception {
        PaperExp paperExp = initPaperExp(paperId);
        if (paperExp != null) {
            String answerFileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_" + ExamFileType.ANSWER.getName() + DOCX_SUFFIX;
            File file = new File(TEMP_FILE_EXP + File.separator + zipFileName + File.separator + answerFileName);
            DocxProcessUtil.exportWordNew(paperExp, file, TJDX_TEMPLATE_ANSWER);
            DocxProcessUtil.processImage(zipFileName + File.separator + answerFileName, getPkgList(paperId));
        }
    }

    @Override
    public void uploadFile(ExtractConfig extractConfig, String paperId, ExportStructure exportStructure, User accessUser, ExamPaper examPaper) throws Exception {
        PaperExp paperExp = initPaperExp(paperId);
        if (paperExp != null) {
            String currNum = CommonUtils.getCurNum();
            //没有试卷结构导出设置
            if (exportStructure == null) {
                //上传试卷
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, TJDX_TEMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, TJDX_TEMPLATE_ANSWER, ExamFileType.ANSWER, examPaper);
            } else if (exportStructure != null && exportStructure.getExportType() == ExportType.NORMAL) {
                List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
                //检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
                checkObjectiveDetailsNum(paperExp, questionTypeNums);
                //上传试卷
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, TJDX_TEMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, TJDX_TEMPLATE_ANSWER, ExamFileType.ANSWER, examPaper);
                //上传试卷结构   不能在上传试卷和答案之前
                uploadPaperStructure(paperExp, extractConfig, accessUser, currNum, questionTypeNums, examPaper);
            } else if (exportStructure != null && exportStructure.getExportType() == ExportType.ONLINE) {
                //上传机考JSON文件
                uploadComputerTestFile(extractConfig, accessUser, examPaper);
            }
        }
    }


}
