package org.batfish.datamodel.answers;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Nullable;
import org.batfish.common.BatfishException;
import org.batfish.common.BatfishException.BatfishStackTrace;
import org.batfish.common.BfConsts;
import org.batfish.common.QuestionException;
import org.batfish.datamodel.questions.Question;

public class Answer {

  public static Answer failureAnswer(String message, @Nullable Question question) {
    Answer answer = new Answer();
    answer.setQuestion(question);
    answer.setStatus(AnswerStatus.FAILURE);
    answer.addAnswerElement(new StringAnswerElement(message));
    return answer;
  }

  protected List<AnswerElement> _answerElements = new LinkedList<>();

  private Question _question;

  private AnswerStatus _status;

  private AnswerSummary _summary = new AnswerSummary();

  public void addAnswerElement(AnswerElement answerElement) {
    _answerElements.add(answerElement);
    _summary.combine(answerElement.getSummary());
  }

  public void append(Answer answer) {
    if (answer._question != null) {
      _question = answer._question;
    }
    _answerElements.addAll(answer._answerElements);
    _status = answer._status;
    _summary.combine(answer.getSummary());
    for (AnswerElement answerElement : answer._answerElements) {
      if (answerElement instanceof BatfishStackTrace) {
        BatfishException e = ((BatfishStackTrace) answerElement).getException();
        throw new QuestionException("Exception answering question", e, this);
      }
    }
  }

  @JsonProperty(BfConsts.PROP_ANSWER_ELEMENTS)
  public List<AnswerElement> getAnswerElements() {
    return _answerElements;
  }

  @JsonProperty(BfConsts.PROP_QUESTION)
  public Question getQuestion() {
    return _question;
  }

  @JsonProperty(BfConsts.PROP_SUMMARY)
  public AnswerSummary getSummary() {
    return _summary;
  }

  @JsonProperty(BfConsts.PROP_STATUS)
  public AnswerStatus getStatus() {
    return _status;
  }

  public String prettyPrint() {
    StringBuilder string = new StringBuilder();
    if (_status != null) {
      string.append("Status: " + _status + "\n");
    }
    if (_question != null) {
      string.append("Question: " + _question.prettyPrint() + "\n");
    }
    for (AnswerElement ae : _answerElements) {
      string.append(ae.prettyPrint() + "\n");
    }
    if (_summary != null) {
      string.append("Summary: " + _summary.prettyPrint() + "\n");
    }
    return string.toString();
  }

  public Answer prettyPrintAnswer() {
    Answer answer = new Answer();
    answer.setQuestion(_question);
    answer.setStatus(_status);

    for (AnswerElement ae : _answerElements) {
      String aePrettyStr = ae.prettyPrint();
      AnswerElement prettyAnswerElement = new StringAnswerElement(aePrettyStr);
      answer.addAnswerElement(prettyAnswerElement);
    }

    return answer;
  }

  @JsonProperty(BfConsts.PROP_ANSWER_ELEMENTS)
  public void setAnswerElements(List<AnswerElement> answerElements) {
    _answerElements = answerElements;
  }

  @JsonProperty(BfConsts.PROP_QUESTION)
  public void setQuestion(Question question) {
    _question = question;
  }

  @JsonProperty(BfConsts.PROP_STATUS)
  public void setStatus(AnswerStatus status) {
    _status = status;
  }

  @JsonProperty(BfConsts.PROP_SUMMARY)
  public void setSummary(AnswerSummary summary) {
    _summary = summary;
  }

  @Override
  public String toString() {
    return prettyPrint();
  }
}

@@ -2543,402 +2543,8 @@
  %7D%0A%0A
-  public Answer prettyPrintAnswer() %7B%0A    Answer answer = new Answer();%0A    answer.setQuestion(_question);%0A    answer.setStatus(_status);%0A%0A    for (AnswerElement ae : _answerElements) %7B%0A      String aePrettyStr = ae.prettyPrint();%0A      AnswerElement prettyAnswerElement = new StringAnswerElement(aePrettyStr);%0A      answer.addAnswerElement(prettyAnswerElement);%0A    %7D%0A%0A    return answer;%0A  %7D%0A%0A
   @J

Remove unused prettyPrint function from Answer (#2182)