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.Shop;
import rpgEngine.Data.Abstract.Party.Inventory;
import rpgEngine.Resources.FontPool;
import rpgEngine.Resources.ImagePool;
import rpgEngine.UI.ShopPanel;

public class ShopCamera extends GenericCamera {
	private ShopPanel panel;
	
	public ShopCamera(ShopPanel p) {
		super();
		
		panel = p;
	}

	@Override
	public BufferedImage getImage() {
		BufferedImage img = null;
		Graphics2D g = null;
		Shop shop = null;
		Inventory inv = null;
		Integer width = 800;
		Integer height = 600;
		Integer cursorPos = 0;
		Integer invCursorPos = 0;
		
		Integer x = 0;
		Integer y = 0;
		
		cursorPos = panel.getCursorPosition();
		invCursorPos = panel.getInvCursorPosition() - 1;	//minus 1 because it is converting from 1-based to 0-based
		
		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_SHOP_BACKGROUND),0,0,width,height,null);
		
		//Draw Commands
		g.setColor(Color.WHITE);
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,32));
		g.drawString("Buy", 40, 64);
		g.drawString("Sell", 200, 64);
		g.drawString("Leave", 350, 64);
		
		//Draw Money
		g.drawString("G: " + panel.getParentMainFrame().getGame().getGold(), 560, 64);
		
		//Draw Inventory
		g.setColor(Color.WHITE);
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,24));
		if(cursorPos == ShopPanel.CURSOR_BUY_INVENTORY) {
			shop = panel.getParentMainFrame().getGame().getActiveShop();
			
			for(Integer i = 0; i < shop.getItems().size(); i++) {
				y = (((int)Math.floor(((double)i) / 2.0)) * 40) + 136;
				
				if(i % 2 == 0) {
					x = 80;
				}
				else {
					x = 400;
				}
				
				g.drawString(shop.getItems().get(i).getName(), x, y);
			}
			
			//Draw Price
			g.drawString("Cost: " + shop.getItems().get(invCursorPos).getGoldCost(), 20, 575);
		}
		else if(cursorPos == ShopPanel.CURSOR_SELL_INVENTORY) {
			inv = panel.getParentMainFrame().getGame().getInventory();
			
			for(Integer i = 0; i < inv.getItems().size(); i++) {
				y = (((int)Math.floor(((double)i) / 2.0)) * 40) + 136;
				
				if(i % 2 == 0) {
					x = 80;
				}
				else {
					x = 400;
				}
				
				g.drawString(inv.getItems().get(i).getItem().getName() + " (" + inv.getItems().get(i).getQuantity() + ")", x, y);
			}
			
			//Draw Price
			g.drawString("Sell: " + inv.getItems().get(invCursorPos).getItem().getGoldCost() / 2, 20, 575);
		}
		
		//Draw Cursor
		g.setColor(Color.WHITE);
		if(cursorPos == ShopPanel.CURSOR_BUY) {
			g.fillRect(16, 44, 16, 16);
		}
		else if (cursorPos == ShopPanel.CURSOR_SELL) {
			g.fillRect(176, 44, 16, 16);
		}
		else if (cursorPos == ShopPanel.CURSOR_CANCEL) {
			g.fillRect(326, 44, 16, 16);
		}
		else if ((cursorPos == ShopPanel.CURSOR_BUY_INVENTORY) || (cursorPos == ShopPanel.CURSOR_SELL_INVENTORY)) {
			y = (((int)Math.floor(((double)invCursorPos) / 2.0)) * 40) + 136;
			
			if(invCursorPos % 2 == 0) {
				x = 80;
			}
			else {
				x = 400;
			}
			
			g.fillRect(x-24, y-20, 16, 16);
		}
		
		//Draw Message
		if(panel.getMessage().equals("") == false) {
			g.setColor(Color.WHITE);
			g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,28));
			
			g.drawString(panel.getMessage(), 255, 575);
		}
		else {
			if (cursorPos == ShopPanel.CURSOR_SELL_INVENTORY) {
				inv = panel.getParentMainFrame().getGame().getInventory();
				g.setColor(Color.WHITE);
				g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,28));
				
				g.drawString(inv.getItems().get(invCursorPos).getItem().getDescription(),255,575);
			}
			else if(cursorPos == ShopPanel.CURSOR_BUY_INVENTORY) {
				shop = panel.getParentMainFrame().getGame().getActiveShop();
				g.setColor(Color.WHITE);
				g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,28));
				
				g.drawString(shop.getItems().get(invCursorPos).getDescription(),255,575);
			}
		}
		
		return img;
	}
	
	
	
}
