package weaver.interfaces.htsc.wpscenter.convert;

import net.sf.json.JSONArray;
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.interfaces.htsc.wpscenter.utils.WaterMarkerUtil;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

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

public class OfficeOperate {

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

    private User user;

    /**
     * 文档清稿
     */
    public static final String OPERATE_CLEAN = "OFFICE_CLEAN";

    /**
     * 添加水印
     */
    public static final String OPERATE_WATERMARK = "OFFICE_WATERMARK";

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

    public User getUser() {
        return user;
    }

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

    public Logger getLog() {
        return log;
    }

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

    public Map<String, String> clearDoc(Map<String, String> params) throws Exception {
        JSONArray steps = new JSONArray();
        steps.add(getCleanDocStep());
        return convert(params, steps);
    }

    public Map<String, String> addWaterMark(Map<String, String> params, Map<String, Object> water) throws Exception {
        JSONArray steps = new JSONArray();
        steps.add(getDocWatermarkStep(params, water));
        return convert(params, steps);
    }

    public Map<String, String> convert(Map<String, String> params, JSONArray steps) throws Exception {
        if(null == steps || steps.isEmpty()) {
            Map<String, String> result = new HashMap<String, String>();
            result.put("response", "");
            result.put("status", "-1");
            // TODO 设置多语言
            result.put("message", "未知的操作");
            result.put("messageid", "-1");
            return result;
        }

        String ecfileid = params.get("imagefileId");
        int ecfileidInt = Util.getIntValue(ecfileid);
        String fileName = params.get("fileName");
        fileName = Tools.isEmptyOrNull(fileName) && params.containsKey("filename") ? params.get("filename") : fileName;
        // 判断模块。目前应该只有知识管理的
        String fromMould = Tools.getFromMould(params.get("fromMould"));

        String apiUrl = Config.getCpsV1OfficeOperate();

        JSONObject requestParams = new JSONObject();
        requestParams.put("doc_url", Tools.getDownloadUrl(ecfileidInt, fromMould));
        requestParams.put("doc_filename", Tools.getFileNameWithLowerCaseSuffix(fileName));
        requestParams.put("steps", steps);

        BaseConvert baseConvert = new BaseConvert(user);
        return baseConvert.convert(apiUrl, requestParams, fileName, params);
    }

    public JSONObject getCleanDocStep(){
        JSONObject step = new JSONObject();
        step.put("operate", OPERATE_CLEAN);
        step.put("args", new JSONObject());
        return step;
    }

    public JSONObject getDocWatermarkStep(Map<String, String> params, Map<String, Object> water){
        JSONObject step = new JSONObject();
        JSONObject args = new JSONObject();

        step.put("operate", OPERATE_WATERMARK);

        if(WaterMarkerUtil.WATERMARK_TYPE_TEXT.equals(params.get("type"))) {
            String content = params.get("content");
            JSONObject watermarkSetting = new JSONObject();
            watermarkSetting.put("content", content);

            /*if(null != water && !water.isEmpty()) {
                if(water.containsKey("size")){
                    watermarkSetting.put("size", Util.getIntValue(Util.null2String(water.get("size"))));
                }
                if(water.containsKey("color")) {
                    watermarkSetting.put("color", Util.null2String(water.get("color")));
                }
                if(water.containsKey("transparent")) {
                    watermarkSetting.put("transparent", Util.getIntValue(Util.null2String(water.get("transparent"))));
                }
                if(water.containsKey("tilt")) {
                    watermarkSetting.put("tilt", "true".equals(Util.null2String(water.get("tilt"))));
                }
                if(water.containsKey("position")) {
                    watermarkSetting.put("position", Util.null2String(water.get("position")));
                }
                if(water.containsKey("tiled")) {
                    watermarkSetting.put("tiled", "true".equals(Util.null2String(water.get("tiled"))));
                }
            } else {*/
                if(Config.hasTextWaterProp("size")){
                    watermarkSetting.put("size", Util.getIntValue(Config.getTextWaterProp("size")));
                }
                if(Config.hasTextWaterProp("color")){
                    watermarkSetting.put("color", Config.getTextWaterProp("color"));
                }
                if(Config.hasTextWaterProp("transparent")){
                    watermarkSetting.put("transparent", Util.getFloatValue(Config.getTextWaterProp("transparent")));
                }
                if(Config.hasTextWaterProp("tilt")){
                    watermarkSetting.put("tilt", "true".equals(Config.getTextWaterProp("tilt")));
                }
                if(Config.hasTextWaterProp("position")){
                    watermarkSetting.put("position", Config.getTextWaterProp("position"));
                }
                if(Config.hasTextWaterProp("tiled")){
                    watermarkSetting.put("tiled", "true".equals(Config.getTextWaterProp("tiled")));
                }
            /*}*/

            args.put("text_watermark", watermarkSetting);
        } else if(WaterMarkerUtil.WATERMARK_TYPE_IMAGE.equals(params.get("type"))) {
            String picId = params.get("picid");
            int picIdInt = Util.getIntValue(picId);
            String fromMould = Tools.getFromMould(params.get("fromMould"));

            JSONObject watermarkSetting = new JSONObject();

            FileInfoUtil fileInfoUtil = new FileInfoUtil();
            Map<String, String> fileInfo = fileInfoUtil.getFileInfo(picId,null,fromMould);
            watermarkSetting.put("watermark_url", fileInfo.get("download"));
            watermarkSetting.put("watermark_filename", Tools.getFileNameWithLowerCaseSuffix(fileInfo.get("filename")));
            /*if(null != water && !water.isEmpty()) {
                if(water.containsKey("no_washout")) {
                    watermarkSetting.put("no_washout", "true".equals(Util.null2String(water.get("no_washout"))));
                }
                if(water.containsKey("tilt")) {
                    watermarkSetting.put("tilt", "true".equals(Util.null2String(water.get("tilt"))));
                }
                if(water.containsKey("position")) {
                    watermarkSetting.put("position", Util.null2String(water.get("position")));
                }
                if(water.containsKey("tiled")) {
                    watermarkSetting.put("tiled", "true".equals(Util.null2String(water.get("tiled"))));
                }
                if(water.containsKey("scale")) {
                    watermarkSetting.put("scale", Util.getIntValue(Util.null2String(water.get("scale"))));
                }
            } else {*/
                //  "watermark_url": "http://10.226.20.105/wpsupdate.png", //类型：String 必有字段 备注：水印图片地址
                //  "watermark_filename": "水印.jpg", //类型：String 必有字段 备注： 图片水印的文件名， 必须带后缀
                //  "no_washout": false, //类型：Boolean 可有字段 备注：是否取消冲蚀
                //  "tilt": true, //类型：Boolean 可有字段 备注：是否倾斜 45 度
                //  "position": "CENTER", //类型：String 可有字段 备注： 水印位置，
                //  //枚举： TOP_LEFT | TOP_CENTER | TOP_RIGHT | CENTER_LEFT | CENTER | CENTER_RIGHT | BOTTOM_LEFT | BOTTOM_CENTER | BOTTOM_RIGHT
                //  "tiled": true, //类型：Boolean 可有字段 备注：水印是否平铺
                //  "scale": 2 //类型：Number 可有字段备注：水印图片缩放比例，0.1-5

                if(Config.hasImageWaterProp("no_washout")){
                    watermarkSetting.put("no_washout", "true".equals(Config.getImageWaterProp("no_washout")));
                }
                if(Config.hasImageWaterProp("tilt")){
                    watermarkSetting.put("tilt", "true".equals(Config.getImageWaterProp("tilt")));
                }
                if(Config.hasImageWaterProp("position")){
                    watermarkSetting.put("position", Config.getImageWaterProp("position"));
                }
                if(Config.hasImageWaterProp("tiled")){
                    watermarkSetting.put("tiled", "true".equals(Config.getImageWaterProp("tiled")));
                }
                if(Config.hasImageWaterProp("scale")){
                    watermarkSetting.put("scale", Util.getFloatValue(Config.getImageWaterProp("scale")));
                }
            /*}*/

            args.put("image_watermark", watermarkSetting);
        }

        step.put("args", args);
        return step;
    }
}
