package com.hubspot.singularity;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;

public class SingularityTaskIdHistory implements Comparable<SingularityTaskIdHistory> {

  private final SingularityTaskId taskId;
  private final long updatedAt;
  private final Optional<ExtendedTaskState> lastTaskState;
  private final Optional<String> runId;

  private static final Comparator<SingularityTaskHistoryUpdate> comparator = new Comparator<SingularityTaskHistoryUpdate>() {
    @Override
    public int compare(SingularityTaskHistoryUpdate o1, SingularityTaskHistoryUpdate o2) {
      return o1.getTaskState().compareTo(o2.getTaskState());
    }
  };

  public static SingularityTaskIdHistory fromTaskIdAndTaskAndUpdates(SingularityTaskId taskId, SingularityTask task, List<SingularityTaskHistoryUpdate> updates) {
    ExtendedTaskState lastTaskState = null;
    long updatedAt = taskId.getStartedAt();

    if (updates != null && !updates.isEmpty()) {
      Collections.sort(updates, comparator);
      SingularityTaskHistoryUpdate lastUpdate = Iterables.getLast(updates);
      lastTaskState = lastUpdate.getTaskState();
      updatedAt = lastUpdate.getTimestamp();
    }

    return new SingularityTaskIdHistory(taskId, updatedAt, Optional.fromNullable(lastTaskState), task.getTaskRequest().getPendingTask().getRunId());
  }

  @JsonCreator
  public SingularityTaskIdHistory(@JsonProperty("taskId") SingularityTaskId taskId, @JsonProperty("updatedAt") long updatedAt,
      @JsonProperty("lastStatus") Optional<ExtendedTaskState> lastTaskState, @JsonProperty("runId") Optional<String> runId) {
    this.taskId = taskId;
    this.updatedAt = updatedAt;
    this.lastTaskState = lastTaskState;
    this.runId = runId;
  }

  @Override
  public int compareTo(SingularityTaskIdHistory o) {
    return ComparisonChain.start()
      .compare(o.getUpdatedAt(), updatedAt)
      .compare(taskId.getId(), o.getTaskId().getId())
      .result();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(taskId, updatedAt, lastTaskState, runId);
  }

  @Override
  public boolean equals(Object other) {
      if (other == this) {
          return true;
      }
      if (other == null || other.getClass() != this.getClass()) {
          return false;
      }

      SingularityTaskIdHistory that = (SingularityTaskIdHistory) other;
      return Objects.equal(this.taskId , that.taskId)
              && Objects.equal(this.updatedAt , that.updatedAt)
              && Objects.equal(this.lastTaskState , that.lastTaskState)
              && Objects.equal(this.runId, that.runId);
  }

  public SingularityTaskId getTaskId() {
    return taskId;
  }

  public Optional<ExtendedTaskState> getLastTaskState() {
    return lastTaskState;
  }

  public long getUpdatedAt() {
    return updatedAt;
  }

  public Optional<String> getRunId() {
    return runId;
  }

  @Override
  public String toString() {
    return "SingularityTaskIdHistory [taskId=" + taskId + ", updatedAt=" + updatedAt + ", lastTaskState=" + lastTaskState + ", runId=" + runId + "]";
  }

}

@@ -349,52 +349,8 @@
 ain;
-%0Aimport com.google.common.collect.Iterables;
 %0A%0Apu
@@ -1219,53 +1219,8 @@
 ) %7B%0A
-      Collections.sort(updates, comparator);%0A
     
@@ -1267,33 +1267,43 @@
 e = 
-Iterables.getLast(updates
+Collections.max(updates, comparator
 );%0A 

use Collections.max