package holo.bolt.states;

import holo.bolt.BoltMain;

import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.FontUtils;

public class GameOverState extends BasicGameState
{
	public int level;
	public int score;
	public int startScore;
	
	public float defaultScale;
	
	public Image gameOver;
	public int gameOverX;
	public int gameOverY;

	public Image retry;
	public int retryX;
	public int retryY;
	public float retryScale;
	public MouseOverArea retryBox;
	
	public Image menu;
	public int menuX;
	public int menuY;
	public float menuScale;
	public MouseOverArea menuBox;
	
	public StateBasedGame game;
	public GameContainer container;
	
	@Override
	public void init(GameContainer gc, StateBasedGame game)
			throws SlickException
	{
		this.game = game;
		container = gc;
		
		defaultScale = gc.getHeight() / 480;
		
		gameOver = new Image("res/GameOverText.png").getScaledCopy(0.8F);
		gameOverX = gc.getWidth() / 2;
		gameOverY = gameOver.getHeight() / 2;
		
		retry = new Image("res/Retry.png").getScaledCopy(0.5F);
		retryX = gc.getWidth() - retry.getWidth() / 2 - retry.getWidth() / 20;
		retryY = gc.getHeight() - retry.getHeight() / 2;
		retryScale = defaultScale;
		retryBox = new MouseOverArea(gc, retry, retryX - retry.getWidth() / 2, retryY - retry.getHeight() / 2);
		
		menu = new Image("res/Menu.png").getScaledCopy(0.5F);
		menuX = menu.getWidth() / 2 + menu.getWidth() / 20;
		menuY = gc.getHeight() - menu.getHeight() / 2;
		menuScale = defaultScale;
		menuBox = new MouseOverArea(gc, menu, menuX - menu.getWidth() / 2, menuY - menu.getHeight() / 2);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException
	{
		Font f = gc.getDefaultFont();
		gameOver.drawCentered(gc.getWidth() / 2, gameOver.getHeight() / 2);
		retry.getScaledCopy(retryScale).drawCentered(retryX, retryY);
		menu.getScaledCopy(menuScale).drawCentered(menuX, retryY);
		
		String s = "You died on level " + level + ".";
		FontUtils.drawCenter(f, s, gc.getWidth() / 2 - f.getWidth(s) / 2, gameOver.getHeight() + f.getLineHeight(), f.getWidth(s));
		s = "Your final score was " + score + ".";
		FontUtils.drawCenter(f, s, gc.getWidth() / 2 - f.getWidth(s) / 2, gameOver.getHeight() + f.getLineHeight() * 2, f.getWidth(s));
	}

	@Override
	public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException
	{
		if(retryBox.isMouseOver())
			retryScale = defaultScale * 1.05F;
		else
			retryScale = defaultScale;
		if(menuBox.isMouseOver())
			menuScale = defaultScale * 1.05F;
		else
			menuScale = defaultScale;
	}

	@Override
	public int getID()
	{
		return BoltMain.GAME_OVER_ID;
	}
	
	public void updateStats(int level, int score, int levelStartScore)
	{
		this.level = level;
		this.score = score;
		this.startScore = levelStartScore;
	}
	
	public void mousePressed(int button, int x, int y) 
	{
		if(button == Input.MOUSE_LEFT_BUTTON)
		{
			if(menuBox.isMouseOver())
			{
				((GameState)game.getState(BoltMain.GAME_ID)).reset();
				game.enterState(BoltMain.MAIN_MENU_ID);
			}
			else if(retryBox.isMouseOver())
			{
				((GameState)game.getState(BoltMain.GAME_ID)).initLevel(this.level - 1, startScore);
				game.enterState(BoltMain.GAME_ID);
			}
		}
	}
}
