package com.hubspot.singularity;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;

@JsonIgnoreProperties( ignoreUnknown = true )
public class SingularityTaskOfferResult {
  private final SingularityPendingTaskId taskId;
  private Map<String, String> offerDeclinedReasons = new HashMap<>();
  private Optional<String> launchedOnHost = Optional.absent();
  private Optional<String> acceptedOfferId = Optional.absent();

  public SingularityTaskOfferResult(SingularityPendingTaskId taskId) {
    this.taskId = taskId;
  }

  @JsonCreator
  public SingularityTaskOfferResult(@JsonProperty("taskId") SingularityPendingTaskId taskId,
                                    @JsonProperty("offerDeclinedReasons") Map<String, String> offerDeclinedReasons,
                                    @JsonProperty("launchedOnHost") Optional<String> launchedOnHost) {
    this.taskId = taskId;
    this.offerDeclinedReasons = offerDeclinedReasons;
    this.launchedOnHost = launchedOnHost;
  }

  public SingularityPendingTaskId getTaskId() {
    return taskId;
  }

  public Map<String, String> getOfferDeclinedReasons() {
    return offerDeclinedReasons;
  }

  public Optional<String> getLaunchedOnHost() {
    return launchedOnHost;
  }

  public Optional<String> getAcceptedOfferId() {
    return acceptedOfferId;
  }

  public void addOfferDeclinedReason(String host, String reason) {
    offerDeclinedReasons.put(host, reason);
  }

  public void clearOfferDeclinedReasons() {
    offerDeclinedReasons.clear();
  }

  public void setLaunchedOnHost(Optional<String> launchedOnHost) {
    this.launchedOnHost = launchedOnHost;
  }

  public void setAcceptedOfferId(Optional<String> acceptedOfferId) {
    this.acceptedOfferId = acceptedOfferId;
  }
}

@@ -374,16 +374,48 @@
 esult %7B%0A
+  private final long timestamp;%0A
   privat
@@ -746,24 +746,73 @@
 d = taskId;%0A
+    this.timestamp = System.currentTimeMillis();%0A
   %7D%0A%0A  @Json
@@ -855,16 +855,95 @@
 rResult(
+@JsonProperty(%22timestamp%22) long timestamp,%0A                                    
 @JsonPro
@@ -1202,32 +1202,64 @@
 unchedOnHost) %7B%0A
+    this.timestamp = timestamp;%0A
     this.taskId 
@@ -2127,11 +2127,210 @@
 rId;%0A  %7D
+%0A%0A  public long getTimestamp() %7B%0A    return timestamp;%0A  %7D%0A%0A  public void setOfferDeclinedReasons(Map%3CString, String%3E offerDeclinedReasons) %7B%0A    this.offerDeclinedReasons = offerDeclinedReasons;%0A  %7D
 %0A%7D%0A

add timestamp in offer result json