package com.github.alexcojocaru.mojo.elasticsearch.v2.client;

import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import org.apache.maven.plugin.logging.Log;
import org.awaitility.Awaitility;
import org.mockito.Mockito;

/**
 * A monitor on an Elasticsearch instance.
 * 
 * @author Alex Cojocaru
 *
 */
public class Monitor
{
    private final ElasticsearchClient client;
    private final Log log;
    
    public Monitor(ElasticsearchClient client, Log log)
    {
        this.client = client;
        this.log = log;
    }
    
    public void waitToStartInstance(final String baseDir, final String clusterName, int timeout)
    {
        log.debug(String.format(
        		"Waiting  up to %ds for Elasticsearch instance to start ...",
        		timeout));
        Awaitility.await()
                .atMost(timeout, TimeUnit.SECONDS)
                .pollDelay(1, TimeUnit.SECONDS)
                .pollInterval(1,  TimeUnit.SECONDS)
                .until(new Callable<Boolean>()
                        {
                            @Override
                            public Boolean call() throws Exception
                            {
                                return isProcessRunning(baseDir)
                                        && isInstanceRunning(clusterName, client);
                            }
                        }
                );
        log.debug("Elasticsearch instance has started");
    }
    
    /**
     * Check whether the PID file created by the ES process exists or not.
     * @param baseDir the ES base directory
     * @return true if the process is running, false otherwise
     */
    public static boolean isProcessRunning(String baseDir)
    {
        File pidFile = new File(baseDir, "pid");
        boolean exists = pidFile.isFile();
        return exists;
    }
    
    /**
     * Check whether the cluster with the given name exists in the ES referenced by the client.
     * @param clusterName the ES cluster name
     * @param client the ES client to use to connect to ES
     * @return true if the instance is running, false otherwise
     */
    public static boolean isInstanceRunning(String clusterName, ElasticsearchClient client)
    {
        boolean result;
        try
        {
            @SuppressWarnings("unchecked")
            Map<String, Object> response = client.get("/", Map.class);
            result = clusterName.equals(response.get("cluster_name"));
        }
        catch (ElasticsearchClientException e)
        {
            // failure is allowed
            result = false;
        }
        
        return result;
    }
    
    /**
     * Check whether the cluster with the given name exists in the ES running on the given port.
     * @param clusterName the ES cluster name
     * @param httpPort the HTTP port to connect to ES
     * @return true if the instance is running, false otherwise
     */
    public static boolean isInstanceRunning(String clusterName, int httpPort)
    {
        Log log = Mockito.mock(Log.class);
        ElasticsearchClient client = new ElasticsearchClient(log, "localhost", httpPort);
        return isInstanceRunning(clusterName, client);
    }
    
    /**
     * Wait until the cluster has fully started (ie. all nodes have joined).
     * @param clusterName the ES cluster name
     * @param nodesCount the number of nodes in the cluster
     * @param timeout how many seconds to wait
     */
    public void waitToStartCluster(final String clusterName, int nodesCount, int timeout)
    {
        log.debug(String.format(
        		"Waiting  up to %ds for Elasticsearch cluster to start ...",
        		timeout));
        Awaitility.await()
                .atMost(timeout, TimeUnit.SECONDS)
                .pollDelay(1, TimeUnit.SECONDS)
                .pollInterval(1,  TimeUnit.SECONDS)
                .until(new Callable<Boolean>()
                        {
                            @Override
                            public Boolean call() throws Exception
                            {
                                return isClusterRunning(clusterName, nodesCount, client);
                            }
                        }
                );
        log.debug("Elasticsearch cluster has started");
    }
    
    /**
     * Verify that the cluster name and the number of nodes in the cluster,
     * as reported by the ES node, is as expected.
     * @param clusterName the ES cluster name
     * @param instanceCount the number of ES nodes in the cluster
     * @param client the ES client to use to connect to ES
     * @return true if the cluster is running, false otherwise
     */
	public static boolean isClusterRunning(String clusterName,
    		int nodes,
    		ElasticsearchClient client)
    {
        boolean result;
        try
        {
            @SuppressWarnings("unchecked")
			Map<String, Object> response = client.get("/_nodes", Map.class);
            result = clusterName.equals(response.get("cluster_name"));
            
            @SuppressWarnings("unchecked")
			Map<String, Object> nodesInfo = (Map<String, Object>)response.get("_nodes");
            result &= nodes == (int)(nodesInfo.get("successful"));
        }
        catch (ElasticsearchClientException e)
        {
            // failure is allowed
            result = false;
        }
        
        return result;
    }
    
    /**
     * Verify that the cluster name and the number of nodes in the cluster,
     * as reported by the ES node, is as expected.
     * @param clusterName the ES cluster name
     * @param instanceCount the number of ES nodes in the cluster
     * @param httpPort the HTTP port to connect to ES
     * @return true if the cluster is running, false otherwise
     */
    public static boolean isClusterRunning(String clusterName, int nodes, int httpPort)
    {
        Log log = Mockito.mock(Log.class);
        ElasticsearchClient client = new ElasticsearchClient(log, "localhost", httpPort);
        return isClusterRunning(clusterName, nodes, client);
    }
}

@@ -1436,37 +1436,36 @@
  );%0A        log.
-debug
+info
 (%22Elasticsearch 
@@ -4270,21 +4270,20 @@
     log.
-debug
+info
 (%22Elasti

change the log level on some of the log messages