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_Game extends Scene {
	Sprite spr_hand;
	Sprite spr_player;
	e_Player player;
	
	int score = 0;
	int max = 2;
	int spawns = 0;
	float timer = 2;
	float tanktimer = 25;
	
	Entity layer_bg;
	Entity layer_part;
	Entity layer_proj;
	Entity layer_play;
	Entity layer_tank;
	Entity layer_prot;
	Entity layer_hud;
	
	public boolean paused = false;
	
	public Text scoreText;
	public Text gsText;
	Rectangle fade;
	Text gameover;
	public float gotimer = 10;
	
	public int controller;
	public boolean skiprest = false;
	
	public s_Game(int c) {
		controller = c;
		setBackground(new Background(0.09804f, 0.6274f, 0));

		u_Trail.removeTrail();
		u_Projectiles.removeProjectiles();
		u_Enemy.removeEnemies();
		u_Enemy.stop=false;
		Main.gsmusic.pause();
		Main.gsmusic.seekTo(0);
		Main.tankmusic.pause();
		Main.tankmusic.seekTo(0);

		layer_bg = new Entity();
		attachChild(layer_bg);
		layer_part = new Entity();
		attachChild(layer_part);
		layer_proj = new Entity();
		attachChild(layer_proj);
		layer_play = new Entity();
		attachChild(layer_play);
		layer_tank = new Entity();
		attachChild(layer_tank);
		layer_prot = new Entity();
		attachChild(layer_prot);
		layer_hud = new Entity();
		attachChild(layer_hud);

		Sprite spr_bg = new Sprite(0, 0, Main.spr_bgsp, Main.root.getVertexBufferObjectManager());
		layer_bg.attachChild(spr_bg);
		
		player = new e_Player(0);
		spr_hand = new Sprite(632, 352, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		spr_hand.setScaleCenter(8,8);
		spr_hand.setRotationCenter(8,8);
		spr_hand.setColor(player.hand);
		layer_play.attachChild(spr_hand);
		spr_player = new Sprite(608, 328, Main.spr_player, Main.root.getVertexBufferObjectManager());
		spr_player.setScaleCenter(32,32);
		spr_player.setRotationCenter(32,32);
		layer_play.attachChild(spr_player);

		Sprite spr_over = new Sprite(717, 0, Main.spr_bgscore, Main.root.getVertexBufferObjectManager());
		layer_hud.attachChild(spr_over);
		Text sL = new Text(780, 32, Main.font1, "SCORE", Main.root.getVertexBufferObjectManager());
		sL.setPosition(780-sL.getWidth(), 32);
		sL.setColor(new Color(0.3f,0.3f,0.3f));
		layer_hud.attachChild(sL);
		Text gL = new Text(1080, 32, Main.font1, "GODSPEED", Main.root.getVertexBufferObjectManager());
		gL.setPosition(1080-gL.getWidth(), 32);
		gL.setColor(new Color(0.3f,0.3f,0.3f));
		layer_hud.attachChild(gL);
		scoreText = new Text(800, 24, Main.font2, "00000000", Main.root.getVertexBufferObjectManager());
		scoreText.setColor(new Color(0,0,0));
		layer_hud.attachChild(scoreText);
		gsText = new Text(1100, 24, Main.font2, "000%", Main.root.getVertexBufferObjectManager());
		gsText.setColor(new Color(0,0,0));
		layer_hud.attachChild(gsText);
	}
	
    @Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
    	float ps = ((pSecondsElapsed<(float)1/(float)30) ? pSecondsElapsed : (float)1/(float)30);
		super.onManagedUpdate(ps);
		if (paused) return;
		u_Trail.updateTrail(ps);
		u_Projectiles.updateProjectiles(ps);
		u_Enemy.updateEnemies(ps);
		u_Explosion.updateExplosions(ps);
		
		if (skiprest) return;

		OuyaController c1 = OuyaController.getControllerByDeviceId(controller);
		
		if (fade!=null) {
			if (gotimer<=9) if (fade.getAlpha()>0.005) fade.setAlpha(fade.getAlpha()-0.005f);
			if (gameover.getX()>640-(float)gameover.getWidth()/(float)2) gameover.setPosition(gameover.getX()-10,gameover.getY());
			else gameover.setPosition(640-(float)gameover.getWidth()/(float)2,gameover.getY());
			gotimer-=pSecondsElapsed;
			if (gotimer<=8) {
				boolean getout = false;
				if (c1 != null) {
					if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
						getout=true;
						Main.select.play();
					}
				}
				if (gotimer<=0) getout=true;
				
				if (getout) {
					for (int i=0; i<layer_bg.getChildCount(); i++) layer_bg.getChildByIndex(i).dispose();
					for (int i=0; i<layer_part.getChildCount(); i++) layer_part.getChildByIndex(i).dispose();
					for (int i=0; i<layer_proj.getChildCount(); i++) layer_proj.getChildByIndex(i).dispose();
					for (int i=0; i<layer_play.getChildCount(); i++) layer_play.getChildByIndex(i).dispose();
					for (int i=0; i<layer_tank.getChildCount(); i++) layer_tank.getChildByIndex(i).dispose();
					for (int i=0; i<layer_prot.getChildCount(); i++) layer_prot.getChildByIndex(i).dispose();
					for (int i=0; i<layer_hud.getChildCount(); i++) layer_hud.getChildByIndex(i).dispose();
					layer_bg.detachChildren();
					layer_part.detachChildren();
					layer_proj.detachChildren();
					layer_play.detachChildren();
					layer_tank.detachChildren();
					layer_prot.detachChildren();
					layer_hud.detachChildren();
					layer_bg.detachSelf(); layer_bg.dispose();
					layer_part.detachSelf(); layer_part.dispose();
					layer_proj.detachSelf(); layer_proj.dispose();
					layer_play.detachSelf(); layer_play.dispose();
					layer_tank.detachSelf(); layer_tank.dispose();
					layer_prot.detachSelf(); layer_prot.dispose();
					layer_hud.detachSelf(); layer_hud.dispose();
					u_Enemy.stop=false;
					Main.root.setCurrentScene(new s_Menu(true));
				}
			}
			return;
		}
		
		if (u_Explosion.timer>0) {
			u_Explosion.timer-=ps;
			if (u_Explosion.timer<=0) {
				Main.root.resetCamera();
				u_Explosion.timer=0;
			} else Main.root.shakeCamera();
		}
		
		timer-=ps;
		tanktimer-=ps;
		if (timer<=0) {
			spawns++;
			if (spawns % 40 == 0) max++;
			int a = 3;
			if (tanktimer<=0) {
				int s = (int)Math.floor(Math.random()*2);
				if (s==0) u_Enemy.addTank(-64, (float) (Math.random()*360)+180, 0);
				else u_Enemy.addTank(1344, (float) (Math.random()*360)+180, 1);
				tanktimer = (float) (15+Math.random()*10);
			} else {
				a = (int)Math.ceil(Math.random()*max);
				if (a==0) a=1;
				int s = (int)Math.floor(Math.random()*4);
				for (int i=0; i<a; i++) {
					if (s==0) u_Enemy.addEnemy((float) (Math.random()*1280), -32);
					else if (s==1) u_Enemy.addEnemy((float) (Math.random()*1280), 752);
					else if (s==2) u_Enemy.addEnemy(-32, (float) (Math.random()*720));
					else u_Enemy.addEnemy(1312, (float) (Math.random()*720));
				}
			}
			timer = a*2 + (float)Math.random()*2;
		}
		
		if (c1 != null) {
			player.update(ps*Main.multi,c1,spr_player,spr_hand);
		}
		
		if (score<10) scoreText.setText("0000000"+score);
		else if (score<100) scoreText.setText("000000"+score);
		else if (score<1000) scoreText.setText("00000"+score);
		else if (score<10000) scoreText.setText("0000"+score);
		else if (score<100000) scoreText.setText("000"+score);
		else if (score<1000000) scoreText.setText("00"+score);
		else if (score<10000000) scoreText.setText("0"+score);
		else scoreText.setText(""+score);
		
		int p = (int)Math.floor(player.godspeedbar*100);
		if (p>100) p=100;
		if (p<0) p=0;
		if (p<10) gsText.setText("00"+p+"%");
		else if (p<100) gsText.setText("0"+p+"%");
		else gsText.setText(p+"%");
		
		OuyaController.startOfFrame();
	}
	
	public void gameover() {
		Main.gameover.play();
		u_Trail.removeTrail();
		u_Projectiles.removeProjectiles();
		u_Enemy.removeEnemies();
		spr_player.detachSelf();
		if (!spr_player.isDisposed()) spr_player.dispose();
		spr_hand.detachSelf();
		if (!spr_hand.isDisposed()) spr_hand.dispose();
		fade = new Rectangle(0, 0, 1280, 720, Main.root.getVertexBufferObjectManager());
		layer_hud.attachChild(fade);
		gameover = new Text(1280, 360, Main.font3, "GAME OVER", Main.root.getVertexBufferObjectManager());
		gameover.setPosition(1280,360-(float)gameover.getHeight()/(float)2);
		gameover.setColor(new Color(0,0,0));
		layer_hud.attachChild(gameover);
		u_Enemy.stop=true;
	}
}
