package com.tootsville;

import it.gotoandplay.smartfoxclient.ISFSEventListener;
import it.gotoandplay.smartfoxclient.SFSEvent;
import it.gotoandplay.smartfoxclient.SmartFoxClient;
import it.gotoandplay.smartfoxclient.data.SFSVariable;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TimerTask;

/**
 * Load-testing client
 * 
 * @author brpocock
 * 
 */
public class LoadClient extends TimerTask implements ISFSEventListener {

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 * 
	 * @param args
	 */
	public static void main (final String args[]) {

		for (int number = 0; number < Integer.parseInt (args [0]); ++number) {
			final int x = number;
			final Thread t = new Thread () {
				@Override
				public void run () {
					System.err.println ("$LoadUser." + x + ": running");
					new LoadClient (x);
				}
			};
			System.err.println ("$LoadUser." + number + ": starting");
			t.start ();
			System.err.println ("$LoadUser." + number + ": started");
		}

		System.err.println ("There are now approximately " + args [0]
				+ " simulated users. High water mark success.");

	}

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 */
	Random random = new Random ();

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 */
	SmartFoxClient sfs;

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 */
	boolean talk = false;

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 */
	String userName = "LoadClient";

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 */
	private final int userNumber;

	/**
	 * WRITEME: Document this field. theys Dec 14, 2009
	 * 
	 * @param num
	 */
	public LoadClient (final int num) {
		userNumber = num;
		sfs = new SmartFoxClient (false);
		userName = "$LoadUser." + num;
		sfs.setDebug (false);
		sfs.smartConnect = false; // Don't use BlueBox proxy
		sfs.addEventListener (SFSEvent.onConnection, this);

		System.out
				.println ("$LoadUser."
						+ num
						+ " Connecting to the Smartfox server (Whitney-dev)...");
		sfs.connect ("whitney-dev.tootsville.com");

	}

	/**
	 * This is an overriding method.
	 * 
	 * @see it.gotoandplay.smartfoxclient.ISFSEventListener#handleEvent(it.gotoandplay.smartfoxclient.SFSEvent)
	 */
	public void handleEvent (final SFSEvent event) {
		if (event.getName ().equals (SFSEvent.onConnectionLost)) {
			System.err.println (userName + " Disconnected");
			System.exit (-2);
		} else if (event.getName ().equals (SFSEvent.onConnection)) {
			System.out.println (userName + " Connected. Logging in.");
			sfs.addEventListener (SFSEvent.onConnectionLost, this);
			sfs.addEventListener (SFSEvent.onLogin, this);
			sfs.addEventListener (SFSEvent.onRoomListUpdate, this);
			sfs.addEventListener (SFSEvent.onJoinRoom, this);
			sfs.login ("Lightning", userName, "");
		} else if (event.getName ().equals (SFSEvent.onRoomListUpdate)) {
			System.out.println (userName
					+ " Got room list. Joining Toot Square");
			sfs.joinRoom (userNumber % 35 + 1);
		} else if (event.getName ().equals (SFSEvent.onLogin)) {
			System.out.println (userName
					+ " Logged in. Getting Room List");
			System.err.println (event.toString ());
			sfs.getRoomList ();
		} else if (event.getName ().equals (SFSEvent.onJoinRoom)) {
			/*
			 * System.out.println (userName +
			 * " Entered room. Beginning main loop.");
			 * 
			 * while (true) { run (); }
			 * 
			 * final Timer hammer = new Timer ("LoadClient/hammer " +
			 * userName); hammer.scheduleAtFixedRate (this, 0, 500);
			 */
		} else
			System.out
					.println ("Unhandled event: " + event.toString ());

	}

	/**
	 * This is an overriding method.
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run () {
		if (talk) {
			sfs.sendPublicMessage ("Let's make some noise");
			System.out.println (userName + " talkie");
			talk = false;
		} else {
			Map <String, SFSVariable> vars = new HashMap <String, SFSVariable> ();
			vars.put ("XPos", new SFSVariable ("n", new Double (random
					.nextDouble ()).toString ()));
			vars.put ("YPos", new SFSVariable ("n", new Double (random
					.nextDouble ()).toString ()));
			vars.put ("XVel", new SFSVariable ("n", new Double (random
					.nextDouble ()).toString ()));
			vars.put ("YVel", new SFSVariable ("n", new Double (random
					.nextDouble ()).toString ()));
			vars.put ("files_main",
					new SFSVariable ("s", "zapQuad.swf"));
			vars.put ("TootType", new SFSVariable ("s", "zapQuad.swf"));
			vars.put ("avatar_type", new SFSVariable ("n", "0"));
			vars
					.put (
							"garbage",
							new SFSVariable (
									"s",
									"I want you to know /"
											+ "That I'm happy for you /"
											+ "I wish nothing but /"
											+ "The best for you both /"
											+ "An older version of me /"
											+ "Is she perverted like me /"
											+ "Would she go down on you in the theater? /"
											+ "Does she speak eloquently /"
											+ "And would she have your baby /"
											+ "I'm sure she'd make a fucking excellent mother."));
			sfs.setUserVariables (vars);
			vars = null;
			System.gc ();
			System.out.println (userName + " walkie");
			talk = true;
		}
	}
}
