package net.greencouchgames.ld27;

import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.util.color.Color;

import tv.ouya.console.api.OuyaController;

public class s_Menu extends Scene {
	Sprite spr_hand;
	Sprite spr_player;
	e_Player player;
	
	Entity layer_bg;
	Entity layer_menu;
	
	Sprite spr_logo;
	Sprite spr_bg;
	Rectangle menurect;
	Text presso;
	
	public float introtimer = -3;
	public boolean introFinished = false;
	public int wooshs = -1;
	
	public s_Menu(boolean skipIntro) {
		if (1==1 || skipIntro) introtimer=3;
		setBackground(new Background(0.09804f, 0.6274f, 0));

		layer_bg = new Entity();
		attachChild(layer_bg);
		layer_menu = new Entity();
		attachChild(layer_menu);

		spr_bg = new Sprite(0, 0, Main.spr_bgmenu, Main.root.getVertexBufferObjectManager());
		spr_bg.setColor(new Color(0,0,0));
		layer_bg.attachChild(spr_bg);
		spr_logo = new Sprite(0, 0, Main.spr_logo, Main.root.getVertexBufferObjectManager());
		spr_logo.setPosition(640-spr_logo.getWidth()/(float)2,360-spr_logo.getHeight()/(float)2);
		spr_logo.setRotationCenter(spr_logo.getWidth()/(float)2,spr_logo.getHeight()/(float)2+2);
		spr_logo.setScaleCenter(spr_logo.getWidth()/(float)2,spr_logo.getHeight()/(float)2+2);
		spr_logo.setVisible(false);
		layer_bg.attachChild(spr_logo);
		menurect = new Rectangle(640, 600, 1, 48, Main.root.getVertexBufferObjectManager());
		menurect.setColor(new Color(0,0,0));
		menurect.setVisible(false);
		menurect.setAlpha(0.5f);
		layer_menu.attachChild(menurect);
		presso = new Text(640, 600, Main.font2, "PRESS O FOR SINGLEPLAYER\nPRESS U FOR MULTIPLAYER\nPRESS Y FOR TUTORIAL", Main.root.getVertexBufferObjectManager());
		presso.setColor(new Color(1,0,0));
		presso.setVisible(false);
		presso.setPosition(640-(float)presso.getWidth()/(float)2,600);
		menurect.setPosition(640-(float)presso.getWidth()/(float)2-10,590);
		menurect.setSize(presso.getWidth()+20,presso.getHeight()+20);
		layer_menu.attachChild(presso);
	}
	
    @Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		
		if (!introFinished) {
			introtimer += pSecondsElapsed;
			if (introtimer>=3) {
				introFinished=true;
				presso.setVisible(true);
				menurect.setVisible(true);
				spr_bg.setColor(new Color(1,1,1));
				spr_logo.setRotation(0);
				spr_logo.setScale(1);
				spr_logo.setPosition(640-spr_logo.getWidth()/(float)2,50);
				spr_logo.setVisible(true);
				Main.menumusic.resume();
			} else {
				if (introtimer>=2) {
					spr_bg.setColor(new Color(introtimer-2,introtimer-2,introtimer-2));
					spr_logo.setPosition(640-spr_logo.getWidth()/(float)2,(360-spr_logo.getHeight()/(float)2)-(-(360-spr_logo.getHeight()/(float)2)+340)*(introtimer-2));
				} else if (introtimer>=1) {
					spr_logo.setRotation(0);
					spr_logo.setScale(1);
				} else if (introtimer>=0){
					if (Math.floor(introtimer/0.25f)>wooshs) {
						Main.thrown.play();
						wooshs++;
					}
					spr_logo.setRotation(1480*introtimer);
					spr_logo.setScale(10-9*introtimer);
					spr_logo.setVisible(true);
				}
			}
		}
		
		OuyaController.startOfFrame();
	}
}
