/**
 * 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.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 */
package org.thingml.compilers.javascript.browser;

import org.thingml.compilers.Context;
import org.thingml.compilers.javascript.JSThingActionCompiler;
import org.thingml.xtext.helpers.AnnotatedElementHelper;
import org.thingml.xtext.thingML.Expression;
import org.thingml.xtext.thingML.SendAction;

/**
 * Created by bmori on 01.12.2014.
 */
public class BrowserThingActionCompiler extends JSThingActionCompiler {

    @Override
    public void generate(SendAction action, StringBuilder builder, Context ctx) {
    	if(!AnnotatedElementHelper.isDefined(action.getPort(), "sync_send", "true")) {
    		builder.append("setTimeout(() => ");
    	}
    	builder.append(ctx.getContextAnnotation("thisRef"));
        builder.append("bus.emit('" + action.getPort().getName() + "?" + action.getMessage().getName() + "'");
        for (Expression pa : action.getParameters()) {
            builder.append(", ");
            generate(pa, builder, ctx);
        }
        builder.append(")");
    	if(!AnnotatedElementHelper.isDefined(action.getPort(), "sync_send", "true")) {
            builder.append(", 0)");
    	}
        builder.append(";\n");
    }
}

@@ -925,16 +925,68 @@
 ession;%0A
+import org.thingml.xtext.thingML.PropertyReference;%0A
 import o
@@ -1231,24 +1231,30 @@
 text ctx) %7B%0A
+    %09%0A
     %09if(!Ann
@@ -1335,349 +1335,1069 @@
   %09%09
-builder.append(%22setTimeout(() =%3E %22);%0A    %09%7D%0A    %09builder.append(ctx.getContextAnnotation(%22thisRef%22));%0A        builder.append(%22bus.emit('%22 + action.getPort().getName() + %22?%22 + action.getMessage().getName() + %22'%22);%0A        for (Expression pa : action.getParameters()) %7B%0A            builder.append(%22, %22);%0A            generate(pa, builder, ctx);
+for(Expression pa : action.getParameters()) %7B%0A        %09%09if (pa instanceof PropertyReference) %7B%0A        %09%09%09PropertyReference pr = (PropertyReference)pa;%0A        %09%09%09builder.append(%22const %22 + pr.getProperty().getName() + %22_const = %22);%0A        %09%09%09generate(pa, builder, ctx);%0A        %09%09%09builder.append(%22;%5Cn%22);%0A        %09%09%7D%0A        %09%7D%0A    %09%09builder.append(%22setTimeout(() =%3E %22);%0A    %09%7D%0A    %09builder.append(ctx.getContextAnnotation(%22thisRef%22));%0A        builder.append(%22bus.emit('%22 + action.getPort().getName() + %22?%22 + action.getMessage().getName() + %22'%22);%0A        for (Expression pa : action.getParameters()) %7B%0A            builder.append(%22, %22);%0A            if (pa instanceof PropertyReference) %7B%0A    %09%09%09PropertyReference pr = (PropertyReference)pa;%0A            %09if(!AnnotatedElementHelper.isDefined(action.getPort(), %22sync_send%22, %22true%22)) %7B%0A            %09%09builder.append(pr.getProperty().getName() + %22_const%22);%0A            %09%7Delse %7B%0A            %09%09generate(pa, builder, ctx);%0A            %09%7D %0A            %7D%0A            else %7B%0A        %09%09generate(pa, builder, ctx);%0A        %09%7D
 %0A   

fix bug in send action in JS (browser)