/**
 * TooterShooter.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:04:58 PM
 * 
 */
package com.tootsville.user;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONException;
import org.json.JSONObject;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.types.Colour;
import org.starhope.appius.util.CastsToJSON;

/**
 * 
 * WRITEME: The documentation for this type (TooterShooter) is
 * incomplete. (brpocock, Jan 8, 2010)
 * 
 * @author brpocock
 * 
 */
public class TooterShooter implements CastsToJSON, Serializable {

	/**
	 * Java serialisation unique ID
	 */
	private static final long serialVersionUID = 2045092550447296495L;
	/**
	 * The number of ammo units expended per shot
	 */
	private int ammoCost;

	/**
	 * The rate at which the bullet moves, in px/s
	 */
	private double bulletSpeed;

	/**
	 * The URL of the bullet artwork
	 */
	private String bulletURL;

	/**
	 * If the bullet is colourised generally, the colour value to be
	 * applied to it. This can be null.
	 */
	private Colour colour;

	/**
	 * The amount of ammunition granted for equipping this gun
	 */
	private int initialAmmo;

	/**
	 * If true, the bullet artwork is rotated relative to the angle of
	 * travel
	 */
	private boolean isRotatedShot;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010) maxAmmo
	 * (TooterShooter)
	 */
	private int maxAmmo;

	/**
	 * The maximum number of simultaneous bullets to be traveling
	 */
	private int maxShots;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * 
	 * myID (TooterShooter)
	 */
	private int myID;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * playerParticleURL (TooterShooter)
	 */
	private String playerParticleURL;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * playerSoundURL (TooterShooter)
	 */
	private String playerSoundURL;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010) shotOffsetX
	 * (TooterShooter)
	 */
	private int shotOffsetX;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010) shotOffsetY
	 * (TooterShooter)
	 */
	private int shotOffsetY;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010) shotRange
	 * (TooterShooter)
	 */
	private double shotRange;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010) shotRate
	 * (TooterShooter)
	 */
	private double shotRate;

	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * targetParticleURL (TooterShooter)
	 */
	private String targetParticleURL;
	/**
	 * WRITEME: document this field (brpocock, Jan 8, 2010)
	 * targetSoundURL (TooterShooter)
	 */
	private String targetSoundURL;

	/**
	 * WRITEME
	 * 
	 * @param rs WRITEME
	 */
	TooterShooter (final ResultSet rs) {
		try {
			myID = rs.getInt ("id");
			setAmmoCost (rs.getInt ("ammoCost"));
			setBulletSpeed (rs.getBigDecimal ("bulletSpeed")
					.doubleValue ());
			setBulletURL (rs.getString ("bulletURL"));
			final int colourValue = rs.getInt ("colour");
			if (rs.wasNull ()) {
				setColour (null);
			} else {
				setColour (new Colour (colourValue));
			}
			setInitialAmmo (rs.getInt ("initialAmmo"));
			setMaxAmmo (rs.getInt ("maxAmmo"));
			setMaxShots (rs.getInt ("maxShots"));
			setPlayerParticleURL (rs.getString ("playerParticleURL"));
			setPlayerSoundURL (rs.getString ("playerSoundURL"));
			setRotatedShot ("Y".equals (rs.getString ("rotateShot")));
			setShotOffsetX (rs.getInt ("shotOffsetX"));
			setShotOffsetY (rs.getInt ("shotOffsetY"));
			setShotRange (rs.getBigDecimal ("shotRange").doubleValue ());
			setShotRate (rs.getBigDecimal ("shotRate").doubleValue ());
			setTargetParticleURL (rs.getString ("targetParticleURL"));
			setTargetSoundURL (rs.getString ("targetSoundURL"));
		} catch (final SQLException e) {
			throw AppiusClaudiusCaecus.fatalBug (
					"Caught a SQLException in TooterShooter", e);
		}

	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return the number of ammo units expended for each shot
	 */
	private int getAmmoCost () {
		return ammoCost;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return px/s
	 */
	private double getBulletSpeed () {
		return bulletSpeed;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private String getBulletURL () {
		return bulletURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private Colour getColor () {
		return colour;
	}

	/**
	 * @return the colour
	 */
	public Colour getColour () {
		// default getter (brpocock, Jan 8, 2010)
		return colour;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	public int getID () {
		return myID;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private int getInitialAmmo () {
		return initialAmmo;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private int getMaxAmmo () {
		return maxAmmo;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return shots on screen at once
	 */
	private int getMaxShots () {
		return maxShots;
	}

	/**
	 * @return the playerParticleURL
	 */
	public String getPlayerParticleURL () {
		// default getter (brpocock, Jan 8, 2010)
		return playerParticleURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private String getPlayerParticuleURL () {
		return playerParticleURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private String getPlayerSoundURL () {
		return playerSoundURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private int getShotOffsetX () {
		return shotOffsetX;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private int getShotOffsetY () {
		return shotOffsetY;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return range in px
	 */
	private double getShotRange () {
		return shotRange;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return delay between shots in seconds
	 */
	private double getShotRate () {
		return shotRate;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private String getTargetParticleURL () {
		return targetParticleURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private String getTargetSoundURL () {
		return targetSoundURL;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private boolean hasColour () {
		return null != colour;
	}

	/**
	 * TODO: document this method (brpocock, Jan 8, 2010)
	 * 
	 * @return WRITEME
	 */
	private boolean isRotatedShot () {
		return isRotatedShot;
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.util.CastsToJSON#set(org.json.JSONObject)
	 */
	public void set (final JSONObject o) {
		throw AppiusClaudiusCaecus.fatalBug ("unimplemented");
	}

	/**
	 * @param ammoUsedPerShot the ammoCost to set
	 */
	public void setAmmoCost (final int ammoUsedPerShot) {
		// default setter (brpocock, Jan 8, 2010)
		ammoCost = ammoUsedPerShot;
	}

	/**
	 * @param newBulletSpeed the bulletSpeed to set
	 */
	public void setBulletSpeed (final double newBulletSpeed) {
		// default setter (brpocock, Jan 8, 2010)
		bulletSpeed = newBulletSpeed;
	}

	/**
	 * @param newBulletURL the bulletURL to set
	 */
	public void setBulletURL (final String newBulletURL) {
		// default setter (brpocock, Jan 8, 2010)
		bulletURL = newBulletURL;
	}

	/**
	 * @param newColour the colour to set
	 */
	public void setColour (final Colour newColour) {
		// default setter (brpocock, Jan 8, 2010)
		colour = newColour;
	}

	/**
	 * @param newInitialAmmo the initialAmmo to set
	 */
	public void setInitialAmmo (final int newInitialAmmo) {
		// default setter (brpocock, Jan 8, 2010)
		initialAmmo = newInitialAmmo;
	}

	/**
	 * @param newMaxAmmo the maxAmmo to set
	 */
	public void setMaxAmmo (final int newMaxAmmo) {
		// default setter (brpocock, Jan 8, 2010)
		maxAmmo = newMaxAmmo;
	}

	/**
	 * @param newMaxShots the maxShots to set
	 */
	public void setMaxShots (final int newMaxShots) {
		// default setter (brpocock, Jan 8, 2010)
		maxShots = newMaxShots;
	}

	/**
	 * @param newPlayerParticleURL the playerParticleURL to set
	 */
	public void setPlayerParticleURL (final String newPlayerParticleURL) {
		// default setter (brpocock, Jan 8, 2010)
		playerParticleURL = newPlayerParticleURL;
	}

	/**
	 * @param newPlayerSoundURL the playerSoundURL to set
	 */
	public void setPlayerSoundURL (final String newPlayerSoundURL) {
		// default setter (brpocock, Jan 8, 2010)
		playerSoundURL = newPlayerSoundURL;
	}

	/**
	 * @param newIsRotatedShot the isRotatedShot to set
	 */
	public void setRotatedShot (final boolean newIsRotatedShot) {
		// default setter (brpocock, Jan 8, 2010)
		isRotatedShot = newIsRotatedShot;
	}

	/**
	 * @param newShotOffetX the shotOffsetX to set
	 */
	public void setShotOffsetX (final int newShotOffetX) {
		// default setter (brpocock, Jan 8, 2010)
		shotOffsetX = newShotOffetX;
	}

	/**
	 * @param newShotOffsetY the shotOffsetY to set
	 */
	public void setShotOffsetY (final int newShotOffsetY) {
		// default setter (brpocock, Jan 8, 2010)
		shotOffsetY = newShotOffsetY;
	}

	/**
	 * @param newShotRange the shotRange to set
	 */
	public void setShotRange (final double newShotRange) {
		// default setter (brpocock, Jan 8, 2010)
		shotRange = newShotRange;
	}

	/**
	 * @param newShotRate the shotRate to set
	 */
	public void setShotRate (final double newShotRate) {
		// default setter (brpocock, Jan 8, 2010)
		shotRate = newShotRate;
	}

	/**
	 * @param newTargetParticleURL the targetParticleURL to set
	 */
	public void setTargetParticleURL (final String newTargetParticleURL) {
		// default setter (brpocock, Jan 8, 2010)
		targetParticleURL = newTargetParticleURL;
	}

	/**
	 * @param newTargetSoundURL the targetSoundURL to set
	 */
	public void setTargetSoundURL (final String newTargetSoundURL) {
		// default setter (brpocock, Jan 8, 2010)
		targetSoundURL = newTargetSoundURL;
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.util.CastsToJSON#toJSON()
	 */
	public JSONObject toJSON () {
		final JSONObject jso = new JSONObject ();
		try {
			jso.put ("maxShots", getMaxShots ());
			jso.put ("rate", getShotRate ());
			jso.put ("speed", getBulletSpeed ());
			jso.put ("range", getShotRange ());
			jso.put ("shotURL", getBulletURL ());
			jso.put ("shotColor", hasColour () ? getColor ().toLong ()
					: null);
			jso.put ("colorize", hasColour ());
			jso.put ("initialAmmo", getInitialAmmo ());
			jso.put ("ammoCost", getAmmoCost ());
			jso.put ("maxAmmo", getMaxAmmo ());
			jso.put ("rotateShot", isRotatedShot ());
			jso.put ("shotOffsetX", getShotOffsetX ());
			jso.put ("shotOffsetY", getShotOffsetY ());
			jso.put ("targetSoundURL", getTargetSoundURL ());
			jso.put ("targetParticleURL", getTargetParticleURL ());
			jso.put ("playerSoundURL", getPlayerSoundURL ());
			jso.put ("playerParticleURL", getPlayerParticuleURL ());
		} catch (final JSONException e) {
			AppiusClaudiusCaecus.reportBug (e);
		}
		return jso;
	}

}
