package com.atlassian.bitbucket.mesh.config;

import org.springframework.core.env.Environment;

import javax.annotation.Nonnull;
import java.time.Duration;

class DefaultHookScriptsConfig implements HookScriptsConfig {

    private static final String PROP_GC_PRUNE = "hookscripts.gc.prune";

    private final Environment environment;

    private volatile Settings current;

    DefaultHookScriptsConfig(Environment environment) {
        this.environment = environment;
        current = new Settings(environment);
    }

    @Nonnull
    @Override
    public Duration getGcPrune() {
        return current.gcPrune;
    }

    void reload() {
        current = new Settings(environment);
    }

    private static class Settings {

        private final Duration gcPrune;

        Settings(Environment environment) {
            gcPrune = Duration.ofHours(environment.getProperty(PROP_GC_PRUNE, Long.class, 168L));
        }
    }
}
