
var width = window.innerWidth;
var height = window.innerHeight;
var PI=Math.PI;
var absoluteBaseSpeed=200;
var baseSpeed=absoluteBaseSpeed;
console.log(width,height);
var tqwidth = (width*0.75)|0
var tqwbase = (((width-tqwidth)/2))
var opponentcount=60;
var opponentpause=10;
var opponentspeedfactor=0.75;
var fatdownloadsize=130;
var score=0;
var fatscore=0;
var fatrisk=false;
var stext;
var fstext;
var fdcs=15;
var game = new Phaser.Game(width, height, Phaser.CANVAS, 'gameContainer', { preload: preload, create: create, update: update, render: render });

function preload() {
    game.load.image('player', 'player.png');
    game.load.image('opponent', 'opponent.png');
    game.load.image('opponentb', 'opponentb.png');
    game.load.image('opponent2', 'opponent2.png');
    game.load.image('opponent2b', 'opponent2b.png');
    game.load.image('background', 'background.png');

}

var player;
var pad;
var opponents=[];
var fatdownload=[];
var bg;
function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);

    game.stage.backgroundColor = '#000000';
	
	//calc 3/4 width;
	
	console.log("TQWBase",tqwbase);
    bg = game.add.tileSprite(tqwbase, 0, tqwidth, height, 'background');
	
    //bg.fixedToCamera = true;

	player = game.add.sprite((width/2)-16,height-200,'player')
	this.game.physics.enable(player, Phaser.Physics.ARCADE);
	player.body.drag.set(0.2);
	player.body.maxVelocity.setTo(400, 400);
	player.body.collideWorldBounds = true;
	game.input.gamepad.start();
	initOpponents();
	
					//falls pad nicht funktioniert mittels GamepadDebug sample schauen welches pad das aktive ist und entsprechend umcoden
    pad = game.input.gamepad.pad1;
	pad.addCallbacks(this, { onConnect: addButtons });
	stext = game.add.text(10, 40, "Packets:0",{
        font: "30px arial",
        fill: "#ff0044",
        align: "center"
    });
	fstext = game.add.text(10, 80, "Fat downloads:0",{
        font: "30px arial",
        fill: "#ff0044",
        align: "center"
    });
}

function initOpponents()
{
	for(var oc=0;oc<opponentcount;oc++)
	{//preseed of opponent cache
		var bx=game.rnd.between(0,3)>1;
		var flubberflargh = game.add.sprite(0,-100,(bx?'opponent':'opponentb'));
		this.game.physics.enable(flubberflargh, Phaser.Physics.ARCADE);
		flubberflargh.body.drag.set(0.2);
		flubberflargh.body.maxVelocity.setTo(400, 400);
		opponents[oc]=flubberflargh;
	}
	for(var fc=0;fc<fatdownloadsize;fc++)
	{//preseed of fatdl cache
		var b=game.rnd.between(0,4)>1;
		var blubberlutsch = game.add.sprite(0,-100-(fdcs*fc),(b?'opponent2':'opponent2b'));
		this.game.physics.enable(blubberlutsch, Phaser.Physics.ARCADE);
		blubberlutsch.body.drag.set(0.2);
		blubberlutsch.body.maxVelocity.setTo(400, 400);
		fatdownload[fc]=blubberlutsch;
	}
}

function addButtons() {

    //  We can't do this until we know that the gamepad has been connected and is started

    buttonA = pad.getButton(Phaser.Gamepad.XBOX360_A);
    buttonB = pad.getButton(Phaser.Gamepad.XBOX360_B);
    buttonX = pad.getButton(Phaser.Gamepad.XBOX360_X);
    buttonY = pad.getButton(Phaser.Gamepad.XBOX360_Y);

    buttonA.onDown.add(onDown, this);
    buttonB.onDown.add(onDown, this);
    buttonX.onDown.add(onDown, this);
    buttonY.onDown.add(onDown, this);

    buttonA.onUp.add(onUp, this);
    buttonB.onUp.add(onUp, this);
    buttonX.onUp.add(onUp, this);
    buttonY.onUp.add(onUp, this);

	
}
var ocb = 0;
function update() {
	
	var baseSpeed=absoluteBaseSpeed+(((score/100)|0)*50)
	bg.tilePosition.y += 10;
	
	stext.setText("Packets:"+score);
	fstext.setText("Fat downloads:"+fatscore);
	if(pad&&pad._rawPad)
	{
		var vx =0;
		//TODO für später, vielleicht feiner abstufen, deadzone(!!!);
		if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.1||pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X) < -0.1)
		{
			var af=0;
			if(pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT))
			{
				af=1.0;
			}
			if(pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.1)
			{
				af= -pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X)
			}
			if(pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X) < -0.1)
			{
				af= -pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X)
			}
			if(player.x>=tqwbase)
			{
				vx=-baseSpeed*af;
			}
		}
		else if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.1||pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X) > 0.1)
		{
						var af=0;
			if(pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT))
			{
				af=1.0;
			}
			if(pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.1)
			{
				af= pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X)
			}
			if(pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X) > 0.1)
			{
				af= pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_X)
			}
			
			
			if(player.x<=(width-tqwbase-32))
			{
				vx=baseSpeed;
			}
		}
		/*
		if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.1||pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_Y) < -0.1)
		{
			vy=-baseSpeed;
		}
		else if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.1||pad.axis(Phaser.Gamepad.XBOX360_STICK_RIGHT_Y) >0.1)
		{
			vy=baseSpeed;
		}
		*/		
		player.body.velocity.x=vx;
		//player.body.velocity.y=vy;
	}
	for(var opcol=0;opcol<opponentcount;opcol++)
	{
		var opn = opponents[opcol];
		if(opn.y>-50&&opn.y<(height-opn.height))
		{
			opn.body.velocity.y=baseSpeed*opponentspeedfactor;
		}
		if(opn.x<tqwbase||opn.x>(width-tqwbase-opn.width))
		{
			opn.body.velocity.x=0;
		}
		if(opn.y>height)
		{
			score++;
			opn.y=-100;
		}
		game.physics.arcade.collide(player, opn, 
		gameover, 
		function(){return true}, this);
	}
	if(ocb%opponentpause==0)
	{
		var nxoidx = (ocb/opponentpause)|0;
		console.log(nxoidx);
		var nxo = opponents[nxoidx]
		
		if(nxo.y<-50||nxo.y>height)
		{
			
			//console.log(ocb);
			nxo.y=-30;
			nxo.x=game.rnd.between(tqwbase,tqwbase+tqwidth-nxo.width);
			console.log(nxo.x);
			nxo.body.velocity.y=baseSpeed*opponentspeedfactor;
			nxo.body.velocity.x=0;
			var fff = game.rnd.between(0,1000);
			if(fff<50)
			{
				nxo.body.velocity.x=fff-50;
			}
			if(fff>950)
			{
				nxo.body.velocity.x=fff-950;
			}
			
		}
		
		//ocb++;
	}
	ocb=(ocb+1)%(opponentcount*opponentpause);
	var fdc = game.rnd.between(0,1000);
	if(fdc>500&&fatdownload[0].y<-50)
	{
		var fdcolumn = game.rnd.between(tqwbase,tqwbase+tqwidth-fatdownload[0].width);
		for(var fc=0;fc<fatdownloadsize;fc++)
		{
			var fds = fatdownload[fc];
			fds.x=fdcolumn
			fds.y=fds.y+70;
			fds.body.velocity.y=baseSpeed*opponentspeedfactor;
		}
	}
	else 
	{
		if(fatdownload[0].y>-50)//fatdownload transferierend
		{
			for(var fcc=0;fcc<fatdownloadsize;fcc++)
			{
				fds = fatdownload[fcc];
				fds.body.velocity.y=baseSpeed*opponentspeedfactor;
				game.physics.arcade.collide(player,fds , 
				gameover, 
				function(){return true}, this);
			}
		}
	}
	if(fatdownload[fatdownloadsize-1].y>height-200 &&!fatrisk&&fatdownload[fatdownloadsize-1].y<height-170)
	{
		var fx = fatdownload[fatdownloadsize-1].x
		if(fx<=(tqwbase+(tqwidth*0.2)|0)&&player.x<fx)
		{
			fatrisk=true;
			fatscore+=9;
		}
		else
		{
			if(fx>=(width-tqwbase-(tqwidth*0.2)|0)&&player.x>fx)
			{
				fatrisk=true;
				fatscore+=9;
			}
		}
	}
	if(fatdownload[fatdownloadsize-1].y>height)
	{
		fatscore++;	
		fatrisk=false;
		for(var fc=0;fc<fatdownloadsize;fc++)
		{
			var fds = fatdownload[fc];
			fds.x=0
			fds.y=-100-(fdcs*fc);
			fds.body.velocity.y=0;
		}
	}
}


function gameover()
{
	score=0;
	fatscore=0;
	for(var p=0;p<opponentcount;p++)
	{
		var o = opponents[p];
		o.exists=false;
		opponents[p]=null;
	};
	for(var q=0;q<fatdownloadsize;q++)
	{
		var r = fatdownload[q];
		r.exists=false;
		fatdownload[q]=null;
	}
	console.log("Ätschebätsche");
	alert("Packet Loss");
	initOpponents();
	player.y=height-200;
	player.body.velocity.y=0;
}
function onDown(button, value) {
	/*
    if (button.buttonCode === Phaser.Gamepad.XBOX360_A)
    {
       //TODO
    }
    else if (button.buttonCode === Phaser.Gamepad.XBOX360_B)
    {
    }*/

}
function onUp(button, value) {

	/*
    if (button.buttonCode === Phaser.Gamepad.XBOX360_A)
    {
       //TODO
    }
    else if (button.buttonCode === Phaser.Gamepad.XBOX360_B)
    {
       
    }*/
}


function render () {

    // game.debug.text(game.time.physicsElapsed, 32, 32);
    // game.debug.body(player);
    // game.debug.bodyInfo(player, 16, 24);

}