package weaver.interfaces.htsc.wpscenter.convert;

import net.sf.json.JSONObject;
import weaver.hrm.User;
import weaver.interfaces.htsc.wpscenter.utils.*;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

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

public class BaseConvert {

    private Logger log = LoggerFactory.getLogger(BaseConvert.class);

    private TaskUtil task ;

    private User user;

    public BaseConvert(){
        task = new TaskUtil();
    }

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

    public User getUser() {
        return user;
    }

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

    public Logger getLog() {
        return log;
    }

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

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

        if(null == requestParams || requestParams.isEmpty()){
            throw new IllegalArgumentException("the argument requestParams can't be null or empty!");
        }
        TokenUtil tokenUtil = new TokenUtil();
        String appToken = tokenUtil.getFileFormatToken(user);
        apiUrl = apiUrl + "?app_token=" + appToken;

        HttpUtil httpUtil = new HttpUtil(user);
        requestParams.put("task_id", task.getTask_id());

        // 请求接口转换文档
        JSONObject response = httpUtil.post(apiUrl, requestParams, null);

        String code = Tools.getJsonData(response, "result");
        if(Config.isLogInfo()) {
            log.info("调用接口结果：" + response);
        }

        if(!"0".equals(code)){
            String message = Tools.getJsonData(response, "msg");
            result.put("response", response.toString());
            result.put("status", code);
            result.put("message", message);
            result.put("messageid", code);
            return response;
        }

        JSONObject taskResult = task.waitForTaskResult();
        code = Tools.getJsonData(taskResult, "result");
        if(!"0".equals(code)){
            return taskResult;
        }

        boolean onlyDownloadUrl = "true".equals(params.get("onlyDownloadUrl"));

        // 文件下载地址，预览时用不到
        String download = Tools.getJsonData(taskResult, "download_id");
        if("".equals(download)) {
            result.put("response", response.toString());
            result.put("status", "514617");
            result.put("message", SystemEnv.getHtmlLabelName(514617, Tools.getUserLanguageid(user)));
            result.put("messageid", "514617");
            return result;
        } else if(onlyDownloadUrl) {
            result.put("status", "0");
            result.put("downloadUrl", Config.getCpsV1DownloadFile() + Config.URL_SEPARATOR + download);
            return result;
        }

        int newImagefileId = httpUtil.getFileFromRomoteForWPSDoccenter(Config.getCpsV1DownloadFile() + Config.URL_SEPARATOR + download, targetFilename, null);
        if(newImagefileId > 0) {
            result.put("status", "0");
            result.put("imagefileid", newImagefileId+"");
        } else {
            result.put("response", response.toString());
            result.put("status", "514617");
            result.put("message", SystemEnv.getHtmlLabelName(514617, Tools.getUserLanguageid(user)));
            result.put("messageid", "514617");
        }
        return result;
    }
}
