package com.hubspot.singularity.resources;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;

import com.google.inject.Inject;
import com.hubspot.singularity.SingularityDeployHistory;
import com.hubspot.singularity.SingularityRequestHistory;
import com.hubspot.singularity.SingularityService;
import com.hubspot.singularity.SingularityTaskHistory;
import com.hubspot.singularity.SingularityTaskId;
import com.hubspot.singularity.SingularityTaskIdHistory;
import com.hubspot.singularity.data.DeployManager;
import com.hubspot.singularity.data.RequestManager;
import com.hubspot.singularity.data.TaskManager;
import com.hubspot.singularity.data.history.DeployHistoryHelper;
import com.hubspot.singularity.data.history.HistoryManager;
import com.hubspot.singularity.data.history.RequestHistoryHelper;
import com.hubspot.singularity.data.history.TaskHistoryHelper;

@Path(SingularityService.API_BASE_PATH + "/history")
@Produces({ MediaType.APPLICATION_JSON })
public class HistoryResource extends AbstractHistoryResource {

  private final HistoryManager historyManager;
  private final DeployManager deployManager;
  private final TaskManager taskManager;
  private final RequestManager requestManager;
  
  @Inject
  public HistoryResource(HistoryManager historyManager, DeployManager deployManager, TaskManager taskManager, RequestManager requestManager) {
    super(historyManager, taskManager, deployManager);

    this.requestManager = requestManager;
    this.deployManager = deployManager;
    this.historyManager = historyManager;
    this.taskManager = taskManager;
  }

  @GET
  @Path("/task/{taskId}")
  public SingularityTaskHistory getHistoryForTask(@PathParam("taskId") String taskId) {
    SingularityTaskId taskIdObj = getTaskIdObject(taskId);

    return getTaskHistory(taskIdObj);
  }

  private Integer getLimitCount(Integer countParam) {
    if (countParam == null) {
      return 100;
    }

    if (countParam < 1) {
      throw new WebApplicationException(Status.BAD_REQUEST);
    }

    if (countParam > 1000) {
      return 1000;
    }

    return countParam;
  }

  private Integer getLimitStart(Integer limitCount, Integer pageParam) {
    if (pageParam == null) {
      return 0;
    }

    if (pageParam < 1) {
      throw new WebApplicationException(Status.BAD_REQUEST);
    }

    return limitCount * (pageParam - 1);
  }

  @GET
  @Path("/request/{requestId}/tasks/active")
  public List<SingularityTaskIdHistory> getTaskHistoryForRequest(@PathParam("requestId") String requestId) {
    List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(requestId);

    return new TaskHistoryHelper(requestId, taskManager, historyManager).getHistoriesFor(activeTaskIds);
  }

  @GET
  @Path("/request/{requestId}/deploy/{deployId}")
  public SingularityDeployHistory getDeploy(@PathParam("requestId") String requestId, @PathParam("deployId") String deployId) {
    return getDeployHistory(requestId, deployId);
  }

  @GET
  @Path("/request/{requestId}/tasks")
  public List<SingularityTaskIdHistory> getTaskHistoryForRequest(@PathParam("requestId") String requestId, @QueryParam("count") Integer count, @QueryParam("page") Integer page) {
    final Integer limitCount = getLimitCount(count);
    final Integer limitStart = getLimitStart(limitCount, page);

    return new TaskHistoryHelper(requestId, taskManager, historyManager).getBlendedHistory(limitCount, limitStart);
  }

  @GET
  @Path("/request/{requestId}/deploys")
  public List<SingularityDeployHistory> getDeploys(@PathParam("requestId") String requestId, @QueryParam("count") Integer count, @QueryParam("page") Integer page) {
    final Integer limitCount = getLimitCount(count);
    final Integer limitStart = getLimitStart(limitCount, page);

    return new DeployHistoryHelper(requestId, deployManager, historyManager).getBlendedHistory(limitCount, limitStart);
  }

  @GET
  @Path("/request/{requestId}/requests")
  public List<SingularityRequestHistory> getRequestHistoryForRequest(@PathParam("requestId") String requestId, @QueryParam("count") Integer count, @QueryParam("page") Integer page) {
    Integer limitCount = getLimitCount(count);
    Integer limitStart = getLimitStart(limitCount, page);

    return new RequestHistoryHelper(requestId, requestManager, historyManager).getBlendedHistory(limitStart, limitCount);
  }

  @GET
  @Path("/requests/search")
  public List<String> getRequestHistoryForRequestLike(@QueryParam("requestIdLike") String requestIdLike, @QueryParam("count") Integer count, @QueryParam("page") Integer page) {
    Integer limitCount = getLimitCount(count);
    Integer limitStart = getLimitStart(limitCount, page);

    return historyManager.getRequestHistoryLike(requestIdLike, limitStart, limitCount);
  }

}

@@ -4331,32 +4331,38 @@
 ger page) %7B%0A    
+final 
 Integer limitCou
@@ -4384,32 +4384,38 @@
 unt(count);%0A    
+final 
 Integer limitSta
@@ -4551,36 +4551,36 @@
 istory(limit
-Star
+Coun
 t, limit
 Count);%0A  %7D%0A
@@ -4559,36 +4559,36 @@
 imitCount, limit
-Coun
+Star
 t);%0A  %7D%0A%0A  @GET%0A
@@ -4792,24 +4792,30 @@
 page) %7B%0A    
+final 
 Integer limi
@@ -4849,16 +4849,22 @@
 t);%0A    
+final 
 Integer 

fix endpoint