package com.intellij.debugger.streams.trace.smart.handler;

import com.intellij.debugger.streams.wrapper.MethodCall;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author Vitaliy.Bibaev
 */
public class PeekTracerHandler extends HandlerBase {
  private final HashMapVariableImpl myBeforeVariable;
  private final HashMapVariableImpl myAfterVariable;

  public PeekTracerHandler(int num, @NotNull String name) {
    final String variablePrefix = String.format("%sPeek%d", name, num);
    myBeforeVariable = new HashMapVariableImpl(variablePrefix + "before", "Integer", "Object", true);
    myAfterVariable = new HashMapVariableImpl(variablePrefix + "after", "Integer", "Object", true);
  }

  @NotNull
  @Override
  public List<MethodCall> additionalCallsBefore() {
    final String beforeMapName = myBeforeVariable.getName();
    return Collections.singletonList(new PeekCall(String.format("x -> %s.put(time.addAndGet(), x)", beforeMapName)));
  }

  @NotNull
  @Override
  public List<MethodCall> additionalCallsAfter() {
    final String afterMapName = myBeforeVariable.getName();
    return Collections.singletonList(new PeekCall(String.format("x -> %s.put(time.addAndGet(), x)", afterMapName)));
  }

  @NotNull
  @Override
  public String prepareResult() {
    final String beforeConversion = myBeforeVariable.convertToArray("beforeArray");
    final String afterConversion = myAfterVariable.convertToArray("afterArray");
    return beforeConversion + System.lineSeparator() + afterConversion;
  }

  @NotNull
  @Override
  public String getResultExpression() {
    return "new Object[] {beforeArray, afterArray}";
  }

  @NotNull
  @Override
  protected List<Variable> getVariables() {
    return Arrays.asList(myBeforeVariable, myAfterVariable);
  }
}

@@ -976,39 +976,33 @@
  -%3E %25s.put(time.
-addAndG
+g
 et(), x)%22, befor

Fix expression: do not increment a time variable in 'before' peek