package rpgEngine.Logic.Camera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Item;
import rpgEngine.Data.Abstract.Party.Inventory;
import rpgEngine.Data.Abstract.Party.InventoryItem;
import rpgEngine.Resources.FontPool;
import rpgEngine.Resources.ImagePool;
import rpgEngine.UI.ItemMenuPanel;


public class ItemMenuCamera extends GenericCamera{
	private ItemMenuPanel panel;
	
	public ItemMenuCamera(ItemMenuPanel p) {
		super();
		
		panel = p;
	}

	@Override
	public BufferedImage getImage() {
		BufferedImage img = null;
		Graphics2D g = null;
		Integer width = 800;
		Integer height = 600;
		Integer cursorPos = 0;
		Integer cursorListPos = ItemMenuPanel.ITEM_LIST_MIN;
		Game game = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Double valD = 0.0;
		Integer valI = 0;
		Integer x = 0;
		Integer y = 0;
		
		//Get Cursor Position
		cursorPos = panel.getCursorPosition();
		cursorListPos = panel.getCursorListPosition();
		
		img = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
		
		g = img.createGraphics();
		
		//Draw Background
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);		
		g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_MENU_ITEM_BACKGROUND),0,0,width,height,null);
				
		//Draw Menu Choices
		g.setColor(Color.WHITE);
		//g.setFont(new Font("Sans Serif",Font.BOLD,32));
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,32));
		g.drawString("Back", 40, 64);
		
		//Draw Item Actions
		if((cursorPos == ItemMenuPanel.CURSOR_ITEM_USE) || (cursorPos == ItemMenuPanel.CURSOR_ITEM_DROP)) {
			g.drawString("Use", 40, 170);
			g.drawString("Drop", 40, 220);
		}
				
		if(cursorPos == ItemMenuPanel.CURSOR_ITEM_USE) {
			g.fillRect(16, 150, 16, 16);
		}
		else if(cursorPos == ItemMenuPanel.CURSOR_ITEM_DROP) {
			g.fillRect(16, 200, 16, 16);
		}
		else if(cursorPos == ItemMenuPanel.CURSOR_QUIT) {
			g.fillRect(16, 44, 16, 16);
		}
				
		game = panel.getParentMainFrame().getGame();
		
		if(game == null) {
			return img;
		}
		
		inv = game.getInventory();
		
		//Show Item Description
		if((cursorPos == ItemMenuPanel.CURSOR_LIST) || (cursorPos == ItemMenuPanel.CURSOR_ITEM_DROP) || (cursorPos == ItemMenuPanel.CURSOR_ITEM_USE)) {
			invItem = inv.getItems().get(cursorListPos);
			
			if(invItem != null) {
				g.setColor(Color.WHITE);
				//g.setFont(new Font("Sans Serif", Font.PLAIN, 20));
				g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.PLAIN,20));
				
				g.drawString(invItem.getItem().getDescription(), 280, 574);
			}
		}
		
		
		//g.setFont(new Font("Sans Serif", Font.BOLD, 16));
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,16));
		for(Integer i = 0; i < inv.getItems().size(); i++) {
			invItem = inv.getItems().get(i);
			valD = Double.valueOf(i);
			
			if(i % 2 == 0) {
				x = 310;
			}
			else {
				x = 560;
			}
			
			valI = (int) Math.floor(valD / 2.0);
			y = (valI * 24) + 30;
			
			if((invItem.getItem().getType() == Item.TYPE_HEALING) || (invItem.getItem().getType() == Item.TYPE_MP_HEALING) || (invItem.getItem().getType() == Item.TYPE_REVIVE) || (invItem.getItem().getType() == Item.TYPE_TOME)) {
				g.setColor(Color.WHITE);
			}
			else {
				g.setColor(Color.GRAY);
			}
			
			g.drawString(invItem.getItem().getName() + " (" + invItem.getQuantity() +  ")", x, y);
			
			if(cursorPos == ItemMenuPanel.CURSOR_LIST) {
				if(i == cursorListPos) {
					g.setColor(Color.WHITE);
					g.fillRect(x-18, y-12, 12, 12);
				}
			}
		}
		
		
		return img;
	}
	
	
	
}
