package org.opencb.opencga.server.rest.analysis;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.opencb.commons.datastore.core.QueryResponse;
import org.opencb.commons.datastore.core.QueryResult;
import org.opencb.opencga.core.exception.VersionException;
import org.opencb.opencga.core.models.Job;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.io.IOException;
import java.util.Map;

@Path("/{version}/analysis/tool")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Analysis - Tool", position = 4, description = "Methods for working with 'tool' endpoint")
public class ToolAnalysisWSService extends AnalysisWSService {

    public ToolAnalysisWSService(@Context UriInfo uriInfo, @Context HttpServletRequest httpServletRequest,
                                      @Context HttpHeaders httpHeaders) throws IOException, VersionException {
        super(uriInfo, httpServletRequest, httpHeaders);
    }

    public ToolAnalysisWSService(String version, @Context UriInfo uriInfo, @Context HttpServletRequest httpServletRequest,
                                      @Context HttpHeaders httpHeaders) throws IOException, VersionException {
        super(version, uriInfo, httpServletRequest, httpHeaders);
    }

    private class ExecuteParams {
        public String jobName;
        public String description;
        @JsonProperty(required = true)
        public String toolId;
        public String execution;
        @JsonProperty(required = true)
        public String outDir;
        @JsonProperty(required = true)
        public Map<String, String> toolParams;

        public ExecuteParams() {
        }
    }

    @POST
    @Path("/execute")
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Execute an analysis using an internal or external tool", response = QueryResponse.class)
    public Response run(
            @ApiParam(value = "Study [[user@]project:]study where study and project can be either the id or alias") @QueryParam("study")
                    String studyStr,
            @ApiParam(value = "Execution parameters", required = true) ExecuteParams params) {
        try {
            QueryResult<Job> queryResult = catalogManager.getJobManager().create(studyStr, params.jobName, params.description,
                    params.toolId, params.execution, params.outDir, params.toolParams, sessionId);
            return createOkResponse(queryResult);
        } catch(Exception e) {
            return createErrorResponse(e);
        }
    }


}

@@ -1416,16 +1416,23 @@
 private 
+static 
 class Ex
@@ -1855,50 +1855,8 @@
 e%22)%0A
-    @Consumes(MediaType.APPLICATION_JSON)%0A
     
@@ -2107,16 +2107,32 @@
  alias%22)
+%0A               
  @QueryP
@@ -2144,37 +2144,16 @@
 %22study%22)
-%0A                    
 String s
@@ -2188,25 +2188,45 @@
 am(value = %22
-E
+Json containing the e
 xecution par

fix analysis tool webservice