package com.electrotank.electroserver.plugins;

import com.electrotank.electroserver.plugins.AbstractPlugin;
import com.electrotank.electroserver.plugins.PluginException;
import com.electrotank.electroserver.plugins.utilities.PluginHelper;

import java.util.HashMap;
import java.util.Map;

/*
 * This is an example java plugin for ElectroServer. You can tell the server to use it by 
   including this section in your configuration.xml file (inside the <Plugins> node).
 
	<Plugin>
		<Name>SimpleJavaPlugin</Name>
		<Type>Java</Type>
		<Scope>Room</Scope>
		<File>com.electrotank.electroserver.plugins.VariablesExampleJavaPlugin </File>
		<Variables/>
	</Plugin>
 
 */

public class VariablesExampleJavaPlugin extends AbstractPlugin {
    
    // This is a handle for the name of the test variable
    public static final String TEST_VARIABLE_NAME = "TestVariable";
    
    // This contains the value of the variable from the configuration file
    private String testVariableValue = null;
    
    // This is called when the plugin is initialized by the server
    public void pluginInit(Map parameters) throws PluginException {
        
        // Get the value of the test variable
        testVariableValue = (String) parameters.get(TEST_VARIABLE_NAME);
    }
    
    // This is executed when a request is made to the plugin
    public void pluginRequest(Map parameters) throws PluginException {
        
        // Get the name of who called this plugin
        String caller = (String)parameters.get(EXECUTING_USER_NAME);
        
        // Get the plugin helper
        PluginHelper helper = getPluginHelper();
        
        // Create the variables to send back to the user
        Map variables = new HashMap();
        variables.put(TEST_VARIABLE_NAME, testVariableValue);
        
        // Send the caller the results
        helper.sendPrivateMessage(caller, "Results", variables);
        
    }
    
    public void pluginDestroy() throws PluginException {
        // Do nothing in this case
    }
    
}