package weaver.interfaces.htsc.wpscenter.convert;

import net.sf.json.JSONObject;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.wpscenter.utils.Config;
import weaver.interfaces.htsc.wpscenter.utils.FileInfoUtil;
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 DocTypeConvert {
    private Logger log = LoggerFactory.getLogger(DocTypeConvert.class);

    private User user;

    public DocTypeConvert(){
    }

    /**
     * 构造函数，用户基本上是用于获取多语言
     * @param user
     */
    public DocTypeConvert(User user){
        this.user = user;
    }

    public User getUser() {
        return user;
    }

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

    public Logger getLog() {
        return log;
    }

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

    public Map<String, String> convert(Map<String, String> params) throws Exception {
        Map<String, String> result = new HashMap<String, String>();
        JSONObject requestParams = new JSONObject();

        String ecfileid = params.get("imagefileid");
        // 判断模块。目前应该只有知识管理的
        String fromMould = Tools.null2String(params.get("fromMould"));
        fromMould = Tools.getFromMould(fromMould);

        String targetFileFormat = Tools.null2String(params.get("targetFileFormat"));
        String apiUrl = Config.getCpsV1OfficeConvert();
        FileInfoUtil fileInfoUtil = new FileInfoUtil();
        Map<String,String> fileInfo = fileInfoUtil.getFileInfo(ecfileid, user, fromMould);

        String filename = fileInfo.get("filename");

        requestParams = new JSONObject();
        requestParams.put("doc_filename", Tools.getFileNameWithLowerCaseSuffix(filename));
        requestParams.put("doc_url", fileInfo.get("download"));
        requestParams.put("target_file_format", targetFileFormat);

        // 结果文件的文件名
        String targetFilename = params.containsKey("targetFilename") ? params.get("targetFilename"): "";
        if(Tools.isEmptyOrNull(targetFilename) && !Tools.isEmptyOrNull(filename)) {
            String nameOnly = filename.contains(".") ? filename.substring(0, filename.lastIndexOf(".")) : filename;
            targetFilename = nameOnly + "." + targetFileFormat.toLowerCase();
        }
        
        BaseConvert baseConvert = new BaseConvert(user);
        result = baseConvert.convert(apiUrl, requestParams, targetFilename, params);
        // 处理其他的事务
        return result;
    }

    public int convertDocType(Map<String, String> params){
        Map<String, String> result = new HashMap<String, String>();
        try {
            result = convert(params);
            if("0".equals(result.get("status"))){
                return Util.getIntValue(result.get("imagefileid"));
            }
        } catch (Exception e) {
            log.error(e);
        }
        return -1;
    }
}
