package me.qwq.doghouse.entity.config;

import java.io.Serializable;

import org.springframework.cache.CacheManager;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.experimental.Accessors;
import me.qwq.doghouse.annotation.ConfigInfo;
import me.qwq.doghouse.cache.CacheConstants;
import me.qwq.doghouse.enums.PostTypeEnum;
import me.qwq.doghouse.interfaces.IConfig;
import me.qwq.doghouse.interfaces.IPostTypeConfig;
import me.qwq.doghouse.util.SpringContextHolder;

@SuppressWarnings("serial")
@Data
@Accessors(chain=true)
@ConfigInfo(field="page", name="独立页面设置", initDefault=true, managed=false)
public class PageConfig implements Serializable, IConfig<PageConfig>, IPostTypeConfig<PageConfig> {

    private static final String DEFAULT_NAME = "Doghouse";
    private static final String DEFAULT_DESCRIPTION = "Doghouse 个人博客系统";
    private static final Long DEFAULT_COMMENT_PAGE_SIZE = 5L;
    private static final Long DEFAULT_RECENT_SIZE = 6L;
    private static final Long DEFAULT_POPULAR_SIZE = 6L;
    private static final Long DEFAULT_TAG_SIZE = 8L;
    private static final Long DEFAULT_RECENT_COMMENT_SIZE = 6L;
    private static final Integer DEFAULT_RECENT_COMMENT_LEVEL = 3;
    
    @NotBlank
    private String name = DEFAULT_NAME;

    @NotBlank
    private String description = DEFAULT_DESCRIPTION;
    
    @NotNull
    @Size(min=0)
    private Long commentPageSize = DEFAULT_COMMENT_PAGE_SIZE;
    @NotNull
    @Size(min=0)
    private Long recentSize = DEFAULT_RECENT_SIZE;
    @NotNull
    @Size(min=0)
    private Long popularSize = DEFAULT_POPULAR_SIZE;
    @NotNull
    @Size(min=0)
    private Long tagSize = DEFAULT_TAG_SIZE;
    @NotNull
    @Size(min=0)
    private Long recentCommentSize = DEFAULT_RECENT_COMMENT_SIZE;
    @NotNull
    @Size(min=0)
    private Integer recentCommentLevel = DEFAULT_RECENT_COMMENT_LEVEL;
    
    private Boolean addToSitemap = true;
    
    private Boolean addToFeed = false;

    private Boolean rssOn = false;
    
    private Boolean imageCompress = false;
    
    @Override
    public PageConfig afterSaving() {
        // 缓存驱逐：GET_JUMP_TO_URL
        // 因为具体是哪一页是和每页评论数有关的，每页评论数又在 SiteConfig 里定义
        // 一旦更改，那么评论对应页的缓存得驱逐
        ((CacheManager)SpringContextHolder.getBean("cacheManager")).getCache(CacheConstants.Comments.GET_JUMP_TO_URL).clear();
        return this;
    }
    
    public PostTypeEnum getPostType() {
        return PostTypeEnum.PAGE;
    }

    public Boolean getEnabled() {
        return true;
    }
    
    /**
     * 独立页面不需要 PageSize
     */
    public Long getPageSize() {
        return 0L;
    }
    
    public String getMappingPrefix() {
        return "";
    }
}
