package com.oguerreiro.resilient.error;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.oguerreiro.resilient.domain.Variable;

public class PeriodProcessDependenciesNotSatisfyedException extends ResilientRuntimeException {
  private static final String CODE = "E012";

  private Map<Variable, Set<String>> remainingVariableDependencies;

  public PeriodProcessDependenciesNotSatisfyedException(Map<Variable, Set<String>> remainingVariableDependencies) {
    super("Dependencies not satisfyed");

    this.remainingVariableDependencies = remainingVariableDependencies;

    addMessageArg(this.buildDependencyArg());
  }

  @Override
  public String getMessageCode() {
    return CODE;
  }

  public void printDependencies() {
    for (Entry<Variable, Set<String>> entry : remainingVariableDependencies.entrySet()) {
      System.out.print(entry.getKey().getCode() + " depends of ");
      for (String dependency : entry.getValue()) {
        System.out.print(dependency + " ; ");
      }
      System.out.println();
    }
  }

  private String buildDependencyArg() {
    StringBuilder arg = new StringBuilder();

    boolean first = true;
    int max = 10;
    for (Entry<Variable, Set<String>> entry : remainingVariableDependencies.entrySet()) {
      if (!first)
        arg.append(";");
      arg.append(entry.getKey().getCode());

      first = false;
      max--;

      if (max == 0) {
        arg.append("; ...");
        break;
      }
    }

    return arg.toString();
  }
}
