/**
 * TooterShooterEnum.java (com.tootsville.user)
 * 
 * Project: com.tootsville.user
 * 
 * Copyright © 2010, Bruce-Robert Pocock.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author brpocock
 *
 * Created: Jan 8, 2010 4:45:41 PM
 * 
 */
package com.tootsville.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.sql.SQLPeerEnum;

/**
 *
 * WRITEME: The documentation for this type (TooterShooterEnum) is incomplete. (brpocock, Jan 8, 2010)
 *
 * @author brpocock
 *
 */
public class TooterShooterEnum extends SQLPeerEnum {

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * gunEnumeration (TooterShooterEnum)
	 */
	private final static HashMap <Integer, TooterShooter> gunEnumeration = new HashMap <Integer, TooterShooter> ();
	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 *
	 * serialVersionUID (long)
	 */
	private static final long serialVersionUID = 70521255128823135L;

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 *
	 * @param id WRITEME
	 * @return WRITEME
	 */
	public static TooterShooter getTooterShooter (final int id) {
		/* Dummy object to be sure that the cache has been primed */
		final TooterShooterEnum n = new TooterShooterEnum ();
		n.prepCache ();
		return TooterShooterEnum.gunEnumeration.get(id);
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 *
	 * @param toot WRITEME
	 */
	public static void sendGunRunner (final Toot toot) {
		/* Dummy object to be sure that the cache has been primed */
		final TooterShooterEnum n = new TooterShooterEnum ();
		n.prepCache ();
		final JSONObject gunRunner = new JSONObject();
		try {
			gunRunner.put ("from", "gunrunner");
			gunRunner.put("status",true);
			final JSONObject armory = new JSONObject ();
			for ( final TooterShooter gun: TooterShooterEnum.gunEnumeration.values ()) {
				armory.put(String.valueOf(gun.getID()), gun.toJSON ());
			}
			gunRunner.put ("guns", armory);
		} catch (final JSONException e) {
			AppiusClaudiusCaecus.reportBug ("Caught a JSONException in sendGunRunner", e);
		}
		toot.sendResponse (gunRunner, -1);
	}

	/**
	 * WRITEME
	 */
	public TooterShooterEnum () {
		super (TooterShooterEnum.class);
	}

	/**
	 * WRITEME
	 * 
	 * @param id unique ID of a gun
	 */
	public TooterShooterEnum (final int id) {
		super (TooterShooterEnum.class, id);
	}

	/**
	 * This is an overriding method.
	 *
	 * @see org.starhope.appius.sql.SQLPeerDatum#flush()
	 */
	@Override
	public void flush () {
		// TODO Auto-generated method stub (brpocock, Jan 8, 2010)

	}

	/**
	 * This is an overriding method.
	 * @see org.starhope.appius.sql.SQLPeerEnum#getStatement(java.sql.Connection)
	 */
	@Override
	protected PreparedStatement getStatement (final Connection connection) {
		try {
			return connection.prepareStatement ("SELECT * FROM tootGuns");
		} catch (final SQLException e) {
			throw AppiusClaudiusCaecus.fatalBug ("Caught a SQLException in getStatement", e);
		}
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	public TooterShooter getTooterShooter () {
		return TooterShooterEnum.gunEnumeration.get (instance);
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#set(java.sql.ResultSet)
	 */
	@Override
	protected void set (final ResultSet rs) throws SQLException {
		TooterShooterEnum.gunEnumeration.put (rs.getInt ("id"),
				new TooterShooter (rs));
	}

}
