package me.qwq.doghouse.service.post;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import me.qwq.doghouse.annotation.RequireSession;
import me.qwq.doghouse.entity.Post;
import me.qwq.doghouse.entity.config.TweetConfig;

/**
 * 文章服务
 *
 * @author Doghole
 */
@Service
@Slf4j
public class TweetService extends AbstractPostService<TweetService, TweetConfig> {

    @Override
    @RequireSession
    public void beforeSaving(
            Post post,
            Long[] assets,
            String[] tagsName,
            Long[] categoriesId,
            Boolean isUpdate) {
        super.beforeSaving(post, assets, tagsName, categoriesId, isUpdate);
        if (!isUpdate) {
            // Tweet 填充 title
            post.setPostTitle("Untitled");
        }
    }
    
    @Override
    public void afterSaving(
            Post post,
            Long[] assets,
            String[] tagsName,
            Long[] categoriesId,
            Boolean isUpdate) {
        
        if (isUpdate) {
            // 重设 tweet title
            post.setPostTitle("#" + post.getPostId());
            proxy().saveOrUpdate(post);
        }
        super.afterSaving(post, assets, tagsName, categoriesId, isUpdate);
        
    }
    
    @Override
    public String getFrontEndPostTitle(Post post) {
        if (StringUtils.isEmpty(post.getPassword())) {
            return "#" + post.getPostId();
        }
        return super.getFrontEndPostTitle(post);
    }
}
