package com.suiton2d.assets.loaders;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.SynchronousAssetLoader;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.suiton2d.assets.JavascriptScript;
import com.suiton2d.assets.Script;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptLoader extends SynchronousAssetLoader<Script, ScriptLoader.ScriptParameter>{

    private static final String TAG = ScriptLoader.class.getSimpleName();

    public ScriptLoader(FileHandleResolver resolver) {
        super(resolver);
    }

    @Override
    public Script load(AssetManager assetManager, String fileName, FileHandle file, ScriptParameter parameter) {
        Script script = null;
        if ("js".equalsIgnoreCase(file.extension())) {
            try {
                script = loadJavascript(file);
            } catch (ScriptException e) {
                Gdx.app.log(TAG, "Failed to load javascript file.", e);
            }
        }

        // TODO: Figure out what needs to be done here in case bad input is provided...
        return script;
    }

    private JavascriptScript loadJavascript(FileHandle file) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");
        engine.eval(file.reader());
        return new JavascriptScript(file.path(), engine);
    }

    @Override
    public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, ScriptParameter parameter) {
        return null;
    }

    public static class ScriptParameter extends AssetLoaderParameters<Script> {

    }
}

@@ -372,16 +372,231 @@
 Handle;%0A
+import com.badlogic.gdx.math.Vector;%0Aimport com.badlogic.gdx.scenes.scene2d.actions.Actions;%0Aimport com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;%0Aimport com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;%0A
 import c
@@ -1828,16 +1828,55 @@
 horn%22);%0A
+        initJavascriptObjects(engine);%0A
         
@@ -1960,32 +1960,641 @@
 engine);%0A    %7D%0A%0A
+    private void initJavascriptObjects(ScriptEngine scriptEngine) throws ScriptException %7B%0A        scriptEngine.put(Actions.class.getSimpleName(), scriptEngine.eval(Actions.class.getName()));%0A        scriptEngine.put(MoveByAction.class.getSimpleName(), scriptEngine.eval(MoveByAction.class.getName()));%0A        scriptEngine.put(MoveToAction.class.getSimpleName(), scriptEngine.eval(MoveToAction.class.getName()));%0A        scriptEngine.put(Vector.class.getSimpleName(), scriptEngine.eval(Vector.class.getName()));%0A%0A        scriptEngine.put(%22Input%22, Gdx.input);%0A        scriptEngine.put(%22Net%22, Gdx.net);%0A    %7D%0A%0A
     @Override%0A  

Initialize javascript scopes with a few classes.