package net.greencouchgames.ld27;

import org.andengine.entity.sprite.Sprite;
import org.andengine.util.color.Color;

import android.util.Log;

import tv.ouya.console.api.OuyaController;

public class s_Tutorial extends s_Game {
	public int tutstep = 1;
	public int tuttimer = 0;
	
	public Sprite tutorial1;
	public Sprite tutorial2;
	public Sprite tutorial3;
	public Sprite tutorial4;
	public Sprite tutorial5;
	public Sprite tutorial6;
	public Sprite close;
	
	public e_Projectile p;
	public e_Enemy e1;
	public e_Enemy e2;
	public e_Enemy e3;
	
    public s_Tutorial(int c) {
		super(c);
		skiprest=true;
		
		tutorial1 = new Sprite(20, 20, Main.spr_tutorial1, Main.root.getVertexBufferObjectManager());
		layer_hud.attachChild(tutorial1);
		tutorial2 = new Sprite(20, 20, Main.spr_tutorial2, Main.root.getVertexBufferObjectManager());
		tutorial2.setVisible(false);
		layer_hud.attachChild(tutorial2);
		tutorial3 = new Sprite(20, 20, Main.spr_tutorial3, Main.root.getVertexBufferObjectManager());
		tutorial3.setVisible(false);
		layer_hud.attachChild(tutorial3);
		tutorial4 = new Sprite(20, 20, Main.spr_tutorial4, Main.root.getVertexBufferObjectManager());
		tutorial4.setVisible(false);
		layer_hud.attachChild(tutorial4);
		tutorial5 = new Sprite(20, 20, Main.spr_tutorial5, Main.root.getVertexBufferObjectManager());
		tutorial5.setVisible(false);
		layer_hud.attachChild(tutorial5);
		tutorial6 = new Sprite(20, 20, Main.spr_tutorial6, Main.root.getVertexBufferObjectManager());
		tutorial6.setVisible(false);
		layer_hud.attachChild(tutorial6);
		close = new Sprite(20, 20, Main.spr_close, Main.root.getVertexBufferObjectManager());
		close.setPosition(20+tutorial5.getWidth()-close.getWidth(), 20+tutorial5.getHeight()+10);
		layer_hud.attachChild(close);
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed*Main.multi);
		if (paused) return;
		
		OuyaController c1 = OuyaController.getControllerByDeviceId(controller);
		if (c1 != null) {
			player.update(pSecondsElapsed*Main.multi,c1,spr_player,spr_hand);
		}
		
		if (u_Explosion.timer>0) {
			u_Explosion.timer-=pSecondsElapsed;
			if (u_Explosion.timer<=0) {
				Main.root.resetCamera();
				u_Explosion.timer=0;
			} else Main.root.shakeCamera();
		}
		
		if (tutstep==1) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				tutstep++;
				u_Projectiles.p.get(u_Projectiles.p.size()-1).forcedestroy = true;
				tutorial1.setVisible(false);
				close.setVisible(false);
				p = u_Projectiles.addBakedGood(90, 90);
				float tempX = p.spr.getX();
				float tempY = p.spr.getY();
				float tempR = p.spr.getRotation();
				p.spr.detachSelf();
				p.spr.dispose();
				p.spr = new Sprite(tempX, tempY, Main.spr_baked2, Main.root.getVertexBufferObjectManager());
				p.sprite = Main.spr_baked2;
				p.rottype = 2;
				p.spr.setRotation(tempR);
				p.trail = new Color(1,0.8f,0.66f);
				p.trailsize = 1;
				p.xs = p.ys = 0;
				layer_proj.attachChild(p.spr);
			}
		} else if (tutstep==2) {
			if (p.grabbed || p.thrown || tuttimer>0) {
				tuttimer++;
				if (tuttimer>=30) {
					tuttimer=0;
					tutstep++;
					tutorial2.setVisible(true);
					close.setVisible(true);
					p = null;
				}
			}
		} else if (tutstep==3) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				tutstep++;
				u_Projectiles.p.get(u_Projectiles.p.size()-1).forcedestroy = true;
				tutorial2.setVisible(false);
				close.setVisible(false);
				e1 = u_Enemy.addEnemy(1000,600);
				e1.speed = 0;
			}
		} else if (tutstep==4) {
			if (e1.spr==null) {
				tutstep++;
				tutorial3.setVisible(true);
				close.setVisible(true);
				e1 = null;
			}
		} else if (tutstep==5) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				tutstep++;
				u_Projectiles.p.get(u_Projectiles.p.size()-1).forcedestroy = true;
				tutorial3.setVisible(false);
				close.setVisible(false);
				e1 = u_Enemy.addEnemy(120,360);
				e2 = u_Enemy.addEnemy(120,160);
				e3 = u_Enemy.addEnemy(120,560);
			}
		} else if (tutstep==6) {
			if (e1.spr==null && e2.spr==null && e3.spr==null) {
				tutstep++;
				tutorial4.setVisible(true);
				close.setVisible(true);
				e1 = e2 = e3 = null;
			}
		} else if (tutstep==7) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				tutstep++;
				u_Projectiles.p.get(u_Projectiles.p.size()-1).forcedestroy = true;
				tutorial4.setVisible(false);
				close.setVisible(false);
				e1 = u_Enemy.addTank(120,560,0);
				Main.tankmusic.pause();
				Main.tankmusic.seekTo(0);
				e1.d=0;
				e1.spr3.setVisible(false);
				e1.timer=5000;
				e1.next=4998;
			}
		} else if (tutstep==8) {
			Log.d("e1",e1.timer+";"+e1.next);
			if (e1.spr==null || tuttimer>0) {
				tuttimer++;
				if (tuttimer>=30) {
					tuttimer=0;
					tutstep++;
					tutorial5.setVisible(true);
					close.setVisible(true);
					e1 = null;
				}
			}
		} else if (tutstep==9) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				tutorial5.setVisible(false);
				tutorial6.setVisible(true);
				tutstep++;
				u_Projectiles.p.get(u_Projectiles.p.size()-1).forcedestroy = true;
			}
		} else if (tutstep==10) {
			if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
				Main.root.setCurrentScene(new s_Game(controller));
			}
		}
		
		OuyaController.startOfFrame();
	}
}
