package com.elab.redis.componts.config;

import com.elab.redis.componts.config.scope.RedisConfigRefreshCallback;
import com.elab.redis.componts.config.scope.RefreshConfigScopeRegistry;
import com.elab.redis.consts.CacheConstants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
@ConditionalOnProperty(prefix = "spring.redis.elab.config-refresh", value = "enable", havingValue = "true")
public class RedisRefreshConfigure {

    @Bean
    public RefreshConfigScopeRegistry refreshConfigScopeRegistry() {
        return new RefreshConfigScopeRegistry();
    }

    @Bean(CacheConstants.REFRESH_CONFIG_SCOPE_NAME)
    @ConditionalOnBean(RefreshConfigScopeRegistry.class)
    public RedisConfigRefreshCallback refreshConfig() {
        return new RedisConfigRefreshCallback();
    }

    @Bean
    @ConditionalOnBean({RedisConfigRefreshCallback.class})
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        return new RedisMessageListenerContainer();
    }

    @Bean
    @ConditionalOnBean(name = {CacheConstants.REFRESH_CONFIG_SCOPE_NAME})
    public ConfigCenterRedisStater configCenterRedisStater() {
        return new ConfigCenterRedisStater();
    }
}
