package com.hubspot.baragon.models;

import java.util.Collection;
import java.util.Collections;

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

@JsonIgnoreProperties(ignoreUnknown = true)
public class BasicServiceContext {
  private final BaragonService service;
  private final Collection<UpstreamInfo> upstreams;
  private final Collection<BaragonConfigFile> nginxConfigs;
  private final Long timestamp;

  @JsonCreator
  public BasicServiceContext(@JsonProperty("service") BaragonService service,
                             @JsonProperty("upstreams") Collection<UpstreamInfo> upstreams,
                             @JsonProperty("nginxConfigs") Collection<BaragonConfigFile> nginxConfigs,
                             @JsonProperty("timestamp") Long timestamp) {
    this.service = service;
    this.upstreams = MoreObjects.firstNonNull(upstreams, Collections.emptyList());
    this.nginxConfigs = nginxConfigs;
    this.timestamp = timestamp;
  }

  public BasicServiceContext(@JsonProperty("service") BaragonService service,
                             @JsonProperty("upstreams") Collection<UpstreamInfo> upstreams,
                             @JsonProperty("nginxConfigs") Collection<BaragonConfigFile> nginxConfigs) {
    this(service, upstreams, nginxConfigs, System.currentTimeMillis());
  }

  public BasicServiceContext(@JsonProperty("service") BaragonService service,
                             @JsonProperty("upstreams") Collection<UpstreamInfo> upstreams) {
    this(service, upstreams, Collections.EMPTY_LIST, System.currentTimeMillis());
  }

  public BaragonService getService() {
    return service;
  }

  public Collection<UpstreamInfo> getUpstreams() {
    return upstreams;
  }

  public Long getTimestamp() {
    return timestamp;
  }

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

    BasicServiceContext that = (BasicServiceContext) o;

    if (!service.equals(that.service)) {
      return false;
    }
    if (upstreams.size() != that.upstreams.size()) {
      return false;
    }

    for (UpstreamInfo upstreamInfo : upstreams) {
      boolean foundMatching = false;
      for (UpstreamInfo otherUpstream : that.upstreams) {
        if (UpstreamInfo.upstreamAndGroupMatches(upstreamInfo, otherUpstream)) {
          foundMatching = true;
          break;
        }
      }
      if (!foundMatching) {
        return false;
      }
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = service.hashCode();
    result = 31 * result + upstreams.hashCode();
    return result;
  }

  @Override
  public String toString() {
    return "BasicServiceContext{" +
        "service=" + service +
        ", upstreams=" + upstreams +
        ", timestamp=" + timestamp +
        '}';
  }
}

@@ -516,29 +516,32 @@
 ConfigFile%3E 
-nginx
+rendered
 Configs;%0A  p
@@ -791,37 +791,40 @@
  @JsonProperty(%22
-nginx
+rendered
 Configs%22) Collec
@@ -843,29 +843,32 @@
 ConfigFile%3E 
-nginx
+rendered
 Configs,%0A   
@@ -1058,21 +1058,24 @@
    this.
-nginx
+rendered
 Configs 
@@ -1076,21 +1076,24 @@
 nfigs = 
-nginx
+rendered
 Configs;
@@ -1344,21 +1344,24 @@
 operty(%22
-nginx
+rendered
 Configs%22
@@ -1392,21 +1392,24 @@
 igFile%3E 
-nginx
+rendered
 Configs)
@@ -1444,13 +1444,16 @@
 ms, 
-nginx
+rendered
 Conf

Rename nginxConfigs -> renderedConfigs