/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.debugger.memory.filtering;

import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.engine.SuspendContextImpl;
import com.intellij.debugger.engine.evaluation.EvaluateException;
import com.intellij.debugger.engine.evaluation.EvaluationContextImpl;
import com.intellij.debugger.engine.evaluation.TextWithImportsImpl;
import com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator;
import com.intellij.debugger.engine.events.DebuggerContextCommandImpl;
import com.intellij.debugger.ui.tree.render.CachedEvaluator;
import com.intellij.openapi.project.Project;
import com.intellij.xdebugger.XExpression;
import com.sun.jdi.BooleanValue;
import com.sun.jdi.ObjectReference;
import com.sun.jdi.Value;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Vitaliy.Bibaev
 */
public class ConditionCheckerImpl implements ConditionChecker {
  private final DebugProcessImpl myDebugProcess;
  private final MyCachedEvaluator myEvaluator;
  private final AtomicReference<CheckingResult> myResultReference = new AtomicReference<>();

  public ConditionCheckerImpl(@NotNull DebugProcessImpl debugProcess,
                              @Nullable XExpression expression,
                              @NotNull String className) {
    myDebugProcess = debugProcess;
    myEvaluator = new MyCachedEvaluator(myDebugProcess.getProject(), className);
    myEvaluator.setReferenceExpression(TextWithImportsImpl.fromXExpression(expression));
  }

  @Override
  public CheckingResult check(@NotNull ObjectReference ref) {
    myDebugProcess.getManagerThread().invokeAndWait(new MyCheckerCommand(ref));
    return myResultReference.get();
  }

  private class MyCheckerCommand extends DebuggerContextCommandImpl {
    private final ObjectReference myReference;

    protected MyCheckerCommand(@NotNull ObjectReference ref) {
      super(myDebugProcess.getDebuggerContext());
      myReference = ref;
    }

    @Override
    public Priority getPriority() {
      return Priority.LOWEST;
    }

    @Override
    public void threadAction(@NotNull SuspendContextImpl suspendContext) {
      ExpressionEvaluator evaluator;
      try {
        evaluator = myEvaluator.getEvaluator();
        Value result =
          evaluator.evaluate(new EvaluationContextImpl(suspendContext, suspendContext.getFrameProxy(), myReference));
        if (result instanceof BooleanValue && ((BooleanValue)result).value()) {
          myResultReference.set(CheckingResultImpl.SUCCESS);
        }
        else {
          myResultReference.set(CheckingResultImpl.FAIL);
        }
      }
      catch (EvaluateException e) {
        myResultReference.set(CheckingResultImpl.error(e.getMessage()));
      }
    }
  }

  private static class MyCachedEvaluator extends CachedEvaluator {
    private final Project myProject;
    private final String myClassName;

    public MyCachedEvaluator(@NotNull Project project, @NotNull String className) {
      super();
      myProject = project;
      myClassName = className;
    }

    ExpressionEvaluator getEvaluator() throws EvaluateException {
      return getEvaluator(myProject);
    }

    @Override
    protected String getClassName() {
      return myClassName;
    }
  }
}

@@ -21,9 +21,9 @@
 -201
-6
+7
  Jet
@@ -1103,24 +1103,75 @@
 ommandImpl;%0A
+import com.intellij.debugger.impl.DebuggerUtilsEx;%0A
 import com.i
@@ -1311,41 +1311,8 @@
 on;%0A
-import com.sun.jdi.BooleanValue;%0A
 impo
@@ -1347,34 +1347,8 @@
 ce;%0A
-import com.sun.jdi.Value;%0A
 impo
@@ -2817,151 +2817,64 @@
     
-ExpressionEvaluator evaluator;%0A      try %7B%0A        evaluator = myEvaluator.getEvaluator();%0A        Value result =%0A          evaluator.evaluate(
+try %7B%0A        EvaluationContextImpl evaluationContext = 
 new 
@@ -2955,17 +2955,16 @@
 ference)
-)
 ;%0A      
@@ -2969,112 +2969,141 @@
     
-if (result instanceof BooleanValue && ((BooleanValue)result).value()) %7B%0A          myResultReference.set(
+myResultReference.set(DebuggerUtilsEx.evaluateBoolean(myEvaluator.getEvaluator(), evaluationContext)%0A                              ? 
 Chec
@@ -3124,18 +3124,16 @@
 .SUCCESS
-);
 %0A       
@@ -3137,18 +3137,20 @@
     
-%7D%0A
+    
         
 else
@@ -3149,15 +3149,8 @@
     
-else %7B%0A
     
@@ -3155,38 +3155,18 @@
         
-myResultReference.set(
+: 
 Checking
@@ -3187,18 +3187,8 @@
 L);%0A
-        %7D%0A
     

use DebuggerUtilsEx.evaluateBoolean to allow conditions returning Boolean