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


public class EquipmentMenuCamera extends GenericCamera {
	private EquipmentMenuPanel panel;
	
	public EquipmentMenuCamera(EquipmentMenuPanel p) {
		super();
		
		panel = p;
	}

	@Override
	public BufferedImage getImage() {
		Integer cursorPos = 0;
		Integer cursorPartyPos = 0;
		Integer cursorEquipPos = 0;
		Integer cursorInvPos = 0;
		HumanPlayer player = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Item item = null;
		Item equipItem = null;
		Item selItem = null;
		BufferedImage img = null;
		Graphics2D g = null;
		Integer width = 800;
		Integer height = 600;
		Game game = null;
		Integer count = 0;
		Integer x = 0;
		Integer y = 0;
		Boolean toShow = false;
		String str = "";
		
		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_EQUIPMENT_BACKGROUND),0,0,width,height,null);
		
		//Get Cursor Position
		cursorPos = panel.getCursorPosition();
		cursorPartyPos = panel.getCursorPartyPosition();
		cursorEquipPos = panel.getCursorEquipmentPosition();
		cursorInvPos = panel.getCursorInventoryPosition();
		
		game = panel.getParentMainFrame().getGame();
		
		if(game == null) {
			return img;
		}
		
		//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);
		
		for(int i = 0; i < game.getParty().size(); i++) {
			g.drawString(game.getParty().get(i).getName(), 40, 130 + (60*i));
		}
		
		//Draw Equipment
		if((cursorPos == EquipmentMenuPanel.CURSOR_PARTY) || (cursorPos == EquipmentMenuPanel.CURSOR_EQUIPMENT) || (cursorPos == EquipmentMenuPanel.CURSOR_INVENTORY)) {
			player = game.getParty().get(cursorPartyPos);
			
			g.setColor(Color.WHITE);
			//g.setFont(new Font("Sans Serif",Font.BOLD,24));
			g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,24));
			
			item = player.getWeapon();
			if(item != null) {
				g.drawString("Weapon: " + item.getName(),310,45);
			}
			else {
				g.drawString("Weapon: ",310,45);
			}
			
			item = player.getHelmet();
			if(item != null) {
				g.drawString("Helmet: " + item.getName(),310,90);
			}
			else {
				g.drawString("Helmet: ",310,90);
			}
			
			item = player.getArmor();
			if(item != null) {
				g.drawString("Armor: " + item.getName(),310,135);
			}
			else {
				g.drawString("Armor: ",310,135);
			}
			
			item = player.getAccessory();
			if(item != null) {
				g.drawString("Special: " + item.getName(),310,180);
			}
			else {
				g.drawString("Special: ",310,180);
			}
		}
		
		
		//Draw Inventory For Selected Position
		if ((cursorPos == EquipmentMenuPanel.CURSOR_EQUIPMENT) || (cursorPos == EquipmentMenuPanel.CURSOR_INVENTORY)) {
			if(cursorPartyPos != -1) {
				if(cursorEquipPos != -1) {
					inv = game.getInventory();
					
					x = 300;
					y = 320;
					//g.setFont(new Font("Sans Serif",Font.PLAIN,20));
					g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.PLAIN,20));
					count = 0;
					
					for(int i = 0; i < inv.getItems().size(); i++) {
						invItem = inv.getItems().get(i);
						item = invItem.getItem();
						toShow = false;
						
						if((cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_WEAPON) && (item.getType() == Item.TYPE_WEAPON)) {
							toShow = true;
						}
						else if((cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ARMOR) && (item.getType() == Item.TYPE_ARMOR)) {
							toShow = true;
						}
						else if((cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ACCESSORY) && (item.getType() == Item.TYPE_ACCESSORY)) {
							toShow = true;
						}
						else if((cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_HELMET) && (item.getType() == Item.TYPE_HELMET)) {
							toShow = true;
						}
						
						if(toShow) {
							if (count % 2 == 0) {
								g.drawString(item.getName() + " (" + invItem.getQuantity() + ")", x, y);
							}
							else {
								g.drawString(item.getName() + " (" + invItem.getQuantity() + ")", x+260, y);
								y = y + 25;
							}
							
							//draw inventory cursor
							if(count == cursorInvPos) {
								if (count % 2 == 0) {
									g.fillRect(x-20, y-16, 16, 16);
								}
								else {
									g.fillRect(x+260-20, y-25-16, 16, 16);
								}
								
								selItem = item;
							}
							
							count++;
						}
					}
				}
			}
		}
		
		
		//Draw Change For Selected Item
		if(cursorPos == EquipmentMenuPanel.CURSOR_INVENTORY) {
			if(cursorPartyPos != -1) {
				if(cursorEquipPos != -1) {
					if(cursorInvPos != -1) {
						if(selItem != null) {
							str = "";
							player = game.getParty().get(cursorPartyPos);
							
							if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_WEAPON) {
								equipItem = player.getWeapon();
							}
							else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_HELMET) {
								equipItem = player.getHelmet();
							}
							else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ARMOR) {
								equipItem = player.getArmor();
							}
							else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ACCESSORY) {
								equipItem = player.getAccessory();
							}
							
							if(equipItem == null) {
								equipItem = new Item();
							}
							
							//compare hp
							if(selItem.getStatChanges().getMaxHP() - equipItem.getStatChanges().getMaxHP() != 0) {
								str = str + "HP: " + (selItem.getStatChanges().getMaxHP() - equipItem.getStatChanges().getMaxHP()) + " "; 
							}
							
							//compare mp
							if(selItem.getStatChanges().getMaxMP() - equipItem.getStatChanges().getMaxMP() != 0) {
								str = str + "MP: " + (selItem.getStatChanges().getMaxMP() - equipItem.getStatChanges().getMaxMP()) + " "; 
							}
							
							//compare atk
							if(selItem.getStatChanges().getAttack() - equipItem.getStatChanges().getAttack() != 0) {
								str = str + "Atk: " + (selItem.getStatChanges().getAttack() - equipItem.getStatChanges().getAttack()) + " "; 
							}
							
							//compare def
							if(selItem.getStatChanges().getDefense() - equipItem.getStatChanges().getDefense() != 0) {
								str = str + "Def: " + (selItem.getStatChanges().getDefense() - equipItem.getStatChanges().getDefense()) + " "; 
							}
							
							//compare mag
							if(selItem.getStatChanges().getSkill() - equipItem.getStatChanges().getSkill() != 0) {
								str = str + "Skl: " + (selItem.getStatChanges().getSkill() - equipItem.getStatChanges().getSkill()) + " "; 
							}
							
							//compare bar
							if(selItem.getStatChanges().getBarrier() - equipItem.getStatChanges().getBarrier() != 0) {
								str = str + "Bar: " + (selItem.getStatChanges().getBarrier() - equipItem.getStatChanges().getBarrier()) + " "; 
							}
							
							g.drawString(str, 280, 250);
						}
					}
				}
			}
		}
		
		
		//Draw Cursor
		g.setColor(Color.WHITE);
		
		if(cursorPos == EquipmentMenuPanel.CURSOR_QUIT) {
			g.fillRect(16, 44, 16, 16);
		}
		else if(cursorPos == EquipmentMenuPanel.CURSOR_PARTY) {
			g.fillRect(16, 110+(60 * cursorPartyPos), 16, 16);
		}
		else if(cursorPos == EquipmentMenuPanel.CURSOR_EQUIPMENT) {
			if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_WEAPON) {
				g.fillRect(285, 25, 16, 16);
			}
			else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_HELMET) {
				g.fillRect(285, 70, 16, 16);
			}
			else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ARMOR) {
				g.fillRect(285, 115, 16, 16);
			}
			else if(cursorEquipPos == EquipmentMenuPanel.EQUIPMENT_CURSOR_ACCESSORY) {
				g.fillRect(285, 160, 16, 16);
			}
		}
		
		
		return img;
	}
	
}
