/*
 * This file is part of GenericsLib for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


package com.jcwhatever.bukkit.generic.scripting;

import com.jcwhatever.bukkit.generic.scripting.api.IScriptApi;
import com.jcwhatever.bukkit.generic.utils.PreCon;

import java.util.Collection;
import javax.annotation.Nullable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

/**
 * Generics default {@code IScript} implementation
 */
public class GenericsScript implements IScript {

    private final GenericsScriptManager _manager;
    private final String _name;
    private final String _type;
    private final String _script;

    /**
     * Constructor.
     *
     * @param name    The name of the script.
     * @param type    The script type.
     * @param script  The script source.
     */
    public GenericsScript(GenericsScriptManager manager, String name, String type, String script) {
        PreCon.notNull(manager);
        PreCon.notNullOrEmpty(name);
        PreCon.notNullOrEmpty(type);
        PreCon.notNull(script);

        _manager = manager;
        _name = name;
        _type = type;
        _script = script;
    }

    /**
     * Get the name of the script.
     */
    @Override
    public String getName() {
        return _name;
    }

    /**
     * Get the script source.
     */
    @Override
    public String getScript() {
        return _script;
    }

    /**
     * Get the script type. (file extension)
     */
    @Override
    public String getType() {
        return _type;
    }

    /**
     * Evaluate the script.
     *
     * @param apiCollection  The api to include.
     */
    @Override
    @Nullable
    public IEvaluatedScript evaluate(@Nullable Collection<? extends IScriptApi> apiCollection) {

        ScriptEngine engine = _manager.getEngineManager().getEngineByExtension(getType());
        if (engine == null)
            return null;

        GenericsEvaluatedScript script = new GenericsEvaluatedScript(this, engine, apiCollection);

        try {
            // evaluate script
            engine.eval(getScript());

            return script;

        } catch (ScriptException e) {
            e.printStackTrace();
            return null;
        }
    }

}

@@ -1477,16 +1477,57 @@
 Engine;%0A
+import javax.script.ScriptEngineManager;%0A
 import j
@@ -1841,32 +1841,98 @@
 tructor.%0A     *%0A
+     * %3Cp%3EIs evaluated with the global engine manager.%3C/p%3E%0A     *%0A
      * @param na
@@ -2075,16 +2075,402 @@
 sScript(
+String name, String type, String script) %7B%0A        this(null, name, type, script);%0A    %7D%0A%0A%0A    /**%0A     * Constructor.%0A     *%0A     * @param manager  The scripts owning manager. Script engine from manager is used.%0A     * @param name     The name of the script.%0A     * @param type     The script type.%0A     * @param script   The script source.%0A     */%0A    public GenericsScript(@Nullable 
 Generics
@@ -2539,41 +2539,8 @@
 ) %7B%0A
-        PreCon.notNull(manager);%0A
     
@@ -3388,35 +3388,648 @@
 e = 
-_manager.getEngineManager()
+getScriptEngine();%0A        if (engine == null) %7B%0A            return null;%0A        %7D%0A%0A        IEvaluatedScript script = instantiateEvaluatedScript(engine, apiCollection);%0A%0A        if (!eval(engine)) %7B%0A            return null;%0A        %7D%0A%0A        return script;%0A    %7D%0A%0A    /**%0A     * Get a script engine for the script.%0A     *%0A     * @return  Null if an engine could not be found.%0A     */%0A    @Nullable%0A    protected ScriptEngine getScriptEngine() %7B%0A        ScriptEngineManager engineManager = _manager != null%0A                ? _manager.getEngineManager()%0A                : ScriptHelper.getGlobalEngineManager();%0A%0A        return engineManager
 .get
@@ -4066,157 +4066,722 @@
     
+%7D%0A%0A
     
-if (engine == null)%0A 
+/**%0A     * Called to instantiate a new evaluated script.%0A
      
+*%0A
      
- return null;%0A%0A        GenericsEvaluatedScript script = new GenericsEvaluatedScript(this, engine, apiCollection);%0A
+* @param engine         The scripts engine.%0A     * @param apiCollection  Optional initial api collection.%0A     */%0A    protected IEvaluatedScript instantiateEvaluatedScript(ScriptEngine engine,%0A                                                          @Nullable Collection%3C? extends IScriptApi%3E apiCollection) %7B%0A        return new GenericsEvaluatedScript(this, engine, apiCollection);%0A    %7D%0A%0A    /**%0A     * Called to evaluate the script into the specified engine.%0A     *%0A     * @param engine  The engine to evaluate the script into.%0A     *%0A     * @return Null if evaluation failed.%0A     */%0A    protected boolean eval(ScriptEngine engine) %7B
 %0A   
@@ -4880,22 +4880,20 @@
  return 
-script
+true
 ;%0A%0A     
@@ -4969,36 +4969,37 @@
          return 
-null
+false
 ;%0A        %7D%0A    

add additional constructor