/**
 * <p>
 * Copyright © 2009-2010, Bruce-Robert Pocock
 * </p>
 * <p>
 * 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.
 * </p>
 * <p>
 * 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.
 * </p>
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * </p>
 * 
 * @author brpocock
 */
package com.tootsville;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONException;
import org.json.JSONObject;
import org.starhope.appius.except.NotFoundException;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.sql.SQLPeerDatum;
import org.starhope.appius.types.ItemCreationTemplate;
import org.starhope.appius.util.AppiusConfig;

/**
 * This represents an abstract/template item found in a store
 * 
 * @author brpocock
 */
public class StoreItem extends SQLPeerDatum implements
ItemCreationTemplate {

	/**
	 * Java serialization UID
	 */
	private static final long	serialVersionUID	= 1330548723192564366L;

	/**
	 * Instantiate a StoreItem by ID
	 * 
	 * @param id the item id
	 * @return the item
	 * @throws NotFoundException if the item can't be found
	 */
	public static StoreItem getByID (final int id)
	throws NotFoundException {
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		final StoreItem self = new StoreItem ();
		try {
			con = AppiusConfig.getDatabaseConnection ();
			st = con
			.prepareStatement ("SELECT ID AS id, name AS title, description, value AS price FROM items WHERE ID=?");
			st.setInt (1, id);
			rs = st.executeQuery ();
			rs.next ();
			self.set (rs);
		} catch (final SQLException e) {
			throw new NotFoundException (String.valueOf (id));
		} finally {
			if (null != rs) {
				try {
					rs.close ();
				} catch (final SQLException e) { /* No Op */
				}
			}
			if (null != st) {
				try {
					st.close ();
				} catch (final SQLException e) { /* No Op */
				}
			}
			if (null != con) {
				try {
					con.close ();
				} catch (final SQLException e) { /* No Op */
				}
			}

		}
		return self;
	}

	/**
	 * user-visible description
	 */
	private String		description;

	/**
	 * unique database ID
	 */
	private int			itemID;

	/**
	 * price in peanuts
	 */
	private BigDecimal	price	= new BigDecimal (0);

	/**
	 * user-visible title
	 */
	private String		title;

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#flush()
	 */
	@Override
	public void flush () {
		AppiusClaudiusCaecus
		.reportBug ("Trying to flush() a StoreItem: I don't do that");
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#getCacheUniqueID()
	 */
	@Override
	protected String getCacheUniqueID () {
		return String.valueOf (itemID);
	}

	/**
	 * @return WRITEME
	 * @see ItemCreationTemplate#getDescription()
	 */
	public String getDescription () {
		return description;
	}

	/**
	 * @return WRITEME
	 * @see ItemCreationTemplate#getItemID()
	 */
	public int getItemID () {
		return itemID;
	}

	/**
	 * @return WRITEME
	 * @see ItemCreationTemplate#getPrice()
	 */
	public BigDecimal getPrice () {
		return price;
	}

	/**
	 * @return WRITEME
	 * @see ItemCreationTemplate#getTitle()
	 */
	public String getTitle () {
		return title;
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#set(java.sql.ResultSet)
	 */
	@Override
	protected void set (final ResultSet rs) throws SQLException {
		itemID = rs.getInt ("id");
		price = new BigDecimal (rs.getInt ("price"));
		title = rs.getString ("title");
		description = rs.getString ("description");
	}

	/**
	 * @param itemID1 WRITEME
	 * @see ItemCreationTemplate#setItemID(int)
	 */
	public void setItemID (final int itemID1) {
		// default setter (brpocock, Aug 27, 2009)
		itemID = itemID1;
	}

	/**
	 * @param price1 WRITEME
	 * @see ItemCreationTemplate#setPrice(java.math.BigDecimal)
	 */
	public void setPrice (final BigDecimal price1) {
		// default setter (brpocock, Aug 27, 2009)
		price = price1;
	}

	/**
	 * @param title1 WRITEME
	 * @see ItemCreationTemplate#setTitle(java.lang.String)
	 */
	public void setTitle (final String title1) {
		// default setter (brpocock, Aug 27, 2009)
		title = title1;
	}

	/**
	 * @see ItemCreationTemplate#toJSON()
	 */
	@Override
	public JSONObject toJSON () {
		final JSONObject self = new JSONObject ();
		try {
			self.put ("id", itemID);
			self.put ("title", title);
			self.put ("price", price.toPlainString ());
			self.put ("desc", description);
		} catch (final JSONException e) {
			AppiusClaudiusCaecus.reportBug (e);
		}
		return self;
	}
}
