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

public class SaveGamePanel 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 SaveGamePanel(Integer id, MainFrame p) {
		super(id, p);
		
		camera = new SaveGameCamera(this);
		cursorPosition = CURSOR_CANCEL;
	}
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_CANCEL) {
				cursorPosition = CURSOR_GAME1;
			}
		}
		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) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_GAME2) {
				cursorPosition = CURSOR_GAME1;
			}
			else if(cursorPosition == CURSOR_GAME3) {
				cursorPosition = CURSOR_GAME2;
			}
			else if(cursorPosition == CURSOR_GAME4) {
				cursorPosition = CURSOR_GAME3;
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_GAME1) {
				cursorPosition = CURSOR_GAME2;
			}
			else if(cursorPosition == CURSOR_GAME2) {
				cursorPosition = CURSOR_GAME3;
			}
			else if(cursorPosition == CURSOR_GAME3) {
				cursorPosition = CURSOR_GAME4;
			}
			else if(cursorPosition == CURSOR_GAME4) {
				cursorPosition = CURSOR_GAME1;
			}
		}
		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_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_GAME1) {
			GameFactory.saveGame(0, this.getParentMainFrame().getGame());
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_GAME2) {
			GameFactory.saveGame(1, this.getParentMainFrame().getGame());
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);		
		}
		else if(cursorPosition == CURSOR_GAME3) {
			GameFactory.saveGame(2, this.getParentMainFrame().getGame());
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_GAME4) {
			GameFactory.saveGame(3, this.getParentMainFrame().getGame());
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
	}
	
	
	private void processEscapeKey() {		
		this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
	}
	
	
	@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;
	}
	
}
