package com.atlassian.bitbucket.mesh.config;

import org.springframework.core.env.Environment;

import static com.atlassian.bitbucket.mesh.rpc.ManagedConfigurationProperties.HOSTING_ALLOW_FILTER;
import static com.atlassian.bitbucket.mesh.rpc.ManagedConfigurationProperties.HOSTING_ATOMIC_INITIAL_PUSH;

class DefaultHostingConfig implements HostingConfig {

    private final Environment environment;

    private volatile Settings current;

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

    @Override
    public boolean isAtomicInitialPush() {
        return current.atomicInitialPush;
    }

    @Override
    public boolean isFilterAllowed() {
        return current.filterAllowed;
    }

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

    private static class Settings {

        private final boolean atomicInitialPush;
        private final boolean filterAllowed;

        Settings(Environment environment) {
            atomicInitialPush = environment.getProperty(HOSTING_ATOMIC_INITIAL_PUSH, Boolean.class, true);
            filterAllowed = environment.getProperty(HOSTING_ALLOW_FILTER, Boolean.class, true);
        }
    }
}
