package com.atlassian.bitbucket.internal.stp;

import com.atlassian.bitbucket.cluster.ClusterNode;
import com.atlassian.bitbucket.cluster.ClusterService;
import com.atlassian.support.tools.spi.RootLevelSupportInfoAppender;
import com.atlassian.support.tools.spi.SupportInfoBuilder;

/**
 * Adds cluster information to the STP support information.
 */
public class ClusterSupportInfo extends RootLevelSupportInfoAppender {

    private final ClusterService clusterService;

    public ClusterSupportInfo(ClusterService clusterService) {
        this.clusterService = clusterService;
    }

    @Override
    protected void addSupportInfo(SupportInfoBuilder builder) {
        SupportInfoBuilder clusterBuilder = builder.addCategory(StpKeys.CLUSTER);
        clusterBuilder.addValue(StpKeys.CLUSTER_AVAILABLE, Boolean.toString(clusterService.isAvailable()));
        clusterBuilder.addValue(StpKeys.CLUSTER_CLUSTERED, Boolean.toString(clusterService.isClustered()));

        for (ClusterNode node : clusterService.getInformation().getNodes()) {
            SupportInfoBuilder nodeBuilder = clusterBuilder.addCategory(StpKeys.CLUSTER_NODE);
            nodeBuilder.addValue(StpKeys.CLUSTER_NODE_ID, node.getId());
            nodeBuilder.addValue(StpKeys.CLUSTER_NODE_ADDRESS, "" + node.getAddress());
            nodeBuilder.addValue(StpKeys.CLUSTER_NODE_LOCAL, Boolean.toString(node.isLocal()));

            nodeBuilder.addContext(node);
        }
    }
}
