package ei.game.gamestate;

import ei.engine.LWJGLGameWindow;
import ei.engine.effects.Fade;
import ei.engine.math.Vector3f;
import ei.engine.scene.Sprite;
import ei.engine.state.GameState;
import ei.engine.state.GameStateManager;

public class EndGameState extends GameState{
	public static final int VICTORY = 0;
	public static final int DEFEAT = 1;
	private Fade fade;
	private int status;
	private int timer;
	
	
	public EndGameState(String name, int status){
		super(name,LWJGLGameWindow.getCamera());
		this.status = status;
	}
	
	@Override
	public void init() {
		fade = new Fade("EGameStateFade");
		fade.fadeOut(false);
		switch(status){
		case VICTORY:
			Sprite v = new Sprite("Victory","data/victory.png");
			v.setLocation(new Vector3f(
					(LWJGLGameWindow.getWidth()/2+LWJGLGameWindow.getCamera().getLocation().getX()),
					(LWJGLGameWindow.getHeight()/2+LWJGLGameWindow.getCamera().getLocation().getY()),0.0f));
			fade.setEntity(v);
			break;
		case DEFEAT:
			Sprite d = new Sprite("Defeat","data/defeat.png");
			d.setLocation(new Vector3f(
					(LWJGLGameWindow.getWidth()/2+LWJGLGameWindow.getCamera().getLocation().getX()),
					(LWJGLGameWindow.getHeight()/2+LWJGLGameWindow.getCamera().getLocation().getY()),0.0f));
			fade.setEntity(d);
			break;
		}
		InGameState.input(false);
	}

	@Override
	public void render() {
		fade.render();		
	}

	@Override
	public void update() {
		fade.update();
		if(fade.isDone() && timer <= 120){
			timer++;
		}
		else if(fade.isDone() && timer > 120){
			GameStateManager.getInstance().removeState(this);
			if(GameStateManager.getInstance().getState("MenuState") != null){
				GameStateManager.getInstance().removeStateByName("InGameState");
				GameStateManager.getInstance().setActive("MenuState");
			}
			else{
				LWJGLGameWindow.exit();
			}
		}
	}

}
