package weaver.interfaces.htsc.wpscenter.utils;

import com.api.doc.detail.util.ImageConvertUtil;
import net.sf.json.JSONObject;
import weaver.general.Util;
import weaver.hrm.User;

public class WaterMarkerUtil {

    /**
     * 水印的内容是图片
     */
    public static final String WATERMARK_TYPE_IMAGE = "img";

    /**
     * 水印的内容是文本
     */
    public static final String WATERMARK_TYPE_TEXT = "txt";

    public static String WATER_TYPE_WORD = "word";
    public static String WATER_TYPE_PDF = "pdf";
    public static String WATER_TYPE_IMG = "img";

    /**
     * watermark: {
     *  type: 1, //水印类型， 0 为无水印； 1 为文字水印
     *  value: "禁止传阅", //文字水印的文字，当type 为1 时此字段必选
     *  fillstyle: "rgba( 192, 192, 192, 0.6 )", //水印的透明度，非必选，有默认值
     *  font: "bold 20px Serif", //水印的字体，非必选，有默认值
     *  rotate: -0.7853982, //水印的旋转度，非必选，有默认值
     *  horizontal: 50, //水印水平间距，非必选，有默认值
     *  vertical: 100 //水印垂直间距，非必选，有默认值
     * }
     * @param user
     * @return
     */
    public static JSONObject getWaterMarkerSetting(User user) {
        JSONObject watermarkSettings = new JSONObject();
        String watermark = new ImageConvertUtil().getWatermark(user, null, false);
        if(watermark.isEmpty()) {
            watermarkSettings.put("type", 0);
            return watermarkSettings;
        }
        watermarkSettings.put("type", 1);
        watermark = (watermark.startsWith("&watermark_txt=") || watermark.startsWith("?watermark_txt=")) ? watermark.substring(15) : watermark;
        watermarkSettings.put("value", watermark);

        if (Config.hasViewerWaterProp("fillstyle")) {
            watermarkSettings.put("fillstyle", Config.getViewerWaterProp("fillstyle"));
        }
        String font = Config.getViewerWaterProp("font");
        if (Config.hasViewerWaterProp("font")) {
            watermarkSettings.put("font", font);
        }
        String rotate = Config.getViewerWaterProp("rotate");
        if (Config.hasViewerWaterProp("rotate")) {
            watermarkSettings.put("rotate", Config.getViewerWaterProp("rotate"));
        }
        if (Config.hasViewerWaterProp("width")) {
            watermarkSettings.put("horizontal", Util.getIntValue(Config.getViewerWaterProp("width"), 50));
        }
        if (Config.hasViewerWaterProp("height")) {
            watermarkSettings.put("vertical", Util.getIntValue(Config.getViewerWaterProp("height"), 100));
        }

        return watermarkSettings;
    }
}
