package com.apobates.forum.attention.core.decorater.posts;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.apobates.forum.core.entity.Posts;
import com.apobates.forum.utils.Commons;
import java.util.Objects;
import java.util.function.BiFunction;

/**
 * 话题回复装饰器
 *
 * @author xiaofanku
 * @since 20200516
 */
public final class ForumPostsDecorator {
    //是否显示
    //不显示或删除--->内容被删除(<div class="alert alert-danger" role="alert"><strong>提示:</strong> 作者被禁止发言或内容自动屏蔽</div>)
    //显示:
    //1):编辑日期(<div class="alert alert-warning" role="alert">该回复最后由 ${posts.getModifyer().getNickname()} 于 <forum:format date="${post.modifyDate}"/> 编辑</div>)
    //2):脏词过滤
    //3):表情解码
    //4):像册
    private final Posts posts;
    private final String content;
    private final static Logger logger = LoggerFactory.getLogger(ForumPostsDecorator.class);
    //public final static BiFunction<String, String, ForumPostsDecorator> EmptyScale = (imageSiteDomain, uploadFileDirectName) -> this.decorateUploadImage(imageSiteDomain, uploadFileDirectName, lazyLoad, null);
    /**
     * 初始装饰器
     *
     * @param posts 回复实例
     * @return
     */
    public static ForumPostsDecorator init(Posts posts) {
        Objects.requireNonNull(posts);
        return new ForumPostsDecorator(posts, null);
    }
    
    /**
     * [优先调用]设置不显示内容(删除或阻塞)时的提示消息
     *
     * @param blockTipMessage
     * @return
     */
    public ForumPostsDecorator block(String blockTipMessage) {
        if (!getPosts().isNormal()) {
            return new ForumPostsDecorator(null, blockTipMessage);
        }
        return this;
    }
    
    /**
     * 解码回复内容中的上传图片,图片的缩放为自动(auto)
     * 
     * @param lazyLoad 内容中的图片是否懒加载; true进行懒加载,false直接加载
     * @return 
     */
    public BiFunction<String, String, ForumPostsDecorator> decorateUploadImageEmptyScale(boolean lazyLoad) {
        return decorateUploadImageUseScale(lazyLoad, null);
    }
    
    /**
     * 解码回复内容中的上传图片,使用指定的图片缩放设置(scale)
     * 
     * @param lazyLoad 内容中的图片是否懒加载; true进行懒加载,false直接加载
     * @param scale 图片的缩放尺寸,支持: Width[x]Height, 要求Width,Height必须是正整数
     * @return 
     */
    public BiFunction<String, String, ForumPostsDecorator> decorateUploadImageUseScale(boolean lazyLoad, String scale) {
        return (imageSiteDomain, uploadFileDirectName) -> this.decorateUploadImage(imageSiteDomain, uploadFileDirectName, lazyLoad, scale);
    }
    
    /**
     * [最后调用]设置编辑提示消息
     *
     * @param modifyTipMessage
     * @return
     */
    public ForumPostsDecorator modify(String modifyTipMessage) {
        if (null == getPosts()) {
            return this;
        }
        
        if (null != posts.getModifyDateTime() && Commons.isNotBlank(posts.getModifyMemberNickname())) {
            String rs = (null == content) ? posts.getContent() : content;
            return new ForumPostsDecorator(posts, rs + "<br/>" + modifyTipMessage);
        }
        return this;
    }
    
    public String getContent() {
        return content;
    }
    
    private ForumPostsDecorator(Posts posts, String content) {
        this.posts = posts;
        this.content = content;
    }
    //[优先调用+1]执行敏感词过滤
    //ETC
    private Posts getPosts() {
        return posts;
    }
    /**
     * 解码回复内容中的上传图片
     *
     * @param imageSiteDomain 图片存储的域名,http://center.test.com
     * @param uploadFileDirectName 图片保存的目录名称
     * @param lazyLoad 内容中的图片是否懒加载; true进行懒加载,false直接加载
     * @param scale 图片的尺寸,若为null表示自动
     * @return
     */
    private ForumPostsDecorator decorateUploadImage(String imageSiteDomain, String uploadFileDirectName, boolean lazyLoad, String scale) {
        if (null == getPosts()) {
            return this;
        }
        
        if (null != content) { // 前面处理过一次了?
            posts.setContent(content);
        }
        logger.info("[SCALE][ForumPostsDecorator]image scale: " + scale);
        String rs = new PostsUploadImageDecodeContentDecorator(new PostsDisplay(posts), lazyLoad, scale, imageSiteDomain, uploadFileDirectName).getContent();
        return new ForumPostsDecorator(posts, rs);
    }
}