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

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

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.stereotype.Service;

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.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;


/**
 * @author weiwenhai
 * @date 2018年7月25日 上午11:29:49
 * @company QMTH
 * @description 中国地质（武汉）大学导出、上传文件service ZGDDW_TMPLATE_PAPER，ZGDDW_TMPLATE_ANSWER
 */
@Service("zgddwExportPaperService")
public class ZgddwExportPaperService extends ExportPaperAbstractService {

    @Override
    public void downloadPaper(String paperId, String zipFileName, String examType) throws Exception {
        PaperExp paperExp = super.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);
            if (examType.equals("offLine")) {
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
                paperExp.setExamRemarkWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(paperExp.getExamRemark())));
                DocxProcessUtil.exportWordNew(paperExp, file, ZGDDW_TMPLATE_PAPER);
            } else {
                DocxProcessUtil.exportWordNew(paperExp, file, ZGDDW_TMPLATE_PAPER);
            }
            DocxProcessUtil.processImage(zipFileName + File.separator + paperfileName, getPkgList(paperId));
            //下载音频
            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, ZGDDW_TMPLATE_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();
            //删除原有试卷文件
        	/*ExamFile examFile = new ExamFile();
        	examFile.setExamId(extractConfig.getExamId()+"");
        	examFile.setCourseId(extractConfig.getCourseCode());
        	examFile.setOrgId(extractConfig.getOrgId());
        	examFileService.deleteExamFile(examFile);*/
            //没有试卷结构导出设置
            if (exportStructure == null) {
                //上传试卷
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, ZGDDW_TMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, ZGDDW_TMPLATE_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, ZGDDW_TMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, ZGDDW_TMPLATE_ANSWER, ExamFileType.ANSWER, examPaper);
                //上传试卷结构   不能在上传试卷和答案之前
                uploadPaperStructure(paperExp, extractConfig, accessUser, currNum, questionTypeNums, examPaper);
            } else if (exportStructure != null && exportStructure.getExportType() == ExportType.ONLINE) {
                //上传机考JSON文件
                uploadComputerTestFile(extractConfig, accessUser, examPaper);
            }
        }
    }


}
