package com.apobates.forum.thrones.tag;

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

/**
 * 输出图片的缩略图路径
 *
 * @author xiaofanku
 * @since 20191103
 */
public class ThumbImagePathTag extends SimpleTagSupport {
    private String path;
    private String scale = "auto";
    //
    private final String imageBucketDomain;
    private final String uploadImageDirectName;
    
    public ThumbImagePathTag() {
        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;
        }
        UploadImagePathConvert ipc = new UploadImagePathConvert(imageBucketDomain, uploadImageDirectName);
        String imagePath = ipc.decode(path, ImageDirectoryEnum.DEFAT, scale).map(URL::toString).orElse("/static/img/140x140.png");
        getJspContext().getOut().print(imagePath);
    }
}