package rpgEngine.UI;

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

import rpgEngine.Resources.FontPool;
import rpgEngine.Resources.ImagePool;

public class ConfirmDialog extends GenericDialog {
	public static final Integer RESULT_NO = 0;
	public static final Integer RESULT_YES = 1;

	private Boolean isDialogProcessed;
	private Integer cursorPosition;
	
	public ConfirmDialog() {
		isDialogProcessed = false;
		cursorPosition = ConfirmDialog.RESULT_NO;
	}
	
	
	@Override
	public void drawDialogCenteredAt(Graphics g, int x, int y) {
		int x1 = x - 132;
		int y1 = y - 59;
		
		//Draw Background
		g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_DIALOG_BACKGROUND),x1,y1,264,118,null);
		
		//Draw Text
		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("No", x1+60, y1+70);
		g.drawString("Yes", x1+160, y1+70);
		
		//Draw Cursor
		if(cursorPosition == RESULT_YES) {
			g.fillRect(x1+130, y1+50, 16, 16);
		}
		else if(cursorPosition == RESULT_NO) {
			g.fillRect(x1+30, y1+50, 16, 16);
		}
	}

	
	@Override
	public Integer getResult() {
		return cursorPosition;
	}
	
	
	@Override
	public Boolean isProcessed() {
		return isDialogProcessed;
	}
	
	
	@Override
	public void processKeyEvent(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == 0) {
				cursorPosition = 1;
			}
			else {
				cursorPosition = 0;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == 1) {
				cursorPosition = 0;
			}
			else {
				cursorPosition = 1;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == 1) {
				cursorPosition = 0;
			}
			else {
				cursorPosition = 1;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == 0) {
				cursorPosition = 1;
			}
			else {
				cursorPosition = 0;
			}
		}
		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 = ConfirmDialog.RESULT_NO;
		isDialogProcessed = true;
	}
	
	
	
}
