package gaf3.core.services.verify

import gaf3.core.cache.CacheService
import gaf3.core.cache.GafCacheConfigure
import gaf3.core.cache.MemCacheService
import gaf3.core.cache.RedisCacheService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.core.RedisTemplate

@Configuration
@ComponentScan(basePackageClasses = [GafVerifyConfiguration::class])
class GafVerifyConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "gaf.cache.verify")
    fun gafVerifyCacheConfigure(): GafCacheConfigure {
        return GafCacheConfigure();
    }

    @ConditionalOnProperty(prefix = "gaf.cache.verify", name = ["useRedis"], havingValue = "true", matchIfMissing = false)
    @Configuration
    class RedisCache {
        @Bean
        fun gafVerifyCacheService(template: RedisTemplate<String,String>, @Qualifier("gafVerifyCacheConfigure") config: GafCacheConfigure): CacheService {
            return RedisCacheService(template, config)
        }
    }

    @ConditionalOnProperty(prefix = "gaf.cache.verify", name = ["useRedis"], havingValue = "false", matchIfMissing = true)
    @Configuration
    class MemCache {
        @Bean
        fun gafVerifyCacheService(@Qualifier("gafVerifyCacheConfigure") config: GafCacheConfigure): CacheService {
            return MemCacheService(config)
        }
    }
}