package com.hubspot.blazar.discovery.maven;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.hubspot.blazar.base.DependencyInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ProjectObjectModel {
  private final String groupId;
  private final String artifactId;
  private final String packaging;
  private final List<Map<String, String>> dependencies;
  private final Set<String> depends;

  @JsonCreator
  public ProjectObjectModel(@JsonProperty("parent") final Map<String, String> parent,
                            @JsonProperty("groupId") Optional<String> groupId,
                            @JsonProperty("artifactId") String artifactId,
                            @JsonProperty("packaging") Optional<String> packaging,
                            @JsonProperty("dependencies") List<Map<String, String>> dependencies) {
    this.groupId = groupId.or(new Supplier<String>() {

      @Override
      public String get() {
        return parent.get("groupId");
      }
    });
    this.artifactId = Preconditions.checkNotNull(artifactId);
    this.packaging = packaging.or("jar");
    this.dependencies = Objects.firstNonNull(dependencies, new ArrayList<Map<String, String>>());
    if (parent != null) {
      this.dependencies.add(parent);
    }
    this.depends = toDepends(this.dependencies);
  }

  public String getGroupId() {
    return groupId;
  }

  public String getArtifactId() {
    return artifactId;
  }

  public String getPackaging() {
    return packaging;
  }

  public List<Map<String, String>> getDependencies() {
    return dependencies;
  }

  @JsonIgnore
  public DependencyInfo getDependencyInfo() {
    return new DependencyInfo(getDepends(), getProvides());
  }

  @JsonIgnore
  public Set<String> getDepends() {
    return depends;
  }

  @JsonIgnore
  public Set<String> getProvides() {
    return Collections.singleton(groupId + ":" + artifactId);
  }

  private static Set<String> toDepends(List<Map<String, String>> dependencies) {
    Set<String> depends = new HashSet<>();

    if (dependencies == null) {
      return depends;
    }

    for (Map<String, String> dependency : dependencies) {
      String groupId = Preconditions.checkNotNull(dependency.get("groupId"));
      String artifactId = Preconditions.checkNotNull(dependency.get("artifactId"));
      if (groupId.contains("${") || artifactId.contains("${")) {
        continue;
      }

      depends.add(groupId + ":" + artifactId);
    }

    return depends;
  }
}

@@ -721,38 +721,38 @@
 ist%3CMap%3CString, 
-String
+Object
 %3E%3E dependencies;
@@ -875,22 +875,22 @@
 String, 
-String
+Object
 %3E parent
@@ -1195,38 +1195,38 @@
 ist%3CMap%3CString, 
-String
+Object
 %3E%3E dependencies)
@@ -1342,16 +1342,25 @@
   return
+ (String)
  parent.
@@ -1580,22 +1580,22 @@
 String, 
-String
+Object
 %3E%3E());%0A 
@@ -1920,22 +1920,22 @@
 String, 
-String
+Object
 %3E%3E getDe
@@ -2353,22 +2353,22 @@
 String, 
-String
+Object
 %3E%3E depen
@@ -2504,22 +2504,22 @@
 String, 
-String
+Object
 %3E depend
@@ -2583,32 +2583,41 @@
 ns.checkNotNull(
+(String) 
 dependency.get(%22
@@ -2677,24 +2677,33 @@
 heckNotNull(
+(String) 
 dependency.g

Handle exclusions