package com.marianhello.bgloc.cordova.headless;

import android.content.Context;

import com.evgenii.jsevaluator.JsEvaluator;
import com.evgenii.jsevaluator.interfaces.JsCallback;
import com.marianhello.bgloc.cordova.PluginRegistry;
import com.marianhello.bgloc.headless.AbstractTaskRunner;
import com.marianhello.bgloc.headless.Task;

public class JsEvaluatorTaskRunner extends AbstractTaskRunner {
    private JsEvaluator mJsEvaluator;
    public static String BUNDLE_KEY = "JS";

    public JsEvaluatorTaskRunner(Context context) {
        mJsEvaluator = new JsEvaluator(context);
    }

    @Override
    public void runTask(final Task task) {
        String headlessTask = PluginRegistry.getInstance().getHeadlessTask();

        if (headlessTask == null) {
            task.onError("Cannot run task due missing jsEvaluator. Did you called setFunction?");
            return;
        }

        JsCallback callback = new JsCallback() {
            @Override
            public void onResult(String value) {
                task.onResult(value);
            }

            @Override
            public void onError(String errorMessage) {
                task.onError(errorMessage);
            }
        };

        String jsTask = new StringBuilder()
                .append("function task(name, paramsString) {")
                .append("var params = JSON.parse(paramsString);")
                .append("var task = { name: name, params: params };")
                .append("return(" + headlessTask + ")(task);")
                .append("}").toString();

        mJsEvaluator.callFunction(jsTask, callback, "task", task.getName(), task.toString());
    }
}

@@ -513,80 +513,11 @@
 ner(
-Context context) %7B%0A        mJsEvaluator = new JsEvaluator(context);%0A    
+) %7B
 %7D%0A%0A 
@@ -1583,11 +1583,162 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void setContext(Context context) %7B%0A        super.setContext(context);%0A        mJsEvaluator = new JsEvaluator(context);%0A    %7D
 %0A%7D%0A

fix JsEvaluatorTaskRunner zero arg constructor