package com.nitorcreations.willow.servers;

import java.util.Collection;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.elasticsearch.node.Node;

import com.nitorcreations.willow.messages.HostInfoMessage;
import com.nitorcreations.willow.messages.metrics.MetricConfig;
import com.nitorcreations.willow.metrics.HostInfoMetric;

public class HostInfoHostLookupService implements HostLookupService {

  private Logger logger = Logger.getLogger(this.getClass().getCanonicalName());

  @Inject
  Node node;
  @Inject
  HostInfoMetric hostInfoMetric;

  @Override
  public String getAdminUserFor(String tagHost) {
    logger.info(String.format("Resolving username for %s", tagHost));
    Collection<HostInfoMessage> hostInfoMessages = getHostInfoMessages(tagHost);
    for (HostInfoMessage msg : hostInfoMessages) {
      if (msg.getInstance().equals(tagHost)) {
        logger.info(String.format("Resolved %s username to %s", tagHost, msg.username));
        return msg.username;
      }
    }
    logger.info("Could not resolve username using HostInfo metric, returning system username");
    return System.getProperty("user.name");
  }

  @Override
  public String getResolvableHostname(String tagHost) {
    logger.info(String.format("Resolving hostname for %s", tagHost));
    Collection<HostInfoMessage> hostInfoMessages = getHostInfoMessages(tagHost);
    for (HostInfoMessage msg : hostInfoMessages) {
      if (msg.getInstance().equals(tagHost)) {
        logger.info(String.format("Resolved %s to %s", tagHost, msg.privateHostname));
        return msg.privateHostname; //TODO return public or private based on configuration
      }
    }
    logger.info("Could not resolve hostname using HostInfo metric, returning host tag as is.");
    return tagHost;
  }

  private Collection<HostInfoMessage> getHostInfoMessages(String tagHost) {
    MetricConfig metricConfig = new MetricConfig();
    metricConfig.setTags("host_" + tagHost);
    long now = System.currentTimeMillis();
    metricConfig.setStart(now - 90000);
    metricConfig.setStop(now);
    return hostInfoMetric.calculateMetric(metricConfig);
  }

  @Override
  public int getSshPort(String host) {
    return 22;
  }
}

@@ -41,137 +41,8 @@
 s;%0A%0A
-import java.util.Collection;%0Aimport java.util.logging.Logger;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.elasticsearch.node.Node;%0A%0A
 impo
@@ -96,16 +96,16 @@
 essage;%0A
+
 import c
@@ -218,16 +218,107 @@
 etric;%0A%0A
+import javax.inject.Inject;%0Aimport java.util.Collection;%0Aimport java.util.logging.Logger;%0A%0A
 public c
@@ -465,31 +465,8 @@
 );%0A%0A
-  @Inject%0A  Node node;%0A
   @I

Remove unused injection of elasticsearch node.