package fi.csc.chipster.toolbox.resource;

import java.io.IOException;

import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import fi.csc.chipster.toolbox.Toolbox;
import fi.csc.chipster.toolbox.ToolboxModule;
import fi.csc.chipster.toolbox.ToolboxModule.ToolboxCategory;
import fi.csc.chipster.toolbox.ToolboxTool;

@Singleton
@Path("modules")
public class ModuleResource {

    private Toolbox toolbox;

    public ModuleResource(Toolbox toolbox) throws IOException {
        this.toolbox = toolbox;
    }

    @GET
//    @JacksonFeatures(serializationEnable =  { SerializationFeature.INDENT_OUTPUT })
    @Produces(MediaType.APPLICATION_JSON)
    public final Response getModules() throws IOException {
    	
        // node factory for creating json nodes
        JsonNodeFactory factory = new JsonNodeFactory(false);
 
        ArrayNode modules = factory.arrayNode();

        // modules
        for (ToolboxModule toolboxModule: toolbox.getModules()) {
        	ObjectNode module = factory.objectNode();
        	module.put("name", toolboxModule.getName());
        	
        	// categories in a module 
        	ArrayNode categories = factory.arrayNode();
        	module.set("categories", categories);
        	for (ToolboxCategory toolboxCategory : toolboxModule.getCategories()) {
    			ObjectNode category = factory.objectNode();
    			category.put("name", toolboxCategory.getName());
    			category.put("color", toolboxCategory.getColor());
    			category.put("hidden", toolboxCategory.isHidden());
    			
				// tools in a category
				ArrayNode tools = factory.arrayNode();
				category.set("tools", tools);
    			for (ToolboxTool toolboxTool : toolboxCategory.getTools()) {
    				ObjectNode tool = factory.objectNode();
    				tool.put("id", toolboxTool.getId());
    				tool.put("name", toolboxTool.getSadlDescription().getName().getDisplayName());
    				tools.add(tool);
    			}
    			categories.add(category);
    		}
        	
        	modules.add(module);
        }
        
        return Response.ok(modules).build();
    }
}


@@ -63,16 +63,48 @@
 ception;
+%0Aimport java.util.LinkedHashSet;
 %0A%0Aimport
@@ -1215,24 +1215,25 @@
 modules%0A
+%0A
         
 for (Too
@@ -1224,21 +1224,274 @@
         
-for (
+// define order or the modules%0A        String%5B%5D priorityModules = new String%5B%5D %7B %22ngs%22, %22microarray%22, %22misc%22 %7D; %0A        LinkedHashSet%3CToolboxModule%3E orderedModules = new LinkedHashSet%3CToolboxModule%3E();%0A        for (String name : priorityModules) %7B%0A        %09
 ToolboxM
@@ -1495,24 +1495,33 @@
 oxModule
+ module =
  toolbox
 Module: 
@@ -1516,36 +1516,395 @@
 lbox
+.get
 Module
-: toolbox.getModules()
+(name);%0A            if (module != null) %7B%0A            %09orderedModules.add(module);%0A            %7D%0A        %7D%0A        for (ToolboxModule module: toolbox.getModules()) %7B%0A        %09if (!orderedModules.contains(module)) %7B%0A        %09%09orderedModules.add(module);%0A        %09%7D%0A        %7D        %0A        %0A        // go through modules%0A        for (ToolboxModule toolboxModule: orderedModules
 ) %7B%0A
@@ -1995,32 +1995,38 @@
 oxModule.getName
+Pretty
 ());%0A        %09%0A 

Define order of the modules in the json api