package com.apobates.forum.thrones.tag;

import com.apobates.forum.attention.core.decorater.ForumEntityIcoDecorder;
import com.apobates.forum.utils.Commons;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import java.net.URL;
import java.util.ResourceBundle;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 图标路径解码标签
 * 
 * @author xiaofanku
 * @since 20200530
 */
public class ForumEntityIcoPathTag extends SimpleTagSupport{
    private String path;
    private String scale="auto";
    //
    private final String imageBucketDomain;
    private final String uploadImageDirectName;
    
    public ForumEntityIcoPathTag() {
        ResourceBundle gisResource = ResourceBundle.getBundle("global");
        this.imageBucketDomain = gisResource.getString("img.bucket.domain");
        this.uploadImageDirectName = gisResource.getString("img.bucket.upload.direct");
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    public void setScale(String scale) {
        this.scale = scale;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
        if (!Commons.isNotBlank(path)) {
            getJspContext().getOut().print("/static/img/140x140.png");
            return;
        }
        URL icoURL = new ForumEntityIcoDecorder(imageBucketDomain, uploadImageDirectName).decode(path, scale).orElse(null);
        if(null == icoURL){
            icoURL = new URL(String.format("%s/%s/ico/default_icon.png", imageBucketDomain, uploadImageDirectName));
        }
        //
        getJspContext().getOut().print(icoURL.toString());
    }
}