package com.atlassian.bitbucket.installer.scripts;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Properties;

class ConfigFileGenerator {
    private Properties properties = new Properties();
    private Path path;

    ConfigFileGenerator(Path path) {
        this.path = path;
    }

    ConfigFileGenerator put(String name, String value) {
        properties.put(name, value);
        return this;
    }

    ConfigFileGenerator remove(String name) {
        properties.remove(name);
        return this;
    }

    ConfigFileGenerator mirrorMode() {
        return put(BitbucketConfig.KEY_APPLICATION_MODE, "mirror");
    }

    ConfigFileGenerator noMode() {
        return remove(BitbucketConfig.KEY_APPLICATION_MODE);
    }

    ConfigFileGenerator defaultMode() {
        return put(BitbucketConfig.KEY_APPLICATION_MODE, "default");
    }

    ConfigFileGenerator write() {
        try (OutputStream fos = Files.newOutputStream(path, StandardOpenOption.CREATE)) {
            properties.store(fos, null);
            return this;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
