/*
 * Copyright (C) 2011 Romain Reuillon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openmole.plugin.tool.netlogo6;

import org.nlogo.agent.Observer;
import org.nlogo.agent.World;
import org.nlogo.api.LogoException;
import org.nlogo.api.LogoListBuilder;
import org.nlogo.core.LogoList;
import org.nlogo.headless.HeadlessWorkspace;
import org.nlogo.nvm.Procedure;
import org.nlogo.nvm.RuntimePrimitiveException;
import org.openmole.plugin.tool.netlogo.NetLogo;
import scala.collection.JavaConverters;

import java.util.LinkedList;
import java.util.Map;

/**
 * @author Romain Reuillon
 */
public class NetLogo6 implements NetLogo {

    protected HeadlessWorkspace workspace = null;

    private HeadlessWorkspace getWorkspace() {
        if(workspace == null) {
            workspace = HeadlessWorkspace.newInstance();
        }
        return workspace;
    }

    @Override
    public void open(String script) throws Exception {
        getWorkspace().open(script);
    }

    @Override
    public void command(String cmd) throws Exception {
        getWorkspace().command(cmd);
    }

    @Override
    public boolean isNetLogoException(Throwable e) {
        return LogoException.class.isAssignableFrom(e.getClass());
    }

    @Override
    public Object report(String variable) throws Exception {
        return getWorkspace().report(variable);
    }

    @Override
    public void setGlobal(String variable, Object value) throws Exception {
        if(value instanceof Object[]){
            workspace.world().setObserverVariableByName(variable,arrayToList((Object[]) value));
        }
        else{
            workspace.world().setObserverVariableByName(variable,value);
        }
    }

    @Override
    public void dispose() throws Exception {
        getWorkspace().dispose();
    }

    @Override
    public String[] globals() {
        World world = getWorkspace().world();
        Observer observer = world.observer();
        String nlGlobalList[] = new String[world.getVariablesArraySize(observer)];
        for (int i = 0; i < nlGlobalList.length; i++) {
            nlGlobalList[i] = world.observerOwnsNameAt(i);
        }
        return nlGlobalList;
    }

    public String[] reporters() {
        LinkedList<String> reporters = new LinkedList<String>();
        for (scala.Tuple2<java.lang.String,org.nlogo.nvm.Procedure> e : JavaConverters.asJavaCollectionConverter(getWorkspace().procedures()).asJavaCollection()) {
            if (e._2().isReporter()) reporters.add(e._1());
        }
        return reporters.toArray(new String[0]);
    }

    @Override
    public ClassLoader getNetLogoClassLoader() {
        return HeadlessWorkspace.class.getClassLoader();
    }

    /**
     * Converts an iterable to a LogoList
     * @param array
     * @return
     */
    public static LogoList arrayToList(Object[] array){
        LogoListBuilder list = new LogoListBuilder();
        for(Object o:array){
            if(o instanceof Object[]){list.add(arrayToList((Object[]) o));}
            else{list.add(o);}
        }
        return(list.toLogoList());
    }

}

@@ -1115,16 +1115,53 @@
 rters;%0A%0A
+import java.util.AbstractCollection;%0A
 import j
@@ -1177,24 +1177,24 @@
 LinkedList;%0A
-
 import java.
@@ -1963,22 +1963,31 @@
         
-return
+Object result =
  getWork
@@ -2008,24 +2008,173 @@
 (variable);%0A
+        if(result instanceof LogoList)%7B%0A            return listToCollection((LogoList) result);%0A        %7Delse %7B%0A            return result;%0A        %7D%0A
     %7D%0A%0A    @
@@ -3604,21 +3604,22 @@
 */%0A    p
-ublic
+rivate
  static 
@@ -3898,10 +3898,344 @@
 %0A    %7D%0A%0A
+    private static AbstractCollection listToCollection(LogoList list)%7B%0A        AbstractCollection collection = new LinkedList();%0A        for(Object o:list.toJava())%7B%0A            if(o instanceof LogoList)%7Bcollection.add(listToCollection((LogoList) o));%7D%0A            else%7Bcollection.add(o);%7D%0A        %7D%0A        return(collection);%0A    %7D%0A
 %7D%0A

patch NetLogo6