// Copyright 2014 Boundary, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.boundary.metrics.vmware.client.client.metrics;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.async.TypeListener;
import com.sun.jersey.core.util.Base64;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.google.common.base.Preconditions.checkNotNull;

public class MetricsClient {

    private final WebResource baseResource;
    private final AsyncWebResource asyncWebResource;
    private final String authentication;

    private static final Joiner PATH_JOINER = Joiner.on('/');
    private static final Logger LOG = LoggerFactory.getLogger(MetricsClient.class);

    /**
     * Constructor
     * @param client Jersey client
     * @param baseUrl URI to make REST call
     * @param authentication authentication string
     */
    public MetricsClient(Client client, URI baseUrl, String authentication) {
        checkNotNull(client);
        checkNotNull(baseUrl);
        this.baseResource = client.resource(baseUrl);
        this.asyncWebResource = client.asyncResource(baseUrl);
        this.authentication = checkNotNull(authentication);
    }

    /**
     * Creates or updates a metric if the metric alrady exists
     * 
     * @param metricName
     * @param unit
     */
    public void createMetric(String metricName, String unit) {
        CreateUpdateMetric metricRequest = new CreateUpdateMetric(metricName, unit);
        ClientResponse response = baseResource.path(PATH_JOINER.join("v1", "metrics", metricRequest.getName()))
                .header(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(authentication), Charsets.US_ASCII))
                .entity(metricRequest, MediaType.APPLICATION_JSON_TYPE)
                .put(ClientResponse.class);
        response.close();
    }

    public void addMeasurements(int sourceId, Map<String, Number> measurements, Optional<DateTime> optionalTimestamp) {
        List<List<Object>> payload = Lists.newArrayList();
        final long timestamp = optionalTimestamp.or(new DateTime()).getMillis();
        for (Map.Entry<String, Number> m : measurements.entrySet()) {
            payload.add(ImmutableList.<Object>of(String.valueOf(sourceId), m.getKey(), m.getValue(), timestamp));
        }
        sendMeasurements(payload);
    }

    public void addMeasurements(List<Measurement> measurements) {
        sendMeasurements(FluentIterable.from(measurements)
                .transform(MetricUtils.toBulkEntry())
                .toList());
    }

    private void sendMeasurements(final List<List<Object>> payload) {
        asyncWebResource.path(PATH_JOINER.join("v1", "measurements"))
                .header(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(authentication), Charsets.US_ASCII))
                .entity(payload, MediaType.APPLICATION_JSON_TYPE)
                .post(new TypeListener<ClientResponse>(ClientResponse.class) {
                    @Override
                    public void onComplete(Future<ClientResponse> f) throws InterruptedException {
                        try {
                            ClientResponse response = f.get();
                            String entity = response.getEntity(String.class);
                            response.close();
                            if (Response.Status.OK.getStatusCode() != response.getStatus()) {
                                LOG.error("Unexpected response adding measurements: {} - {}", response.getStatusInfo(), entity);
                                throw new RuntimeException(entity);
                            }
                            response.close();
                        } catch (ExecutionException e) {
                            LOG.error("Interrupted trying to add measurement");
                        }
                    }
                });
    }
}

@@ -2543,30 +2543,78 @@
 tricName
-%0A     * @param
+ Boundary metric identifier%0A     * @param unit Boundary metric
  unit%0A  
@@ -3144,32 +3144,275 @@
 close();%0A    %7D%0A%0A
+    /**%0A     * Adds metric measurements and sends to Boundary APIs%0A     * %0A     * @param sourceId meter source id%0A     * @param measurements measurements to associate with the source%0A     * @param optionalTimestamp time of the metrics%0A     */%0A
     public void 
@@ -3887,24 +3887,96 @@
 ad);%0A    %7D%0A%0A
+    /**%0A     * %0A     * @param measurements Metrics measurements%0A     */%0A
     public v
@@ -4173,24 +4173,136 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Sends measurements to the Boundary metric API%0A     * @param payload List of measurements%0A     */%0A
     private 

Fix compilation of JavaDoc