/**
 * THE StoreInventory.java WRITEME...
 */
package com.tootsville.tootsbook.client.pages;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.tootsville.tootsbook.client.BookServiceAsync;
import com.tootsville.tootsbook.client.TootsBook;
import com.tootsville.tootsbook.client.panel.StoreItemPanel;
import com.tootsville.tootsbook.client.util.StoreInventory;
import com.tootsville.tootsbook.client.util.StoreItem;

/**
 * WRITEME: Document this type. twheys@gmail.com Feb 23, 2010
 * 
 * @author <a href="mailto:twheys@gmail.com@resinteractive.com">Tim
 *         Heys</a>
 */
public class StoreInventoryPanel extends FlowPanel {

	/**
	 *
	 */
	private StoreInventory inventory;

	/**
	 *
	 */
	private final int storeID;

	/**
	 * <pre>
	 * twheys@gmail.com Feb 24, 2010
	 * </pre>
	 * 
	 * A StoreInventoryPanel WRITEME...
	 * 
	 * @param newStoreID WRITEME twheys@gmail.com
	 */
	public StoreInventoryPanel (final int newStoreID) {
		storeID = newStoreID;
		add (new Label ("Loading Items"));
		setStyleName ("store-inventory");
	}

	/**
	 * <pre>
	 * twheys@gmail.com Feb 24, 2010
	 * </pre>
	 * 
	 * TO displayStoreInventory WRITEME...
	 */
	protected void displayStoreInventory () {
		if (null == inventory)
			throw new IllegalStateException ("Inventory is null!!!");
		clear ();
		for (final StoreItem item : inventory.getItems ()) {
			add (new StoreItemPanel (item));
		}
	}

	/**
	 * @return the inventory
	 */
	public StoreInventory getInventory () {
		return inventory;
	}

	/**
	 * <pre>
	 * twheys@gmail.com Feb 24, 2010
	 * </pre>
	 * 
	 * TO loadItems WRITEME...
	 */
	public void loadItems () {
		final BookServiceAsync service = TootsBook.initService ();
		final AsyncCallback <StoreInventory> callback = new AsyncCallback <StoreInventory> () {

			@Override
			public void onFailure (final Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess (final StoreInventory result) {
				setInventory (result);
				displayStoreInventory ();

			}
		};
		service.getInventoryForStore (storeID, callback);
	}

	/**
	 * <pre>
	 * twheys@gmail.com Feb 24, 2010
	 * </pre>
	 * 
	 * TO setInventory WRITEME...
	 * 
	 * @param items WRITEME
	 */
	public void setInventory (final StoreInventory items) {
		inventory = items;
	}

}
