package com.spotify.wasd.service;

import com.spotify.wasd.db.DatabaseHolder;
import com.spotify.wasd.db.Host;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Set;

@Slf4j
@Path("/find/hosts")
public class FindHostResource {
    private final DatabaseHolder holder;

    public FindHostResource(DatabaseHolder holder) {
        this.holder = holder;
    }

    @GET
    @Path("/unresolved")
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray getUnresolvedNames() {
        JSONArray namesArray = new JSONArray();

        Set<String> namesList = holder.current().getHosts().getUnresolvedNamesSet();
        for (String name: namesList) {
            namesArray.add(name);
        }

        return namesArray;
    }

    @GET
    @Path("/ring_failures")
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray getRingFailures() {
        JSONArray addrArray = new JSONArray();

        Set<Host> hostsList = holder.current().getHosts().getRingFailureHostsSet();
        for (Host host: hostsList) {
            addrArray.add(host.getAddress().toString());
        }

        return addrArray;
    }
}

@@ -1011,20 +1011,21 @@
 ONArray 
-addr
+names
 Array = 
@@ -1175,20 +1175,21 @@
         
-addr
+names
 Array.ad
@@ -1202,26 +1202,19 @@
 .get
-Address().toString
+ReverseName
 ());
@@ -1244,12 +1244,13 @@
 urn 
-addr
+names
 Arra

use reverse names in Cassandra ring failures