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.Pattern;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Pattern.Flag;

import lombok.Data;
import lombok.experimental.Accessors;
import me.qwq.doghouse.annotation.ConfigInfo;
import me.qwq.doghouse.cache.CacheConstants;
import me.qwq.doghouse.constants.UsefulRegex;
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="site", name="基本设置", initDefault=true)
public class SiteConfig implements Serializable, IConfig<SiteConfig>, IPostTypeConfig<SiteConfig> {

    private static final String GRAVATAR_URL_ADDRESS_INVALID = "Gravatar URL 地址不正确";
    private static final String ADDRESS_INVALID = "站点地址不正确";
    
    private static final String DEFAULT_NAME = "Doghouse";
    private static final String DEFAULT_DESCRIPTION = "Doghouse 个人博客系统";
    private static final String DEFAULT_ADDRESS = "http://localhost";
    private static final String DEFAULT_KEYWORDS = "Doghouse,个人博客,博客,狗洞,日记本";
    private static final String DEFAULT_COPYRIGHT = "Doghouse";
    private static final String DEFAULT_BEIAN_INFO = "";
    private static final Long DEFAULT_PAGE_SIZE = 3L;
    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;
    
    private String themeIdentity;

    @NotBlank
    private String description = DEFAULT_DESCRIPTION;

    @NotBlank
    @Pattern(regexp  = UsefulRegex.URL_WITH_SCHEME, 
    flags   = Flag.CASE_INSENSITIVE,
    message = ADDRESS_INVALID)
    private String address = DEFAULT_ADDRESS;

    private String keywords = DEFAULT_KEYWORDS;
    
    private String copyright = DEFAULT_COPYRIGHT;
    
    @NotBlank
    @Pattern(regexp  = UsefulRegex.URL_ENDS_WITH_SLASH, 
    flags   = Flag.CASE_INSENSITIVE,
    message = GRAVATAR_URL_ADDRESS_INVALID)
    private String gravatarUrl = "//sdn.geekzu.org/avatar/";
    
    private String beiAnInfo = DEFAULT_BEIAN_INFO;

    @NotNull
    @Size(min=1)
    private Long pageSize = DEFAULT_PAGE_SIZE;
    @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 = true;
    
    private Boolean rssOn = false;
    
    /**
     * 是否启用图片压缩
     */
    private Boolean imageCompress = false;
    
    @Override
    public SiteConfig 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.POST;
    }

    public Boolean getEnabled() {
        return true;
    }
    
    public String getMappingPrefix() {
        return "";
    }
}
