package kohl.hadrien.vtl.script;

/*-
 * #%L
 * java-vtl-script
 * %%
 * Copyright (C) 2016 Hadrien Kohl
 * %%
 * 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.
 * #L%
 */

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import kohl.hadrien.VTLLexer;
import kohl.hadrien.VTLParser;
import kohl.hadrien.vtl.script.connector.Connector;
import kohl.hadrien.vtl.script.visitors.AssignmentVisitor;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import javax.script.*;
import java.io.IOException;
import java.io.Reader;

/**
 * A VTL {@link ScriptEngine} implementation.
 */
public class VTLScriptEngine extends AbstractScriptEngine {

    private final ImmutableList<Connector> connectors;

    /**
     * Create a new engine instance.
     *
     * @param connectors connectors that will be used to retrieve data.
     */
    public VTLScriptEngine(Connector... connectors) {
        this.connectors = ImmutableList.copyOf(connectors);

    }

    /**
     * Create a new engine instance.
     *
     * @param n          the bindings to use.
     * @param connectors connectors that will be used to retrieve data.
     */
    public VTLScriptEngine(Bindings n, Connector... connectors) {
        super(n);
        this.connectors = ImmutableList.copyOf(connectors);
    }

    @Override
    public Object eval(String script, ScriptContext context) throws ScriptException {
        VTLLexer lexer = new VTLLexer(new ANTLRInputStream(script));
        VTLParser parser = new VTLParser(new CommonTokenStream(lexer));

        ParseTree start = parser.start();
        // Run loop.
        AssignmentVisitor assignmentVisitor = new AssignmentVisitor(context, connectors);
        return assignmentVisitor.visit(start);
    }

    @Override
    public Object eval(Reader reader, ScriptContext context) throws ScriptException {
        try {
            VTLLexer lexer = new VTLLexer(new ANTLRInputStream(reader));
            return null;
        } catch (IOException ioe) {
            throw new ScriptException(ioe);
        }
    }

    @Override
    public Bindings createBindings() {
        return new SimpleBindings(Maps.newConcurrentMap());
    }

    @Override
    public ScriptEngineFactory getFactory() {
        return null;
    }
}

@@ -761,16 +761,45 @@
 t.Maps;%0A
+import kohl.hadrien.Dataset;%0A
 import k
@@ -1058,52 +1058,8 @@
 eam;
-%0Aimport org.antlr.v4.runtime.tree.ParseTree;
 %0A%0Aim
@@ -1129,16 +1129,45 @@
 .Reader;
+%0Aimport java.io.StringReader;
 %0A%0A/**%0A *
@@ -2006,32 +2006,213 @@
 riptException %7B%0A
+        return eval(new StringReader(script), context);%0A    %7D%0A%0A    @Override%0A    public Object eval(Reader reader, ScriptContext context) throws ScriptException %7B%0A        try %7B%0A    
         VTLLexer
@@ -2258,18 +2258,22 @@
 eam(
-script));%0A
+reader));%0A    
     
@@ -2353,17 +2353,34 @@
     
-ParseTree
+    VTLParser.StartContext
  sta
@@ -2408,16 +2408,20 @@
         
+    
 // Run l
@@ -2425,16 +2425,20 @@
 n loop.%0A
+    
         
@@ -2531,151 +2531,114 @@
     
-return assignmentVisitor.visit(start)
+    Dataset last = null
 ;%0A    
-%7D%0A%0A
     
-@Override%0A    public Object eval(Reader reader, ScriptContext context) throws ScriptException
+    for (VTLParser.StatementContext statementContext : start.statement())
  %7B%0A 
@@ -2640,38 +2640,32 @@
 ) %7B%0A        
-try %7B%0A
         
     VTLLexer
@@ -2656,73 +2656,72 @@
     
-    VTLLexer lexer = new VTLLexer(new ANTLRInputStream(reader)
+last = assignmentVisitor.visit(statementContext
 );%0A
+            %7D%0A
     
@@ -2731,28 +2731,28 @@
      return 
-null
+last
 ;%0A        %7D 

Support for multi-statement expressions