package com.netflix.conductor.contribs.lambda;

import com.netflix.conductor.common.metadata.tasks.Task;
import com.netflix.conductor.common.run.Workflow;
import com.netflix.conductor.core.events.ScriptEvaluator;
import com.netflix.conductor.core.execution.WorkflowExecutor;
import com.netflix.conductor.core.execution.tasks.WorkflowSystemTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;
import javax.script.ScriptException;
import java.util.Map;

@Singleton
public class Lambda extends WorkflowSystemTask {

    private static final Logger logger = LoggerFactory.getLogger(Lambda.class);
    private static final String QUERY_EXPRESSION_PARAMETER = "scriptExpression";

    public Lambda() {
        super("SCRIPT_TASK");
        logger.info("SCRIPT_TASK initialized...");
    }


    @Override
    public void start(Workflow workflow, Task task, WorkflowExecutor executor) {
        Map<String, Object> taskInput = task.getInputData();
        Map<String, Object> taskOutput = task.getOutputData();

        String scriptExpression = (String) taskInput.get(QUERY_EXPRESSION_PARAMETER);

        if(scriptExpression == null) {
            task.setReasonForIncompletion("Missing '" + QUERY_EXPRESSION_PARAMETER + "' in input parameters");
            task.setStatus(Task.Status.FAILED);
            return;
        }

        String scriptExpressionBuilder = "function scriptFun(){" +
                scriptExpression +
                "} scriptFun();";

        logger.info("scriptExpressionBuilder: {}" , scriptExpressionBuilder);

        try {
            Object returnValue = null;
            returnValue = ScriptEvaluator.eval(scriptExpressionBuilder, taskInput);
            taskOutput.put("result", returnValue);
            task.setStatus(Task.Status.COMPLETED);
        } catch (ScriptException e) {
            logger.error(e.getMessage(), e);
            task.setStatus(Task.Status.FAILED);
            task.setReasonForIncompletion(e.getMessage());
            taskOutput.put("error", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        }

    }


}

@@ -747,27 +747,22 @@
  super(%22
-SCRIPT_TASK
+LAMBDA
 %22);%0A    
@@ -782,19 +782,19 @@
 fo(%22
-SCRIPT_TASK
+LAMBDA task
  ini

rename task name