package rpgEngine.UI;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Camera.MainMenuCamera;


public class MainMenuPanel extends GenericPanel {
	private static final long serialVersionUID = 6538729434652140830L;
	private GenericCamera camera;
	private Integer cursorPosition;
	private GenericDialog confirmQuitDialog;
	
	public static final Integer CURSOR_ITEMS = 0;
	public static final Integer CURSOR_SKILLS = 1;
	public static final Integer CURSOR_EQUIPMENT = 2;
	public static final Integer CURSOR_STATUS = 3;
	public static final Integer CURSOR_SAVE = 4;
	public static final Integer CURSOR_QUIT = 5;
	
	
	public MainMenuPanel(Integer id, MainFrame p) {
		super(id, p);
		
		confirmQuitDialog = null;
		camera = new MainMenuCamera(this);
		cursorPosition = CURSOR_ITEMS;
	}
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		//handle dialog input since its modal
		if(confirmQuitDialog != null) {
			confirmQuitDialog.processKeyEvent(e);
			
			if(confirmQuitDialog.isProcessed() == false) {
				return;
			}
			else {
				if(confirmQuitDialog.getResult() == ConfirmDialog.RESULT_YES) {
					System.exit(0);
				}
				else {
					confirmQuitDialog = null;
					return;
				}
			}
		}
		
		//handle normal input otherwise
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition < 5) {
				cursorPosition++;
			}
			else {
				cursorPosition = 0;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition > 0) {
				cursorPosition--;
			}
			else {
				cursorPosition = 5;
			}
		}
		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_ITEMS) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_ITEMS);
		}
		else if(cursorPosition == CURSOR_SKILLS) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_SKILLS);
		}
		else if(cursorPosition == CURSOR_EQUIPMENT) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_EQUIP);
		}
		else if(cursorPosition == CURSOR_STATUS) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_STATUS);
		}		
		else if(cursorPosition == CURSOR_SAVE) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_SAVE_GAME);
		}
		else if(cursorPosition == CURSOR_QUIT) {
			confirmQuitDialog = new ConfirmDialog();
		}
		
	}
	
	
	private void processEscapeKey() {
		this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
	}
	
	@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(confirmQuitDialog != null) {
        	confirmQuitDialog.drawDialogCenteredAt(g, this.getWidth()/2, this.getHeight()/2);
        }
    }

	public Integer getCursorPosition() {
		return cursorPosition;
	}

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

}
