package org.batfish.job;

import java.util.Map;
import org.batfish.common.BatfishException;
import org.batfish.common.BatfishLogger;
import org.batfish.common.BatfishLogger.BatfishLoggerHistory;
import org.batfish.common.Warnings;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.answers.ConvertConfigurationAnswerElement;

public class ConvertConfigurationResult
    extends BatfishJobResult<Map<String, Configuration>, ConvertConfigurationAnswerElement> {

  private ConvertConfigurationAnswerElement _answerElement;

  private Map<String, Configuration> _configurations;

  private String _name;

  private Warnings _warnings;

  public ConvertConfigurationResult(
      long elapsedTime, BatfishLoggerHistory history, String name, Throwable failureCause) {
    super(elapsedTime, history, failureCause);
    _name = name;
  }

  public ConvertConfigurationResult(
      long elapsedTime,
      BatfishLoggerHistory history,
      Warnings warnings,
      String name,
      Map<String, Configuration> configurations,
      ConvertConfigurationAnswerElement answerElement) {
    super(elapsedTime, history);
    _name = name;
    _warnings = warnings;
    _configurations = configurations;
    _answerElement = answerElement;
  }

  @Override
  public void appendHistory(BatfishLogger logger) {
    String terseLogLevelPrefix;
    if (logger.isActive(BatfishLogger.LEVEL_INFO)) {
      terseLogLevelPrefix = "";
    } else {
      terseLogLevelPrefix = _name + ": ";
    }
    logger.append(_history, terseLogLevelPrefix);
  }

  @Override
  public void applyTo(
      Map<String, Configuration> configurations,
      BatfishLogger logger,
      ConvertConfigurationAnswerElement answerElement) {
    appendHistory(logger);
    if (_configurations != null) {
      for (String hostname : _configurations.keySet()) {
        Configuration config = _configurations.get(hostname);
        if (configurations.containsKey(hostname)) {
          throw new BatfishException("Duplicate hostname: " + hostname);
        } else {
          configurations.put(hostname, config);
          if (!_warnings.isEmpty()) {
            answerElement.getWarnings().put(hostname, _warnings);
          }
          if (!_answerElement.getUnusedStructures().isEmpty()) {
            answerElement.getUnusedStructures().putAll(_answerElement.getUnusedStructures());
          }
          if (!_answerElement.getUndefinedReferences().isEmpty()) {
            answerElement.getUndefinedReferences().putAll(_answerElement.getUndefinedReferences());
          }
        }
      }
    } else {
      answerElement.getFailed().add(_name);
      answerElement
          .getErrors()
          .put(_name, ((BatfishException) _failureCause).getBatfishStackTrace());
    }
  }

  public Map<String, Configuration> getConfigurations() {
    return _configurations;
  }

  @Override
  public BatfishLoggerHistory getHistory() {
    return _history;
  }

  public String getName() {
    return _name;
  }

  @Override
  public String toString() {
    if (_configurations != null) {
      return _configurations.keySet().toString();
    } else {
      return "<EMPTY>";
    }
  }
}

@@ -41,16 +41,44 @@
 il.Map;%0A
+import java.util.Map.Entry;%0A
 import o
@@ -1822,23 +1822,47 @@
 or (
-String hostname
+Entry%3CString, Configuration%3E hostConfig
  : _
@@ -1872,26 +1872,28 @@
 figurations.
-k
 e
+ntr
 ySet()) %7B%0A  
@@ -1902,59 +1902,98 @@
     
-Configuration config = _configurations.get(hostname
+String hostname = hostConfig.getKey();%0A        Configuration config = hostConfig.getValue(
 );%0A 

switch from keySet+get to entrySet()