package weaver.interfaces.htsc.wpscenter.impl;

import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.wpscenter.DocumentConvertInterface;
import weaver.interfaces.htsc.wpscenter.convert.DocTypeConvert;
import weaver.interfaces.htsc.wpscenter.utils.Config;
import weaver.interfaces.htsc.wpscenter.utils.Tools;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class ConvertDocToPdf implements DocumentConvertInterface {
    private Logger log = LoggerFactory.getLogger(ConvertDocToPdf.class);



    private User user;

    public ConvertDocToPdf(){
    }

    public ConvertDocToPdf(User user){
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public ConvertDocToPdf setUser(User user) {
        this.user = user;
        return this;
    }

    public Logger getLog() {
        return log;
    }

    public ConvertDocToPdf setLog(Logger log) {
        this.log = log;
        return this;
    }

    @Override
    public int ConvertToPDF(Map<String, Object> paramsMap) {
        if(Config.isLogInfo()) {
            log.info("paramsMap:" + paramsMap.toString());
        }

        if(null == user) {
            user = paramsMap.containsKey("user") ? (User)paramsMap.get("user") : null;
        }

        int fileId = (int)paramsMap.get("fileId");

        String fileName = Util.null2String((String)paramsMap.get("fileName"));
        String fromMould = Tools.getFromMould(Util.null2String((String)paramsMap.get("fromMould")));
        String targetFilename = Util.null2String((String)paramsMap.get("targetFilename"));

        Map<String, String> convertParam = new HashMap<String, String>();
        convertParam.put("imagefileid", fileId+"");
        convertParam.put("fromMould", fromMould);
        convertParam.put("fileName", fileName);
        convertParam.put("targetFileFormat", "PDF");
        convertParam.put("targetFilename", targetFilename);
        DocTypeConvert baseConvert = new DocTypeConvert(user);
        return baseConvert.convertDocType(convertParam);
    }

    /**
     * 清稿后再转pdf，目前WPS的服务不支持清稿和删除批注分开操作，直接复用全部删除的逻辑
     * @param paramsMap
     * @return
     */
    @Override
    public int ConvertToPDFWithoutRevision(Map<String, Object> paramsMap) {
        if(Config.isLogInfo()) {
            log.info("paramsMap:" + paramsMap.toString());
        }
        return ConvertToPDFWithoutRevisionAndComment(paramsMap);
    }

    @Override
    public int ConvertToPDFWithoutRevisionAndComment(Map<String, Object> paramsMap) {
        if(Config.isLogInfo()) {
            log.info("paramsMap:" + paramsMap.toString());
        }

        if(null == user) {
            user = paramsMap.containsKey("user") ? (User)paramsMap.get("user") : null;
        }

        int fileId = (int)paramsMap.get("fileId");
        ClearRevisionAndComents clearRevisionAndComents = new ClearRevisionAndComents().setUser(user);
        Map<String, Object> clearDocParamsMap = new HashMap<String, Object> ();
        clearDocParamsMap.put("imagefileId", fileId);
        clearDocParamsMap.put("fileName", paramsMap.get("fileName"));
        clearDocParamsMap.put("fromMould", paramsMap.get("fromMould"));
        clearDocParamsMap.put("targetFilename", paramsMap.get("targetFilename"));
        int clearfileId = clearRevisionAndComents.acceptRevisions(clearDocParamsMap);
        if(clearfileId <= 0) {
            return ConvertToPDF(paramsMap);
        }
        Map<String, Object> newParamsMap = new HashMap<String, Object>();
        newParamsMap.putAll(paramsMap);
        newParamsMap.put("fileId", clearfileId);
        newParamsMap.put("fromMould", "ecology");
        return ConvertToPDF(newParamsMap);
    }

    public int ConvertToPDF(int fileId, String filename, boolean acceptDoc) {
        return ConvertToPDF(fileId, filename, acceptDoc, "ecology");
    }

    public int ConvertToPDF(int fileId, String filename, boolean acceptDoc, String fromMould) {
        String targetFilename = (!Tools.isEmptyOrNull(filename) && filename.contains(".") ?
            filename.substring(0, filename.lastIndexOf(".")) : filename) + ".pdf";

        if(acceptDoc){
            Map<String, Object> paramsMap = new HashMap<String, Object> ();
            paramsMap.put("fileId", fileId);
            paramsMap.put("fileName", filename);
            paramsMap.put("fromMould", fromMould);
            paramsMap.put("targetFilename", targetFilename);
            return ConvertToPDFWithoutRevisionAndComment(paramsMap);
        }

        Map<String, String> convertParam = new HashMap<String, String>();
        convertParam.put("imagefileid", fileId+"");
        convertParam.put("fromMould", fromMould);
        convertParam.put("fileName", filename);
        convertParam.put("targetFileFormat", "PDF");
        convertParam.put("targetFilename", targetFilename);

        DocTypeConvert baseConvert = new DocTypeConvert(user);
        return baseConvert.convertDocType(convertParam);
    }
}
