package com.hubspot.singularity;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

public class SingularityLoadBalancerService extends SingularityJsonObject {

  private final String serviceId;
  private final Collection<String> owners;
  private final String loadBalancerBaseUri;
  private final List<String> loadBalancerGroups;
  private final Optional<Map<String, Object>> options;

  public static SingularityLoadBalancerService fromRequestAndDeploy(SingularityRequest request, SingularityDeploy deploy) {
    final List<String> owners = request.getOwners().or(Collections.<String>emptyList());
    final List<String> loadBalancerGroups = deploy.getLoadBalancerGroups().or(Collections.<String>emptyList());

    return new SingularityLoadBalancerService(request.getId(), owners, deploy.getServiceBasePath().get(), loadBalancerGroups, Optional.<Map<String, Object>>absent());
  }
  
  public static SingularityLoadBalancerService fromTaskRequest(SingularityTaskRequest taskRequest) {
    return fromRequestAndDeploy(taskRequest.getRequest(), taskRequest.getDeploy());
  }
  
  @JsonCreator
  public SingularityLoadBalancerService(@JsonProperty("serviceId") String serviceId,
                                        @JsonProperty("owners") Collection<String> owners,
                                        @JsonProperty("loadBalancerBaseUri") String loadBalancerBaseUri,
                                        @JsonProperty("loadBalancerGroups") List<String> loadBalancerGroups,
                                        @JsonProperty("options") Optional<Map<String, Object>> options) {
    this.serviceId = serviceId;
    this.owners = owners;
    this.loadBalancerBaseUri = loadBalancerBaseUri;
    this.loadBalancerGroups = loadBalancerGroups;
    this.options = options;
  }

  public String getServiceId() {
    return serviceId;
  }

  public Collection<String> getOwners() {
    return owners;
  }

  public String getLoadBalancerBaseUri() {
    return loadBalancerBaseUri;
  }

  public List<String> getLoadBalancerGroups() {
    return loadBalancerGroups;
  }

  public Optional<Map<String, Object>> getOptions() {
    return options;
  }

  @Override
  public String toString() {
    return "SingularityLoadBalancerService [" +
        "serviceId='" + serviceId + '\'' +
        ", owners=" + owners +
        ", loadBalancerBaseUri='" + loadBalancerBaseUri + '\'' +
        ", loadBalancerGroups=" + loadBalancerGroups +
        ", options=" + options +
        ']';
  }

}

@@ -31,113 +31,8 @@
 y;%0A%0A
-import java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 impo
@@ -175,16 +175,121 @@
 ional;%0A%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 public c
@@ -457,35 +457,31 @@
  String 
-loadBalancerBaseUri
+serviceBasePath
 ;%0A  priv
@@ -1521,56 +1521,48 @@
 ty(%22
-loadBalancerBaseUri%22) String loadBalancerBaseUri
+serviceBasePath%22) String serviceBasePath
 ,%0A  
@@ -1845,49 +1845,41 @@
 his.
-loadBalancerBaseUri = loadBalancerBaseUri
+serviceBasePath = serviceBasePath
 ;%0A  
@@ -2108,62 +2108,54 @@
  get
-LoadBalancerBaseUri() %7B%0A    return loadBalancerBaseUri
+ServiceBasePath() %7B%0A    return serviceBasePath
 ;%0A  
@@ -2498,52 +2498,44 @@
  %22, 
-loadBalancerBaseUri='%22 + loadBalancerBaseUri
+serviceBasePath='%22 + serviceBasePath
  + '
@@ -2645,11 +2645,10 @@
 %5D';%0A  %7D%0A
-%0A
 %7D%0A

rename loadBalancerBaseUri to serviceBasePath for baragon