package rpgEngine.UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Resources.ImagePool;

public class PartyDialog extends GenericDialog {
	public static final Integer RESULT_CANCEL = -1;
	public static final Integer RESULT_PARTY_MEMBER_NUMBER_1 = 0;
	public static final Integer RESULT_PARTY_MEMBER_NUMBER_2 = 1;
	public static final Integer RESULT_PARTY_MEMBER_NUMBER_3 = 2;
	public static final Integer RESULT_PARTY_MEMBER_NUMBER_4 = 3;

	private Boolean isDialogProcessed;
	private Integer cursorPosition;
	private Game game;
	
	public PartyDialog(Game g) {
		game = g;
		isDialogProcessed = false;
		cursorPosition = PartyDialog.RESULT_PARTY_MEMBER_NUMBER_1;
	}
	
	
	@Override
	public void drawDialogCenteredAt(Graphics g, int x, int y) {
		int x1 = x - 132;
		int y1 = y - 132;
		HumanPlayer player = null;
		
		//Draw Background
		g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_DIALOG_LONG_BACKGROUND),x1,y1,264,264,null);
		
		//Draw Text
		g.setColor(Color.WHITE);
		g.setFont(new Font("Sans Serif",Font.BOLD,24));
		
		for(int i = 0; i < game.getParty().size(); i++) {
			player = game.getParty().get(i);
			
			g.setFont(new Font("Sans Serif",Font.BOLD,24));
			g.drawString(player.getName(), x1+50, y1+50+(i*40));
			
			g.setFont(new Font("Sans Serif",Font.BOLD,12));
			g.drawString("HP " + player.getHP() + "/" + player.getMaxHP(), x1+150, y1+(35)+(i*40));
			g.drawString("MP " + player.getMP() + "/" + player.getMaxMP(), x1+150, y1+(50)+(i*40));
		}
		
		g.setFont(new Font("Sans Serif",Font.BOLD,30));
		g.drawString("Cancel", x1+50, y1+240);
		
		//Draw Cursor
		if(cursorPosition == RESULT_CANCEL) {
			g.fillRect(x1+26, y1+220, 16, 16);
		}
		else {
			g.fillRect(x1+26, y1+30+(cursorPosition * 40), 16, 16);
		}
		
	}

	
	@Override
	public Integer getResult() {
		return cursorPosition;
	}
	
	
	@Override
	public Boolean isProcessed() {
		return isDialogProcessed;
	}
	
	
	@Override
	public void processKeyEvent(KeyEvent e) {
		int numPlayers = game.getParty().size();
		
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			cursorPosition++;
			
			if(cursorPosition > (numPlayers-1)) {
				cursorPosition = PartyDialog.RESULT_CANCEL;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			cursorPosition--;
			
			if(cursorPosition < PartyDialog.RESULT_CANCEL) {
				cursorPosition = numPlayers-1;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			//do nothing
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			//do nothing
		}
		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() {
		isDialogProcessed = true;
	}

	
	private void processEscapeKey() {
		cursorPosition = PartyDialog.RESULT_CANCEL;
		isDialogProcessed = true;
	}
	
}
