package de.rico.adventure.inventory;

import javax.media.opengl.GL;

import de.rico.adventure.entity.item.Item;
import de.rico.engine.camera.Camera;
import de.rico.engine.input.InputManager;
import de.rico.engine.material.Color4f;

/**
 * This class represents an inventory that uses a bar at the top of the screen
 * to visualize the items in the inventory.
 * @author Frank Bruns
 */
public class BarInventory extends AbstractInventory
{
	private int space = 0;

	private Color4f hoverColor = new Color4f(1f,1f,1f,1f);

	/**
	 * Creates a new inventory system that uses a bar at the top of the screen
	 * to visualize the items in the inventory.
	 * @param height height of the inventory bar in pixels
	 * @param space between two item pictures
	 * @param input input manager object
	 */
	public BarInventory(int height, int space, InputManager input)
	{
		super(input);
		this.setHeight(height);
		this.space = space;
	}

	/**
	 * Creates a new inventory system that uses a bar at the top of the screen
	 * to visualize the items in the inventory. If the mouse hovers over an item
	 * the image color will be set to the specified hover color
	 * @param height height of the inventory bar in pixels
	 * @param space between two item pictures
	 * @param hoverColor item imgages hover color
	 * @param input input manager object
	 */
	public BarInventory(int height, int space, Color4f hoverColor,
			InputManager input)
	{
		this(height, space, input);
		this.hoverColor = hoverColor;
	}

	/*
	 * (non-Javadoc)
	 * @see de.rico.adventure.inventory.AbstractInventory#isMouseOverItem(de.rico.adventure.entity.item.Item)
	 */
	@Override
	public boolean isMouseOverItem(Item item)
	{
		int xOff = this.getItemXOffset(item);

		// get mouse coordinates
		int mouseX = this.inputManager.getMouseX();
		int mouseY = this.inputManager.getMouseY();

		if (mouseX >= xOff
				&& mouseX <= xOff + item.getInventoryImage().getWidth()
				&& mouseY >= 0
				&& mouseY <= 0 + item.getInventoryImage().getHeight())
		{
			return true;
		}
		else return false;
	}

	/**
	 * Helper method to get the x offset of the specified item in the inventory.
	 * @param item inventory item to get the x offset for
	 * @return x offset of the specified item
	 */
	private int getItemXOffset(Item item)
	{
		// find item position in linked list
		int index = this.getItems().indexOf(item);

		// add the widths + spaces of the all the previous items
		// to find the xoffset of the specified item
		int xOff = 0;
		for (int i = 0; i < index; i++)
		{
			xOff += (this.getItems().get(i).getInventoryImage().getWidth() + this.space);
		}

		return xOff;
	}

	/*
	 * (non-Javadoc)
	 * @see de.rico.adventure.inventory.AbstractInventory#draw(javax.media.opengl.GL)
	 */
	@Override
	public void draw(GL gl)
	{
		// don't draw anything if inventory isn't visible
		if (this.isVisible() == false) return;

		this.rectRenderer.drawRectangle(0, 0, this.getWidth(),
				this.getHeight(), this.getBGColor());

		for (int i = 0; i < this.getItems().size(); i++)
		{
			int xOff = 0;

			if (i > 0)
			{
				// get x-offset of the item
				xOff = this.getItemXOffset(this.getItems().get(i));
			}

			// just draw plain image
			if (this.isMouseOverItem(this.getItems().get(i)) == false)
			{
				this.imgRenderer.drawImage(xOff, 0, this.getItems().get(i)
						.getInventoryImage());
			}
			// draw image with hover color and description
			else
			{
				this.imgRenderer.drawImage(xOff, 0, this.getItems().get(i)
						.getInventoryImage(), this.hoverColor);

				this.textRenderer.drawString(this.getItems().get(i).getScreenName(),
						this.getItemXOffset(this.getItems().get(i)), this
								.getItems().get(i).getInventoryImage()
								.getHeight());
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * @see de.rico.adventure.inventory.AbstractInventory#update(long)
	 */
	@Override
	public void update(long elapsedTime)
	{
		super.update(elapsedTime);

		// update inventory with according to teh window size
		this.setWidth(Camera.getInstance().getViewPort().getWidth());

		if (this.isMouseInInventory()) this.setVisible(true);
		else this.setVisible(false);
	}

}
