package org.nuiton.wikitty.publication.engine;

import java.util.Arrays;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author poussin
 * @version $Revision$
 *
 * Last update: $Date$
 * by : $Author$
 */
public class WPScriptEngineFactory implements ScriptEngineFactory {

    /** to use log facility, just put in your code: log.info(\"...\"); */
    static private Log log = LogFactory.getLog(WPScriptEngineFactory.class);

    static final protected List<String> extensions = Arrays.asList("wp");
    static final protected List<String> mimeTypes = Arrays.asList("text/wp");
    static final protected List<String> names = Arrays.asList("wp");
    
    @Override
    public String getEngineName() {
        return "Wikitty Publication Script Engin";
    }

    @Override
    public String getEngineVersion() {
        return "0.1";
    }

    @Override
    public List<String> getExtensions() {
        return extensions;
    }

    @Override
    public List<String> getMimeTypes() {
        return mimeTypes;
    }

    @Override
    public List<String> getNames() {
        return names;
    }

    @Override
    public String getLanguageName() {
        return "wp";
    }

    @Override
    public String getLanguageVersion() {
        return "0.1";
    }

    @Override
    public Object getParameter(String key) {
        Object result = null;
        if (ScriptEngine.ENGINE.equals(key)) {
            result = getEngineName();
        } else if (ScriptEngine.ENGINE_VERSION.equals(key)) {
            result = getEngineVersion();
        } else if (ScriptEngine.NAME.equals(key)) {
            result = getNames();
        } else if (ScriptEngine.LANGUAGE.equals(key)) {
            result = getLanguageName();
        } else if (ScriptEngine.LANGUAGE_VERSION.equals(key)) {
            result = getLanguageVersion();
        }
        return result;
    }

    @Override
    public String getMethodCallSyntax(String obj, String m, String... args) {
        String result = obj;
        result += "." + m;
        for (int i = 0; i < args.length; i++) {
            result += "," + args[i];
        }
        result.replaceFirst(",", "(");
        return result;
    }

    @Override
    public String getOutputStatement(String toDisplay) {
        String result = "System.out.println(" + toDisplay + ");";
        return result;
    }

    @Override
    public String getProgram(String... statements) {
        String result = "<%\n";
        for (String s : statements) {
            result += s + ";\n";
        }
        result += "%>";
        return result;
    }

    @Override
    public ScriptEngine getScriptEngine() {
        return new WPScriptEngine();
    }

}
