package model;

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

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

public class GameStateManager {

	private List<State> gamestates;
	private State currentState;

	public GameStateManager() {
		gamestates = new ArrayList<State>();

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

		currentState = gamestates.get(0);
	}

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

	private void changeState(State st) {
		if (!currentState.equals(st)) {
			currentState.exit();
			currentState = st;
			currentState.init();
		}
	}

	public void update() {
		currentState.update();
	}

	public void paint(Graphics2D g2d) {
		currentState.paint(g2d);
	}

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

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

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