/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.aspects.dbc.condition.parser;

/**
 *
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.1.8.3 $
 */
public class BeanshellGenerator implements ExpressionVisitor
{
   Expression expr;
   StringBuffer code;
   int indent;
   //int level;
   
   public BeanshellGenerator(Expression expr)
   {
      this.expr = expr;
   }
   
   public String createBeanshellCode()
   {
      code = new StringBuffer();
      //code.append("boolean " + RET_NAME + " = false;\n");
      expr.accept(this);
      return code.toString();
   }
   
   public void visit(Expression expr)
   {
      expr.accept(this);
   }

   public void visit(BooleanExpression expr)
   {
      if (expr.isTopLevel())
      {
         code.append("return (" + expr.getExpression() + ");\n");
      }
      else
      {
         code.append(expr.getExpression());
      }
   }

   public void visit(ForAllExpression expr)
   {
      createLoopCode(expr, false);
   }

   public void visit(ExistsExpression expr)
   {
      createLoopCode(expr, true);
   }

   public void visit(ImpliesExpression expr)
   {
      indent();
      code.append("if (");
      expr.getLhs().accept(this);
      code.append("){\n");
      indent++;
      indent();
      code.append("if(!(");
      expr.getRhs().accept(this);
      code.append(")){\n");
      indent++;
      indent();
      code.append("return false;\n");
      indent--;
      indent();
      code.append("}\n");
      
      indent--;
      indent();
      code.append("}\n");
      
      if (expr.isTopLevel())
      {
         code.append("return true;\n");         
      }
   }
   
   public void visit(JavaExpression expr)
   {
      code.append(expr.getJava());
      code.append("\nreturn true;\n");         
   }
   
   private void indent()
   {
      for (int i = 0 ; i < indent * 2 ; i++)
      {
         code.append(" ");
      }
   }

   private void createLoopCode(LoopExpression expr, boolean breakType)
   {
      String declaration = expr.getDeclaration();
      String collection = expr.getCollection();
      Expression body = expr.getBody();
      
      indent();
      code.append("for (" + declaration + " : " + collection + "){\n");
      indent++;

      if (body instanceof BooleanExpression)
      {
         indent();
         code.append("if (");
         
         if (!breakType) 
         {
            code.append("!");
         }

         code.append("(");         
       	body.accept(this);
         code.append(")){\n");

         indent++;
         indent();
         code.append("return "+ breakType + ";\n");
         
         indent--;
         indent();         
         code.append("}\n");
      }
      else if (body instanceof ImpliesExpression)
      {
         body.accept(this);
      }
      else
      {
       	body.accept(this);
      }
      indent--;
      indent();
      code.append("}\n");
      if (expr.isTopLevel())
      {
         indent();
         code.append("return "+ !breakType + ";\n");         
      }
   }
}
