package ei.game.gamestate;

import ei.engine.LWJGLGameWindow;
import ei.engine.math.Vector2f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.state.GameState;
import ei.engine.state.GameStateManager;
import ei.engine.ui.Button;
import ei.engine.ui.UiComponent;
import ei.engine.ui.UiHandler;
import ei.engine.ui.UiListener;
import ei.game.input.MenuKeyboardInput;
import ei.game.input.MenuMouseInput;

public class MenuState extends GameState implements UiListener{
	private Node menuNode;
	
	private Button resume;
	private Button newGame;
	private Button join;
	private Button host;
	private Button quit;
	
	public MenuState(String name){
		super(name);
	}
	
	@Override
	public void init() {
		menuNode = new Node("MenuNode");
		
		int x = LWJGLGameWindow.getWidth()/2;
		int y = 250;
		
		Sprite logo = new Sprite("Logo","data/logo.png");
		logo.setLocation(new Vector2f(x,100));
		menuNode.add(logo);
		
		UiHandler ui = new UiHandler("MainMenu");
		
		Sprite s1 = new Sprite("ResumeButton","data/ui/resume.png");
		resume = new Button("Resume", s1.getSize());
		resume.setButtonSprite(s1);
		resume.setOnTopButtonSprite(new Sprite("ResumeButton_Selected","data/ui/resume_ontop.png"));
		resume.setDisabledButtonSprite(new Sprite("ResumeButton_Disabled","data/ui/resume_disabled.png"));
		resume.getNode().setLocation(new Vector2f(x,y));
		resume.addListener(this);
		resume.setEnabled(false);
		ui.addUi(resume);
		
		y += 40;
		Sprite s2 = new Sprite("NewButton","data/ui/new.png");
		newGame = new Button("New", s2.getSize());
		newGame.setButtonSprite(s2);
		newGame.setOnTopButtonSprite(new Sprite("NewButton_Selected","data/ui/new_ontop.png"));
		newGame.getNode().setLocation(new Vector2f(x,y));
		newGame.addListener(this);
		ui.addUi(newGame);
		
		y += 40;
		Sprite s3 = new Sprite("JoinButton","data/ui/join.png");
		join = new Button("Join", s3.getSize());
		join.setButtonSprite(s3);
		join.setOnTopButtonSprite(new Sprite("JoinButton_Selected","data/ui/join_ontop.png"));
		join.getNode().setLocation(new Vector2f(x,y));
		join.addListener(this);
		ui.addUi(join);
		
		y += 40;
		Sprite s4 = new Sprite("HostButton","data/ui/host.png");
		host = new Button("Host", s4.getSize());
		host.setButtonSprite(s4);
		host.setOnTopButtonSprite(new Sprite("HostButton_Selected","data/ui/host_ontop.png"));
		host.getNode().setLocation(new Vector2f(x,y));
		host.addListener(this);
		ui.addUi(host);
		
		y += 40;
		Sprite s5 = new Sprite("QuitButton","data/ui/quit.png");
		quit = new Button("Quit", s5.getSize());
		quit.setButtonSprite(s5);
		quit.setOnTopButtonSprite(new Sprite("QuitButton_Selected","data/ui/quit_ontop.png"));
		quit.getNode().setLocation(new Vector2f(x,y));
		quit.addListener(this);
		ui.addUi(quit);
		
		menuNode.add(ui.getNode());
		
		MenuMouseInput mouse = new MenuMouseInput(ui);
		super.getInput().addInput(mouse);
		super.getInput().addInput(new MenuKeyboardInput());
	}
	
	@Override
	public void render() {
		menuNode.render();
	}

	@Override
	public void update() {
		menuNode.update();
		if(GameStateManager.getInstance().getState("InGameState") != null){
			resume.setEnabled(true);
		}
		else{
			resume.setEnabled(false);
		}
	}
	
	public void ActionEvent(UiComponent source){
		if(resume == source){
			GameStateManager.getInstance().setDeActive(this.getName());
			GameStateManager.getInstance().setActive("InGameState");
		}
		else if(newGame == source){
			GameStateManager.getInstance().removeStateByName("InGameState");
			GameStateManager.getInstance().addState(new InGameState("InGameState"));
			GameStateManager.getInstance().addState(new LoadingState("LoadingState","InGameState"));
			GameStateManager.getInstance().setDeActive(this.getName());
			GameStateManager.getInstance().setActive("LoadingState");
		}
		else if(quit == source){
			LWJGLGameWindow.exit();
		}
	}
}
