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.InventoryItem;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Camera.ItemMenuCamera;
import rpgEngine.Logic.Game.PlayerItemUse;


public class ItemMenuPanel extends GenericPanel
{
	private static final long serialVersionUID = 6538729434652140830L;
	private GenericCamera camera;
	private Integer cursorPosition;
	private Integer cursorListPosition;
	private GenericDialog partyDialog;
	
	public static final Integer CURSOR_QUIT = 0;
	public static final Integer CURSOR_LIST = 1;
	public static final Integer CURSOR_ITEM_USE = 2;
	public static final Integer CURSOR_ITEM_DROP = 3;
	
	public static final Integer ITEM_LIST_MIN = 0;
	public static final Integer ITEM_LIST_MAX = 41;

	
	public ItemMenuPanel(Integer id, MainFrame p) {
		super(id, p);
		
		partyDialog = null;
		camera = new ItemMenuCamera(this);
		cursorPosition = CURSOR_QUIT;
		cursorListPosition = ITEM_LIST_MIN;
	}
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		Integer numItems = 0;
		Game game = null;
		
		game = this.getParentMainFrame().getGame();
		numItems = game.getInventory().getItems().size() - 1;
		
		//handle dialog input since its modal
		if(partyDialog != null) {
			partyDialog.processKeyEvent(e);
			
			if(partyDialog.isProcessed() == false) {
				return;
			}
			else {
				if(partyDialog.getResult() != PartyDialog.RESULT_CANCEL) {
					//Use Item
					useItem(partyDialog.getResult());
					
					partyDialog = null;
					return;
				}
				else {
					partyDialog = null;
					return;
				}
			}
		}
		
		//handle normal input otherwise
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_LIST) {
				cursorListPosition = cursorListPosition + 2;
				
				if(cursorListPosition > numItems) {
					cursorListPosition = numItems;
				}
			}
			else if(cursorPosition == CURSOR_ITEM_USE) {
				cursorPosition = CURSOR_ITEM_DROP;
			}
			else if(cursorPosition == CURSOR_ITEM_DROP) {
				cursorPosition = CURSOR_ITEM_USE;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == CURSOR_LIST) {
				if(cursorListPosition == ITEM_LIST_MIN) {
					cursorPosition = CURSOR_QUIT;
				}
				else {
					cursorListPosition = cursorListPosition - 2;
					
					if(cursorListPosition < ITEM_LIST_MIN) {
						cursorListPosition = ITEM_LIST_MIN;
					}
				}
			}
			else if(cursorPosition == CURSOR_ITEM_DROP) {
				cursorPosition = CURSOR_ITEM_USE;
			}
			else if(cursorPosition == CURSOR_ITEM_USE) {
				cursorPosition = CURSOR_ITEM_DROP;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == CURSOR_LIST) {
				if(cursorListPosition == ITEM_LIST_MIN) {
					cursorPosition = CURSOR_QUIT;
				}
				else {
					cursorListPosition = cursorListPosition - 1;
					
					if(cursorListPosition < ITEM_LIST_MIN) {
						cursorListPosition = ITEM_LIST_MIN;
					}
				}
			}		
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_QUIT) {
				if(numItems >= 0) {
					cursorPosition = CURSOR_LIST;
					cursorListPosition = ITEM_LIST_MIN;
				}
			}
			else if(cursorPosition == CURSOR_LIST) {
				cursorListPosition = cursorListPosition + 1;
				
				if(cursorListPosition > numItems) {
					cursorListPosition = numItems;
				}
			}
		}
		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;
			cursorListPosition = ITEM_LIST_MIN;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_LIST) {
			cursorPosition = CURSOR_ITEM_USE;
		}
		else if(cursorPosition == CURSOR_ITEM_USE) {
			showUseItemDialog();
		}
		else if(cursorPosition == CURSOR_ITEM_DROP) {
			dropItem();
		}
	}
	
	
	private void showUseItemDialog() {
		int itemToUse = cursorListPosition;
		InventoryItem invItem = null;
		Game game = null;
		Item item = null;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return;
		}
	
		invItem = game.getInventory().getItems().get(itemToUse);
		item = invItem.getItem();
		
		if((item.getType() == Item.TYPE_HEALING) || (item.getType() == Item.TYPE_MP_HEALING) || (item.getType() == Item.TYPE_REVIVE) || (item.getType() == Item.TYPE_TOME)) {
			partyDialog = new PartyDialog(game);
		}
	}
	
	
	private void useItem(int playerNum) {
		int itemToUse = cursorListPosition;
		InventoryItem invItem = null;
		Game game = null;
		HumanPlayer player = null;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return;
		}
	
		player = game.getParty().get(playerNum);
		invItem = game.getInventory().getItems().get(itemToUse);
		
		PlayerItemUse piu = new PlayerItemUse(game.getInventory(),invItem);			
		piu.useItem(player);
		
		if(game.getInventory().getItems().contains(invItem) == false) {
			cursorListPosition--;
			
			if(cursorListPosition < 0) {
				cursorListPosition = 0;
			}
			
			if(game.getInventory().getItems().size() != 0) {
				cursorPosition = CURSOR_LIST;
			}
			else {
				cursorPosition = CURSOR_QUIT;
			}
		}
				
	}
	
	
	private void dropItem() {
		int itemToDrop = cursorListPosition;
		Game game = null;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return;
		}
		
		PlayerItemUse piu = new PlayerItemUse(game.getInventory(),game.getInventory().getItems().get(itemToDrop));
		piu.dropItem();
		
		cursorListPosition--;
		
		if(cursorListPosition < 0) {
			cursorListPosition = 0;
		}
		
		if(game.getInventory().getItems().size() != 0) {
			cursorPosition = CURSOR_LIST;
		}
		else {
			cursorPosition = CURSOR_QUIT;
		}
	}
	
	
	private void processEscapeKey() {
		if(cursorPosition == CURSOR_QUIT) {
			cursorPosition = CURSOR_QUIT;
			cursorListPosition = ITEM_LIST_MIN;
			
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_LIST) {
			cursorPosition = CURSOR_QUIT;
			cursorListPosition = ITEM_LIST_MIN;
			
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_ITEM_USE) {
			cursorPosition = CURSOR_LIST;
		}
		else if(cursorPosition == CURSOR_ITEM_DROP) {
			cursorPosition = CURSOR_LIST;
		}
	}
	
	@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
     
        BufferedImage img = camera.getImage();
        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
        
        //draw dialog on top of image
        if(partyDialog != null) {
        	partyDialog.drawDialogCenteredAt(g, this.getWidth()/2, this.getHeight()/2);
        }
    }

	public Integer getCursorPosition() {
		return cursorPosition;
	}

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

	public Integer getCursorListPosition() {
		return cursorListPosition;
	}

	public void setCursorListPosition(Integer cursorListPosition) {
		this.cursorListPosition = cursorListPosition;
	}
}
