
var width = window.innerWidth;
var height = window.innerHeight;
var PI=Math.PI;
var baseSpeed=200;
console.log(width,height);
var tqwidth = (width*0.75)|0
var tqwbase = (((width-tqwidth)/2))
var opponentcount=60;
var opponentpause=10;
var opponentspeed=150;
var fatdownloadsize=200;
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('opponent2', 'opponent2.png');
    game.load.image('background', 'background.png');

}

var player;
var pad;
var opponents=[];
var fatdownload=[];
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();
	for(var oc=0;oc<opponentcount;oc++)
	{//preseed of opponent cache
		var flubberflargh = game.add.sprite(0,-100,'opponent');
		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 opponent cache
		var blubberlutsch = game.add.sprite(0,-100-(10*fc),'opponent2');
		this.game.physics.enable(blubberlutsch, Phaser.Physics.ARCADE);
		blubberlutsch.body.drag.set(0.2);
		blubberlutsch.body.maxVelocity.setTo(400, 400);
		fatdownload[fc]=blubberlutsch;
	}
	
					//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 });
}
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() {
	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)
		{
			if(player.x>=tqwbase)
			{
				vx=-baseSpeed;
			}
		}
		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)
		{
			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++)
	{
		game.physics.arcade.collide(player, opponents[opcol], 
		gameover, 
		function(){return true}, this);
	}
	if(ocb%opponentpause==0)
	{
		var nxo = opponents[(ocb/opponentpause)|0]
		//console.log(nxo.y);
		if(nxo.y<-50||nxo.y>height)
		{
			//console.log(ocb);
			nxo.y=-30;
			nxo.x=game.rnd.between(tqwbase,tqwbase+tqwidth-nxo.width);
			nxo.body.velocity.y=opponentspeed;
			nxo.body.velocity.x=0;
			//var fff = game.rnd.between(0,1000);
			
		}
		
		//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=opponentspeed;
		}
	}
	else //fatdownload transferierend
	{
		for(var fcc=0;fcc<opponentcount;fcc++)
		{
			game.physics.arcade.collide(player, fatdownload[fcc], 
			gameover, 
			function(){return true}, this);
		}
	}
	if(fatdownload[fatdownloadsize-1].y>height)
	{
		for(var fc=0;fc<fatdownloadsize;fc++)
		{
			var fds = fatdownload[fc];
			fds.x=0
			fds.y=-100-(10*fc);
			fds.body.velocity.y=0;
		}
	}
}


function gameover()
{
	console.log("Ätschebätsche");
	alert("Packet Loss");
}
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);

}