package com.atlassian.bitbucket.rest.cluster;

import com.atlassian.bitbucket.cluster.ClusterNode;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.google.common.collect.ImmutableMap;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.net.InetSocketAddress;
import java.util.function.Function;

@JsonSerialize
@JsonSurrogate(ClusterNode.class)
public class RestClusterNode extends RestMapEntity {

    public static final Function<ClusterNode, RestClusterNode> REST_TRANSFORM = RestClusterNode::new;

    public RestClusterNode(ClusterNode node) {
        InetSocketAddress socketAddress = node.getAddress();

        put("id", node.getId());
        put("name", node.getName());
        put("address", ImmutableMap.of(
                "hostName", socketAddress.getHostString(), //Name -> String mismatch is intentional here
                "port", socketAddress.getPort()));
        put("local", node.isLocal());
    }
}
