/**
 * Copyright © 2009-2010, Bruce-Robert Pocock & Res Interactive, LLC.
 * All Rights Reserved. Licensed for perpetual use, modification, and/or
 * distribution by either party.
 * 
 * @author brpocock
 */
package com.tootsville;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

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;

/**
 * WRITEME: The documentation for this type (Store) is incomplete.
 * (brpocock, Aug 27, 2009)
 * 
 * @author brpocock
 */
public class Store extends SQLPeerDatum {

	/**
	 * TODO: document this field (brpocock, Oct 13, 2009)
	 * serialVersionUID (long)
	 */
	private static final long serialVersionUID = -8503595537842839095L;

	/**
	 * WRITEME: document this method (brpocock, Aug 27, 2009)
	 * 
	 * @param storeID WRITEME
	 * @return WRITEME
	 */
	public static Store getByID (final int storeID) {
		final Store self = new Store ();
		self.setID (storeID);
		final Vector <Integer> itemIDs = new Vector <Integer> ();
		Connection con = null;
		PreparedStatement st = null;
		ResultSet set = null;
		try {
			con = AppiusConfig.getDatabaseConnection ();
			st = con
			.prepareStatement ("SELECT itemID FROM storeItems WHERE storeID=? ORDER BY itemID DESC");
			st.setInt (1, storeID);
			set = st.executeQuery ();
			while (set.next ()) {
				itemIDs.add (set.getInt (1));
			}
		} catch (final SQLException e) {
			AppiusClaudiusCaecus.reportBug (e);
		} finally {
			if (null != set) {
				try {
					set.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 */ }
			}
		}
		for (final int i : itemIDs) {
			try {
				self.addItem (StoreItem.getByID (i));
			} catch (final NotFoundException e) {
				AppiusClaudiusCaecus.reportBug (
						"Item is in a store, but doesn't exist", e);
			}
		}
		return self;
	}

	/**
	 * TODO: document this field (brpocock, Nov 19, 2009) id (Store)
	 */
	private int id = -1;

	/**
	 * TODO: document this field (brpocock, Nov 19, 2009) items (Store)
	 */
	private final List <StoreItem> items = new LinkedList <StoreItem> ();

	/**
	 * WRITEME: document this method (brpocock, Aug 27, 2009)
	 * 
	 * @param item WRITEME
	 */
	private void addItem (final StoreItem item) {
		items.add (item);
	}

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

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

	/**
	 * @return the id
	 */
	public int getID () {
		return id;
	}

	/**
	 * WRITEME: document this method (brpocock, Aug 27, 2009)
	 * 
	 * @return WRITEME
	 */
	public ItemCreationTemplate [] getItems () {
		return items.toArray (new StoreItem [items.size ()]);
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#set(java.sql.ResultSet)
	 */
	@Override
	protected void set (final ResultSet rs) throws SQLException {
		items.clear ();
		while (rs.next ()) {
			try {
				items.add (StoreItem.getByID (rs.getInt ("itemID")));
			} catch (final NotFoundException e) {
				AppiusClaudiusCaecus.reportBug (
						"Item is in store, but doesn't exist", e);
			}
		}
	}

	/**
	 * @param id1 the id to set
	 */
	protected void setID (final int id1) {
		id = id1;
	}

	/**
	 * This is an overriding method.
	 * 
	 * @see org.starhope.appius.sql.SQLPeerDatum#toJSON()
	 */
	@Override
	public JSONObject toJSON () {
		final JSONObject storeInside = new JSONObject ();
		final JSONObject itemsJSON = new JSONObject ();
		int i = 0;

		for (final ItemCreationTemplate item : items) {
			try {
				itemsJSON.put (String.valueOf (i++ ), item.toJSON ());
			} catch (final JSONException e) {
				AppiusClaudiusCaecus.reportBug (e);
			}
		}
		try {
			storeInside.put ("items", itemsJSON);
		} catch (final JSONException e) {
			AppiusClaudiusCaecus.reportBug (e);
		}
		return storeInside;
	}

}
