package com.kodcu.rapid.path;

import com.kodcu.rapid.config.DockerClient;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Objects;

import static com.kodcu.rapid.util.Constants.CONFIGS;
import static com.kodcu.rapid.util.Networking.deleteResponse;
import static com.kodcu.rapid.util.Networking.getResponse;
import static com.kodcu.rapid.util.Networking.postResponse;

/**
 * Created by hakan on 30/08/2017
 */
@Path("configs")
public class Config extends DockerClient {

    @GET
    public Response listConfigs(@QueryParam("filters") String filters) throws UnsupportedEncodingException {

        WebTarget target = resource().path(CONFIGS);

        if (Objects.nonNull(filters))
            target = target.queryParam("filters", URLEncoder.encode(filters, "UTF-8"));

        Response response = getResponse(target);
        try {
            String raw = response.readEntity(String.class);
            return Response.status(response.getStatus())
                    .entity(Json.createReader(new StringReader(raw)).read())
                    .build();
        } finally {
            response.close();
        }
    }

    @GET
    @Path("{id}")
    public Response inspectConfig(@PathParam("id") String configId) {

        WebTarget target = resource().path(CONFIGS).path(configId);

        Response response = getResponse(target);
        try {
            JsonObject entity = response.readEntity(JsonObject.class);
            return Response.status(response.getStatus())
                    .entity(entity)
                    .build();
        } finally {
            response.close();
        }
    }

    @DELETE
    @Path("{id}")
    public Response deleteConfig(@PathParam("id") String configId) {

        WebTarget target = resource().path(CONFIGS).path(configId);
        Response response = deleteResponse(target);

        try {
            String entity = response.readEntity(String.class);

            if (entity.isEmpty()) {
                JsonObjectBuilder jsonObject = Json.createObjectBuilder();
                jsonObject.add("id", configId);
                jsonObject.add("message", "the config is deleted.");

                return Response.ok(jsonObject.build()).build();
            }

            return Response.status(response.getStatus()).entity(Json.createReader(new StringReader(entity)).read()).build();
        } finally {
            response.close();
        }
    }

    @POST
    @Path("create")
    public Response createConfig(JsonObject content) {

        WebTarget target = resource().path(CONFIGS).path("create");

        Response response = postResponse(target, content);
        try {
            JsonObject entity = response.readEntity(JsonObject.class);
            return Response.status(response.getStatus())
                    .entity(entity)
                    .build();
        } finally {
            response.close();
        }
    }

    @POST
    @Path("{id}/update")
    public Response updateConfig(@PathParam("id") String configId,
                                 @QueryParam("version") String version,
                                 JsonObject content) {

        WebTarget target = resource().path(CONFIGS)
                .path(configId)
                .path("update")
                .queryParam("version", version);

        Response response = postResponse(target, content);
        try {

            String entity = response.readEntity(String.class);

            if (entity.isEmpty()) {
                JsonObjectBuilder jsonObject = Json.createObjectBuilder();
                jsonObject.add("id", configId);
                jsonObject.add("message", "the config is updated.");

                return Response.ok(jsonObject.build()).build();
            }

            return Response.status(response.getStatus()).entity(Json.createReader(new StringReader(entity)).read()).build();
        } finally {
            response.close();
        }
    }

}

@@ -156,24 +156,54 @@
 ectBuilder;%0A
+import javax.json.JsonReader;%0A
 import javax
@@ -1240,34 +1240,16 @@
 arget);%0A
-        try %7B%0A    
         
@@ -1287,32 +1287,108 @@
 y(String.class);
+%0A%0A        try (JsonReader json = Json.createReader(new StringReader(raw))) %7B
 %0A            ret
@@ -1461,49 +1461,13 @@
 ity(
-J
+j
 son.
-createReader(new StringReader(raw)).
 read
@@ -2279,34 +2279,16 @@
 rget);%0A%0A
-        try %7B%0A    
         
@@ -2339,36 +2339,32 @@
 lass);%0A%0A        
-    
 if (entity.isEmp
@@ -2375,36 +2375,32 @@
 ) %7B%0A            
-  
-  
 JsonObjectBuilde
@@ -2434,36 +2434,32 @@
 bjectBuilder();%0A
-    
             json
@@ -2490,36 +2490,32 @@
 d);%0A            
-    
 jsonObject.add(%22
@@ -2556,36 +2556,32 @@
 );%0A%0A            
-  
-  
 return Response.
@@ -2612,36 +2612,32 @@
 uild();%0A        
-    
 %7D%0A%0A            r
@@ -2635,171 +2635,160 @@
     
-    return Response.status(response.getStatus()).entity(Json.createReader(new StringReader(entity)).read()).build();%0A        %7D finally %7B%0A            response.close
+try (JsonReader json = Json.createReader(new StringReader(entity))) %7B%0A            return Response.status(response.getStatus()).entity(json.read()).build
 ();%0A
@@ -4127,32 +4127,118 @@
 %0A            %7D%0A%0A
+            try (JsonReader json = Json.createReader(new StringReader(entity))) %7B%0A    
             retu
@@ -4289,52 +4289,13 @@
 ity(
-J
+j
 son.
-createReader(new StringReader(entity)).
 read
@@ -4299,32 +4299,46 @@
 ead()).build();%0A
+            %7D%0A
         %7D finall

add try-resource