package weaver.interfaces.htsc.wpscenter.impl;


import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.wpscenter.convert.DocTypeConvert;
import weaver.interfaces.htsc.wpscenter.utils.Config;
import weaver.interfaces.htsc.wpscenter.utils.Tools;
import weaver.odoc.ofd.interf.OFDConvertor;
import weaver.odoc.ofd.util.ImageFileUtil;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

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

public class ConvertDocToOfd implements OFDConvertor {
    private Logger log = LoggerFactory.getLogger(ConvertDocToOfd.class);

    private User user;

    private Boolean acceptDoc = true;

    public ConvertDocToOfd(){
    }

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

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

    public Logger getLog() {
        return log;
    }

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

    public Boolean getAcceptDoc() {
        return acceptDoc;
    }

    public void setAcceptDoc(Boolean acceptDoc) {
        this.acceptDoc = acceptDoc;
    }

    @Override
    public Map convert(Set<Integer> imagefileIdSet) throws Exception {
        Map<Integer, Integer> ofdFiles = new HashMap<Integer, Integer>();

        if (null == imagefileIdSet || imagefileIdSet.isEmpty()) {
            throw new IllegalArgumentException(SystemEnv.getHtmlLabelName(389204, Tools.getUserLanguageid(user)) + " imagefileIdSet is null or empty");
        }

        //获取文件名集合
        Map<Integer, String> fileNameMap = ImageFileUtil.getIdAndNameReplation(imagefileIdSet);
        // 诸葛转换为ofd
        for (Integer imagefileId : imagefileIdSet) {
            if (imagefileId == null || imagefileId <= 0) {
                if (Config.isLogInfo()) {
                    log.info("WPS ConvertFileToOfd convert, imagefileId = null or imagefileId <= 0");
                }
                continue;
            }
            String fileName = fileNameMap.get(imagefileId);
            if (null == fileName || "".equals(fileName)) {
                if (Config.isLogInfo()) {
                    log.info("WPS ConvertFileToOfd convert, fileName is null");
                }
                continue;
            }

            int newImagefileId = convertToOfd(imagefileId, fileName, getAcceptDoc());
            if (newImagefileId > 0) {
                ofdFiles.put(imagefileId, newImagefileId);
            }
        }

        return ofdFiles;
    }

    @Override
    public Map convert(Set<Integer> imagefileIdSet, int requestid, User user) throws Exception {
        return null;
    }

    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 convertToOfd(paramsMap);
        }
        Map<String, Object> newParamsMap = new HashMap<String, Object>();
        newParamsMap.putAll(paramsMap);
        newParamsMap.put("fileId", clearfileId);
        newParamsMap.put("fromMould", "ecology");
        return convertToOfd(newParamsMap);
    }

    public int convertToOfd(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", "OFD");
        convertParam.put("targetFilename", targetFilename);
        DocTypeConvert baseConvert = new DocTypeConvert(user);
        return baseConvert.convertDocType(convertParam);
    }

    public int convertToOfd(int fileId, String filename, boolean acceptDoc) {
        if (Config.isLogInfo()) {
            log.info("WPS ConvertFileToOfd convertToOfd, fileId:[" + fileId + 
                "], filename:[" + filename + 
                "], acceptDoc:[" + acceptDoc + "]");
        }
        return convertToOfd(fileId, filename, acceptDoc, "ecology");
    }

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

        if (Config.isLogInfo()) {
            log.info("WPS ConvertFileToOfd convertToOfd, fileId:[" + fileId +
                "], filename:[" + filename +
                "], acceptDoc:[" + acceptDoc +
                "], fromMould:[" + fromMould + "]");
        }
        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", "OFD");
        convertParam.put("targetFilename", targetFilename);
        DocTypeConvert baseConvert = new DocTypeConvert(user);
        return baseConvert.convertDocType(convertParam);
    }
}


