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.LoadGameCamera;
import rpgEngine.Logic.Game.GameFactory;

public class LoadGamePanel extends GenericPanel {
	private static final long serialVersionUID = 6538729434640140830L;
	private GenericCamera camera;
	private Integer cursorPosition;
	
	public static final Integer CURSOR_CANCEL = 0;
	public static final Integer CURSOR_GAME1 = 1;
	public static final Integer CURSOR_GAME2 = 2;
	public static final Integer CURSOR_GAME3 = 3;
	public static final Integer CURSOR_GAME4 = 4;
	
	
	public LoadGamePanel(Integer id, MainFrame p) {
		super(id, p);
		
		camera = new LoadGameCamera(this);
		cursorPosition = CURSOR_CANCEL;
	}
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_CANCEL) {
				if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == CURSOR_GAME1) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_GAME2) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_GAME3) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_GAME4) {
				cursorPosition = CURSOR_CANCEL;
			}
		} 
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == CURSOR_GAME1) {
				if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else {
					cursorPosition = CURSOR_GAME1;
				}
			}
			else if(cursorPosition == CURSOR_GAME2) {
				if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else {
					cursorPosition = CURSOR_GAME2;
				}
			}
			else if(cursorPosition == CURSOR_GAME3) {
				if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else {
					cursorPosition = CURSOR_GAME3;
				}
			}
			else if(cursorPosition == CURSOR_GAME4) {
				if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else {
					cursorPosition = CURSOR_GAME4;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_GAME1) {
				if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else {
					cursorPosition = CURSOR_GAME1;
				}
			}
			else if(cursorPosition == CURSOR_GAME2) {
				if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else {
					cursorPosition = CURSOR_GAME2;
				}
			}
			else if(cursorPosition == CURSOR_GAME3) {
				if(GameFactory.doesSaveGameExist(3)) {
					cursorPosition = CURSOR_GAME4;
				}
				else if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else {
					cursorPosition = CURSOR_GAME3;
				}
			}
			else if(cursorPosition == CURSOR_GAME4) {
				if(GameFactory.doesSaveGameExist(0)) {
					cursorPosition = CURSOR_GAME1;
				}
				else if(GameFactory.doesSaveGameExist(1)) {
					cursorPosition = CURSOR_GAME2;
				}
				else if(GameFactory.doesSaveGameExist(2)) {
					cursorPosition = CURSOR_GAME3;
				}
				else {
					cursorPosition = CURSOR_GAME4;
				}
			}
		}
		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_CANCEL) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_TITLE);
		}
		else if(cursorPosition == CURSOR_GAME1) {
			this.getParentMainFrame().setGame(GameFactory.loadGame(0));
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
		}
		else if(cursorPosition == CURSOR_GAME2) {
			this.getParentMainFrame().setGame(GameFactory.loadGame(1));
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);		
		}
		else if(cursorPosition == CURSOR_GAME3) {
			this.getParentMainFrame().setGame(GameFactory.loadGame(2));
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
		}
		else if(cursorPosition == CURSOR_GAME4) {
			this.getParentMainFrame().setGame(GameFactory.loadGame(3));
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
		}
	}
	
	
	private void processEscapeKey() {		
		this.getParentMainFrame().showScreen(MainFrame.PANEL_TITLE);
	}
	
	
	@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;
	}
}
