package rpgEngine.UI;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
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.Logic.Camera.EquipmentMenuCamera;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Game.PlayerItemEquip;


public class EquipmentMenuPanel extends GenericPanel {
	private static final long serialVersionUID = 1572223976607715452L;
	private GenericCamera camera;
	private Integer cursorPosition;
	private Integer cursorPartyPosition;
	private Integer cursorEquipmentPosition;
	private Integer cursorInventoryPosition;
	
	public static final Integer CURSOR_QUIT = 0;
	public static final Integer CURSOR_PARTY = 1;
	public static final Integer CURSOR_EQUIPMENT = 2;
	public static final Integer CURSOR_INVENTORY = 3;
	
	public static final Integer EQUIPMENT_CURSOR_WEAPON = 0;
	public static final Integer EQUIPMENT_CURSOR_HELMET = 1;
	public static final Integer EQUIPMENT_CURSOR_ARMOR = 2;
	public static final Integer EQUIPMENT_CURSOR_ACCESSORY = 3;
	
	
	public EquipmentMenuPanel(Integer id, MainFrame p) {
		super(id, p);

		camera = new EquipmentMenuCamera(this);
		cursorPosition = CURSOR_QUIT;
		cursorPartyPosition = -1;
		cursorEquipmentPosition = -1;
		cursorInventoryPosition = -1;
	}


	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		Game game = null;
		Integer maxInvPos = 0;
		
		game = this.getParentMainFrame().getGame();
		
		if (cursorPosition == CURSOR_INVENTORY) {
			maxInvPos = getNumberItemsAvailableToEquip() - 1;
		}
		
		
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_QUIT) {
				cursorPosition = CURSOR_PARTY;
				cursorPartyPosition = 0;
			}
			else if(cursorPosition == CURSOR_PARTY) {
				if(cursorPartyPosition == (game.getParty().size()-1)) {
					cursorPosition = CURSOR_QUIT;
					cursorPartyPosition = -1;
				}
				else {
					cursorPartyPosition++;
				}
			}
			else if(cursorPosition == CURSOR_EQUIPMENT) {
				if(cursorEquipmentPosition == EQUIPMENT_CURSOR_WEAPON) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_HELMET;
				}
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_HELMET) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_ARMOR;
				} 
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_ARMOR) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_ACCESSORY;				
				}
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_ACCESSORY) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
				}
			}
			else if (cursorPosition == CURSOR_INVENTORY) {				
				cursorInventoryPosition = cursorInventoryPosition + 2;
				
				if(cursorInventoryPosition > maxInvPos) {
					cursorInventoryPosition = maxInvPos;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == CURSOR_QUIT) {
				cursorPosition = CURSOR_PARTY;
				cursorPartyPosition = game.getParty().size() - 1;
			}
			else if(cursorPosition == CURSOR_PARTY) {
				if(cursorPartyPosition == 0) {
					cursorPartyPosition = -1;
					cursorPosition = CURSOR_QUIT;
				}
				else {
					cursorPartyPosition--;
				}
			}
			else if(cursorPosition == CURSOR_EQUIPMENT) {
				if(cursorEquipmentPosition == EQUIPMENT_CURSOR_WEAPON) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_ACCESSORY;
				}
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_HELMET) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
				} 
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_ARMOR) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_HELMET;				
				}
				else if(cursorEquipmentPosition == EQUIPMENT_CURSOR_ACCESSORY) {
					cursorEquipmentPosition = EQUIPMENT_CURSOR_ARMOR;
				}
			}
			else if (cursorPosition == CURSOR_INVENTORY) {				
				cursorInventoryPosition = cursorInventoryPosition - 2;
				
				if(cursorInventoryPosition < 0) {
					cursorInventoryPosition = 0;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == CURSOR_EQUIPMENT) {
				cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
				cursorPosition = CURSOR_PARTY;
			}
			else if (cursorPosition == CURSOR_INVENTORY) {				
				cursorInventoryPosition = cursorInventoryPosition - 1;
				
				if(cursorInventoryPosition < 0) {
					cursorInventoryPosition = 0;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_PARTY) {
				cursorPosition = CURSOR_EQUIPMENT;
				cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
			}
			else if (cursorPosition == CURSOR_INVENTORY) {				
				cursorInventoryPosition = cursorInventoryPosition + 1;
				
				if(cursorInventoryPosition > maxInvPos) {
					cursorInventoryPosition = maxInvPos;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_ENTER) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			processEscapeKey();
		}
	}

	
	private void processActionKey() {
		
		if(cursorPosition == CURSOR_QUIT) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorEquipmentPosition = -1;
			cursorInventoryPosition = -1;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_PARTY) {
			cursorPosition = CURSOR_EQUIPMENT;
			cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
		}
		else if(cursorPosition == CURSOR_EQUIPMENT) {
			Boolean itemsAvailable = false;
			
			itemsAvailable = areItemsAvailableToEquip();
			
			if(itemsAvailable) {
				cursorPosition = CURSOR_INVENTORY;
				cursorInventoryPosition = 0;
			}
		}
		else if (cursorPosition == CURSOR_INVENTORY) {
			Game game = this.getParentMainFrame().getGame();
			HumanPlayer player = null;
			Item item = null;
			PlayerItemEquip pie = null;
			
			player = game.getParty().get(cursorPartyPosition);
			item = getSelectedItem();
			
			pie = new PlayerItemEquip(game,player,item);
			pie.equipItem();
			
			cursorPosition = CURSOR_EQUIPMENT;
			cursorInventoryPosition = -1;
		}
		
	}
	
	
	private void processEscapeKey() {
		
		if(cursorPosition == CURSOR_QUIT) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorEquipmentPosition = -1;
			cursorInventoryPosition = -1;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_PARTY) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorEquipmentPosition = -1;
			cursorInventoryPosition = -1;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_EQUIPMENT) {
			cursorEquipmentPosition = EQUIPMENT_CURSOR_WEAPON;
			cursorPosition = CURSOR_PARTY;
		}
		else if(cursorPosition == CURSOR_INVENTORY) {
			cursorPosition = CURSOR_EQUIPMENT;
			cursorInventoryPosition = -1;
		}
		
	}
	
	
	private Item getSelectedItem() {
		Integer count = 0;
		Game game = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Item item = null;
		Boolean inGroup = false;
		
		game = this.getParentMainFrame().getGame();
		inv = game.getInventory();
		
		for(int i = 0; i < inv.getItems().size(); i++) {
			invItem = inv.getItems().get(i);
			item = invItem.getItem();
			inGroup = false;
			
			if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_WEAPON) && (item.getType() == Item.TYPE_WEAPON)) {
				inGroup = true;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_HELMET) && (item.getType() == Item.TYPE_HELMET)) {
				inGroup = true;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ARMOR) && (item.getType() == Item.TYPE_ARMOR)) {
				inGroup = true;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ACCESSORY) && (item.getType() == Item.TYPE_ACCESSORY)) {
				inGroup = true;
			}
			
			if(inGroup) {
				if(cursorInventoryPosition == count) {
					return item;
				}
				
				count++;
			}
		}
		
		
		return null;
	}
	
	
	private Integer getNumberItemsAvailableToEquip() {
		Integer count = 0;
		Game game = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Item item = null;
		
		game = this.getParentMainFrame().getGame();
		inv = game.getInventory();
		
		for(int i = 0; i < inv.getItems().size(); i++) {
			invItem = inv.getItems().get(i);
			item = invItem.getItem();
			
			if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_WEAPON) && (item.getType() == Item.TYPE_WEAPON)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_HELMET) && (item.getType() == Item.TYPE_HELMET)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ARMOR) && (item.getType() == Item.TYPE_ARMOR)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ACCESSORY) && (item.getType() == Item.TYPE_ACCESSORY)) {
				count++;
			}
			
		}
		
		return count;
	}
	
	
	private Boolean areItemsAvailableToEquip() {
		Boolean itemsAvailable = false;
		Integer count = 0;
		Game game = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Item item = null;
		
		game = this.getParentMainFrame().getGame();
		inv = game.getInventory();
		
		for(int i = 0; i < inv.getItems().size(); i++) {
			invItem = inv.getItems().get(i);
			item = invItem.getItem();
			
			if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_WEAPON) && (item.getType() == Item.TYPE_WEAPON)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_HELMET) && (item.getType() == Item.TYPE_HELMET)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ARMOR) && (item.getType() == Item.TYPE_ARMOR)) {
				count++;
			}
			else if ((cursorEquipmentPosition == EQUIPMENT_CURSOR_ACCESSORY) && (item.getType() == Item.TYPE_ACCESSORY)) {
				count++;
			}
			
		}
		
		if(count > 0) {
			itemsAvailable = true;
		}
		
		return itemsAvailable;
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        BufferedImage img = camera.getImage();
        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
	}


	public Integer getCursorPosition() {
		return cursorPosition;
	}


	public void setCursorPosition(Integer cursorPosition) {
		this.cursorPosition = cursorPosition;
	}


	public Integer getCursorPartyPosition() {
		return cursorPartyPosition;
	}


	public void setCursorPartyPosition(Integer cursorPartyPosition) {
		this.cursorPartyPosition = cursorPartyPosition;
	}


	public Integer getCursorEquipmentPosition() {
		return cursorEquipmentPosition;
	}


	public void setCursorEquipmentPosition(Integer cursorEquipmentPosition) {
		this.cursorEquipmentPosition = cursorEquipmentPosition;
	}


	public Integer getCursorInventoryPosition() {
		return cursorInventoryPosition;
	}


	public void setCursorInventoryPosition(Integer cursorInventoryPosition) {
		this.cursorInventoryPosition = cursorInventoryPosition;
	}
	
	
}
