package com.electrotank.electroserver.plugins;

import com.electrotank.electroserver.plugins.AbstractEventHandler;
import com.electrotank.electroserver.plugins.EventException;
import com.electrotank.electroserver.plugins.LoginEventInterface;
import com.electrotank.electroserver.plugins.utilities.EventHelper;
import com.electrotank.electroserver.plugins.utilities.LoginResponse;

import java.util.HashMap;
import java.util.Map;

/*
 * This is an example event handler for ElectroServer. You can tell the server to use it by 
   including this section in your configuration.xml file.
 
 	<EventHandlers>
	    <EventHandler>
                <Name>Example Login Handler</Name>
                <Type>Java</Type>
                <Event>Login</Event>
                <File>com.electrotank.electroserver.plugins.LoginEventHandlerExample</File>
                <Variables>
                    <Variable>
                        <Name>ExampleVariable</Name>
                        <Value>blah</Value>
                    </Variable>
                </Variables>
            </EventHandler>
	</EventHandlers>
 
 */
public class LoginEventHandlerExample extends AbstractEventHandler implements LoginEventInterface {

    // This behaves the same as the pluginInit method, the parameters from the configuration file
    // are passed in directly to this Map
    public void eventInit(Map parameters) throws EventException {
        // Do nothing in this example
    }
        
    // This method is invoked when a user attempts to log into the server. They are sucessful or not depending
    // on the contents of the LoginResponse object.
    public LoginResponse login(String userName, String password, Map eventVariables) throws EventException {
        
        boolean status = false;
        String message = null;
        boolean moderator = false;
        
        // Get access to the event helper (just like plugin helper, but for event handlers)
        EventHelper helper = getEventHelper();
        
        // This is a VERY basic example, in the real world, you could check against the database or a web service, etc.
        if(userName.equals("TestLogin") && password.equals("TestLoginPassword")) {
            status = true;
            message = "login successful";
            
        // If here, then they didn't log in properly
        } else {
            status = false;
            message = "login invalid!";
        }
        
        // Create the login response for the user
        LoginResponse response = new LoginResponse(status, message);
        
        // Set if this user is a moderator or not
        response.setModerator(moderator);
        
        // These are variables that the server stores that are accessible to other plugins but not users
        // A example would be the userID in the database that plugins need to know to update the users info
        // You can retrieve this data from a plugin with 
        // String value = (String) getUserServerVariable("userName", "Key");
        Map userServerVariables = new HashMap();
        userServerVariables.put("Key", "Value");
        response.setUserServerVariables(userServerVariables);
        
        // These are variables that are returned to the user from the event handler
        // A good example use would be to use this to look up a users personal information from their
        // login and pass it back to the flash client
        Map responseVariables = new HashMap();
        responseVariables.put("Key", "Value");
        response.setResponseVariables(responseVariables);
        
        // Return the response
        return response;
        
    } // end login method
    
}