/*
 * Copyright 2013-2014 by Cloudsoft Corporation Limited
 *
 * 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 brooklyn.rest.transform;

import java.net.URI;

import org.slf4j.LoggerFactory;

import brooklyn.config.render.RendererHints;
import brooklyn.entity.Entity;
import brooklyn.event.AttributeSensor;
import brooklyn.event.Sensor;
import brooklyn.rest.domain.SensorSummary;
import brooklyn.util.collections.MutableMap;
import brooklyn.util.exceptions.Exceptions;
import brooklyn.util.net.URLParamEncoder;
import brooklyn.util.text.Strings;

import com.google.common.collect.Iterables;

public class SensorTransformer {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(SensorTransformer.class);

    public static SensorSummary sensorSummaryForCatalog(Sensor<?> sensor) {
        return new SensorSummary(sensor.getName(), sensor.getTypeName(),
                sensor.getDescription(), null);
    }

    @SuppressWarnings("rawtypes")
    public static SensorSummary sensorSummary(Entity entity, Sensor<?> sensor) {
        String applicationUri = "/v1/applications/" + entity.getApplicationId();
        String entityUri = applicationUri + "/entities/" + entity.getId();
        String selfUri = entityUri + "/sensors/" + URLParamEncoder.encode(sensor.getName());

        MutableMap.Builder<String, URI> lb = MutableMap.<String, URI>builder()
                .put("self", URI.create(selfUri))
                .put("application", URI.create(applicationUri))
                .put("entity", URI.create(entityUri))
                .put("action:json", URI.create(selfUri));

        Iterable<RendererHints.NamedAction> hints = Iterables.filter(RendererHints.getHintsFor(sensor), RendererHints.NamedAction.class);
        for (RendererHints.NamedAction na : hints) addNamedAction(lb, na , entity, sensor);

        return new SensorSummary(sensor.getName(), sensor.getTypeName(), sensor.getDescription(), lb.build());
    }

    @SuppressWarnings("rawtypes")
    private static void addNamedAction(MutableMap.Builder<String, URI> lb, RendererHints.NamedAction na , Entity entity, Sensor<?> sensor) {
        if (na instanceof RendererHints.NamedActionWithUrl) {
            try {
                String v = ((RendererHints.NamedActionWithUrl) na ).getUrl(entity, (AttributeSensor<?>) sensor);
                if (Strings.isNonBlank(v)) lb.putIfAbsent("action:open", URI.create(v));
            } catch (Exception e) {
                Exceptions.propagateIfFatal(e);
                log.warn("Unable to make use of URL sensor "+sensor+" on "+entity+": "+e);
            }
        }
    }
}

@@ -2776,17 +2776,16 @@
 hUrl) na
- 
 ).getUrl
@@ -2865,16 +2865,108 @@
 lank(v))
+ %7B%0A                    String action = na.getActionName().toLowerCase();%0A                   
  lb.putI
@@ -2985,13 +2985,16 @@
 ion:
-open%22
+%22+action
 , UR
@@ -3003,24 +3003,42 @@
 create(v));%0A
+                %7D%0A
             

Allow different action names in rendering hints