package model;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import model.state.MenuState;
import model.state.PlayState;
import model.state.State;

public class GameStateManager {

	private List<State> gamestates;
	private State currentState;

	// Manage all the states and allow to switch between them
	public GameStateManager() {
		gamestates = new ArrayList<State>();

		gamestates.add(new MenuState(this));
		gamestates.add(new PlayState(this));

		currentState = gamestates.get(0);
	}

	// Change the current state
	public void setState(String name) {
		for (State s : gamestates) {
			if (s.getName() == name) {
				changeState(s);
				return;
			}
		}
	}

	// Get the current state
	public State getState(String name) {
		for (State s : gamestates) {
			if (s.getName() == name) {
				return s;
			}
		}
		return null;
	}

	// Private method to change the state
	private void changeState(State st) {
		if (!currentState.equals(st)) {
			currentState.exit();
			currentState = st;
			currentState.init();
		}
	}

	// Update the current state
	public void update() {
		currentState.update();
	}

	// Paint the current state
	public void paint(Graphics2D g2d) {
		currentState.paint(g2d);
	}

	/*
	 * Send keypresses to the current state
	 */

	public void keyPressed(KeyEvent e) {
		currentState.keyPressed(e);
	}

	public void keyReleased(KeyEvent e) {
		currentState.keyReleased(e);
	}

	public void keyTyped(KeyEvent e) {
		currentState.keyTyped(e);
	}
}
