package lc.mecha.lang;

import lc.mecha.log.MechaLogger;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public class ScriptUtil {
    public static void printEngineTypes(MechaLogger logger) {
        for (ScriptEngineFactory sef : new ScriptEngineManager().getEngineFactories()) {
            logger.info("Script engine [name: {}] [version: {}] [lang: {}] [lang ver: {}] [mime: {}]",
                    sef.getEngineName(), sef.getEngineVersion(), sef.getLanguageName(), sef.getLanguageVersion(),
                    sef.getMimeTypes());
        }
    }

    /**
     * For the given language represented by this script engine, comment the line. The method presumes that the
     * provided line is a single line not containing linefeeds.
     *
     * @param line
     * @param engine
     * @return
     */
    public static String commentLine(String line, ScriptEngine engine) {
        if (engine.getFactory().getLanguageName().equals("ECMAScript")) {
            return "// " + line;
        }

        throw new IllegalArgumentException("Unknown language: " + engine.getFactory().getLanguageName());
    }

    public static String getLanguage(String mimeType) {
        if (mimeType.equals("text/javascript")) return "ECMAScript";

        // TODO Actually search the available factories
        throw new IllegalArgumentException("Unknown mime-type: " + mimeType);
    }
}
