package com.atlassian.bitbucket.internal.ssh.stp;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.ssh.SshConfiguration;
import com.atlassian.bitbucket.ssh.SshConfigurationService;
import com.atlassian.support.tools.spi.RootLevelSupportInfoAppender;
import com.atlassian.support.tools.spi.SupportInfoBuilder;
import org.apache.commons.lang3.StringUtils;

public class SshPluginInfo extends RootLevelSupportInfoAppender {

    private static final String SSH = "stp.properties.stash.ssh";
    private static final String SSH_ACCESS_KEYS = SSH + ".access-keys";
    private static final String SSH_BASE_URL = SSH + ".base-url";
    private static final String SSH_ENABLED = SSH + ".enabled";
    private static final String SSH_NOT_CONFIGURED = SSH + ".not.configured";
    private static final String SSH_PORT = SSH + ".port";

    private final SshConfigurationService configurationService;
    private final I18nService i18nService;

    public SshPluginInfo(SshConfigurationService configurationService, I18nService i18nService) {
        this.configurationService = configurationService;
        this.i18nService = i18nService;
    }

    @Override
    public void addSupportInfo(SupportInfoBuilder builder) {
        SshConfiguration configuration = configurationService.getConfiguration();

        builder.addCategory(SSH)
                .addValue(SSH_ENABLED, String.valueOf(configuration.isEnabled()))
                .addValue(SSH_PORT, String.valueOf(configuration.getPort()))
                .addValue(SSH_BASE_URL,
                        StringUtils.defaultIfEmpty(configuration.getBaseUrl(), i18nService.getMessage(SSH_NOT_CONFIGURED)))
                .addCategory(SSH_ACCESS_KEYS)
                    .addValue(SSH_ENABLED, String.valueOf(configuration.isAccessKeysEnabled()));
    }
}
