/*
 * Copyright (c) 2006-2010 Shane Mc Cormack
 *
 * 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.dmdirc.addons.scriptplugin;
import java.util.Hashtable;

/**
 * Used to allow the rhino javascript to do stuff that it otherwise can't, such
 * as setting global variables, string triming and getting a char.
 *
 * @author Shane 'Dataforce' McCormack
 */
public class JavaScriptHelper {
    /** Used to identify the JavaScriptHelper. */
    private static final String myIDString = "DMDIRC-JSH";
    
    /** Used to allow scripts to know if a specific function they need is available. */
    private final int myVersion = 2;

    /** Hashtable for storing stufff. */
    private static Hashtable<String,Object> globalSettings = new Hashtable<String,Object>();
    
    /**
     * Method to set Stuff.
     *
     * @param setting Name of setting
     * @param value Value of setting
     */
    public void setGlobal(String setting, Object value) {
        if (setting.equals("")) { return; }
        setting = setting.toLowerCase();
        if (globalSettings.containsKey(setting)) { globalSettings.remove(setting); }
        if (value != null) { globalSettings.put(setting,value); }
    }
    
    /**
     * Method to get Stuff.
     *
     * @param setting Name of setting
     * @return Value of setting
     */
    public Object getGlobal(String setting) {
        if (setting.equals("")) { return ""; }
        setting = setting.toLowerCase();
        if (globalSettings.containsKey(setting)) { return globalSettings.get(setting); }
        return null;
    }
    
    /**
     * Method to trim spaces from strings.
     *
     * @param str String to trim
     * @return String without spaces on the ends
     */
    public String trim(String str) { return str.trim(); }
    
    /**
     * Method to get a java Character object.
     *
     * @param str String to make object from
     * @return Character represending first char in the string
     */
    public Character toChar(String str) { return str.charAt(0); }
    
    /**
     * Get the version of JavaScriptHelper in use.
     *
     * @return JavaScriptHelper version
     */
    public int getVersion() { return myVersion; }
    
    /**
     * Get the ID of this JavaScriptHelper.
     * If you extend or modify this, you should change the myIDString variable.
     *
     * @return JavaScriptHelper ID
     */
    public String getID() { return myIDString; }
}