package com.atlassian.bitbucket.mesh.config;

import org.springframework.core.env.Environment;

class DefaultTranscodeConfig implements TranscodeConfig {

    private static final String PROP_ADDRESS = "transcode.socket.address";
    private static final String PROP_PORT = "transcode.socket.port";

    private final Environment environment;

    private volatile Settings current;

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

    @Override
    public String getAddress() {
        return current.address;
    }

    @Override
    public int getPort() {
        return current.port;
    }

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

    private static class Settings {

        private final String address;
        private final int port;

        Settings(Environment environment) {
            address = environment.getProperty(PROP_ADDRESS);
            port = Math.max(0, environment.getProperty(PROP_PORT, Integer.class, 0));
        }
    }
}
