package com.jivesoftware.os.upena.deployable.endpoints;

import com.jivesoftware.os.mlogger.core.MetricLogger;
import com.jivesoftware.os.mlogger.core.MetricLoggerFactory;
import com.jivesoftware.os.routing.bird.shared.ResponseHelper;
import com.jivesoftware.os.upena.deployable.region.HealthPluginRegion;
import com.jivesoftware.os.upena.deployable.region.HealthPluginRegion.HealthPluginRegionInput;
import com.jivesoftware.os.upena.deployable.soy.SoyService;
import java.util.Map;
import javax.inject.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 */
@Singleton
@Path("/ui/health")
public class HealthPluginEndpoints {

    private static final MetricLogger LOG = MetricLoggerFactory.getLogger();

    private final SoyService soyService;
    private final HealthPluginRegion pluginRegion;
    private final ResponseHelper responseHelper = ResponseHelper.INSTANCE;

    public HealthPluginEndpoints(@Context SoyService soyService, @Context HealthPluginRegion pluginRegion) {
        this.soyService = soyService;
        this.pluginRegion = pluginRegion;
    }

    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public Response filter(@Context HttpServletRequest httpRequest,
        @QueryParam("cluster") @DefaultValue("") String cluster,
        @QueryParam("host") @DefaultValue("") String host,
        @QueryParam("service") @DefaultValue("") String service) {
        String rendered = soyService.renderPlugin(httpRequest.getRemoteUser(), pluginRegion,
            new HealthPluginRegionInput(cluster, host, service));
        return Response.ok(rendered).build();
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("/uis")
    public Response uis(@QueryParam("instanceKey") @DefaultValue("") String instanceKey) throws Exception {
        return Response.ok(pluginRegion.renderInstanceHealth(instanceKey)).build();
    }

    @GET
    @Path("/live")
    @Produces(MediaType.APPLICATION_JSON)
    public Response live(@Context HttpServletRequest httpRequest,
        @QueryParam("cluster") @DefaultValue("") String cluster,
        @QueryParam("host") @DefaultValue("") String host,
        @QueryParam("service") @DefaultValue("") String service) {
        return Response.ok(pluginRegion.renderLive(httpRequest.getRemoteUser(), new HealthPluginRegionInput(cluster, host, service))).build();
    }


    @POST
    @Path("/poll")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response poll(
        @FormParam("cluster") @DefaultValue("dev") String cluster,
        @FormParam("host") @DefaultValue("") String host,
        @FormParam("service") @DefaultValue("") String service) {
        try {
            Map<String, Object> result = pluginRegion.poll(new HealthPluginRegionInput(cluster,
                host,
                service));
            return responseHelper.jsonResponse(result != null ? result : "");
        } catch (Exception e) {
            LOG.error("Health poll failed", e);
            return responseHelper.errorResponse("Health poll failed", e);
        }
    }

}

@@ -1714,32 +1714,50 @@
 ring service) %7B%0A
+        try %7B%0A    
         String r
@@ -1845,16 +1845,20 @@
         
+   
+ 
 new Heal
@@ -1903,32 +1903,36 @@
 vice));%0A        
+    
 return Response.
@@ -1945,32 +1945,179 @@
 dered).build();%0A
+        %7D catch (Exception e) %7B%0A            LOG.error(%22filter%22, e);%0A            return responseHelper.errorResponse(%22filter failed%22, e);%0A        %7D%0A
     %7D%0A%0A    @GET%0A
@@ -2277,16 +2277,34 @@
 ption %7B%0A
+        try %7B%0A    
         
@@ -2375,24 +2375,165 @@
 )).build();%0A
+        %7D catch (Exception e) %7B%0A            LOG.error(%22uis%22, e);%0A            return responseHelper.errorResponse(%22uis failed%22, e);%0A        %7D%0A
     %7D%0A%0A    @
@@ -2846,32 +2846,50 @@
 ring service) %7B%0A
+        try %7B%0A    
         return R
@@ -3019,16 +3019,158 @@
 ();%0A
+        %7D catch (Exception e) %7B%0A            LOG.error(%22live%22, e);%0A            return responseHelper.errorResponse(%22live failed%22, e);%0A        %7D%0A
     %7D%0A%0A
-%0A
     
@@ -3798,23 +3798,16 @@
 .error(%22
-Health 
 poll fai
@@ -3869,15 +3869,8 @@
 se(%22
-Health 
 poll

Introduce datacenter and rack notions.