/* ---------------------
Gamepad Support
----------------------*/

var GamePadHelperEntity = me.ObjectEntity.extend({
	init: function(x,y,settings){
		this.parent(x,y,settings);
		this.alwaysUpdate = true;
		this.isPersistent = true;
		this.updateWhenPaused = true;
		//Gamepad Controls
		this.actionPressed = this.cloakPressed = this.rightPressed = this.leftPressed = false;
		
		//Menu Controls
		this.upPressed = this.downPressed = this.leftPressed = this.rightPressed = this.startPressed = this.selectPressed = this.aPressed = this.bPressed = false;
		
		//this.gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads;
		this.gamepadSupportAvailable = true; //Probably always true in nwjs?
		
		//
		console.log('GamePad Support Available? ' + this.gamepadSupportAvailable);
		
		this.lastKeyPressed;
		
	},
	update: function(){
		//Chrome
		if(this.gamepadSupportAvailable){			
			//var gamepad = navigator.webkitGetGamepads()[0];
			/* Deprecated
			if(navigator.webkitGetGamepads()[0] !== undefined){
				var gamepad = navigator.webkitGetGamepads()[0];
			}else if(navigator.webkitGetGamepads()[1] !== undefined){
				var gamepad = navigator.webkitGetGamepads()[1];
			}	
			*/
			if(navigator.getGamepads()[0] !== undefined){
				var gamepad = navigator.getGamepads()[0];
			}else if(navigator.getGamepads()[1] !== undefined){
				var gamepad = navigator.getGamepads()[1];
			}	
			
			
			if(gamepad !== undefined) {
				//console.log('GamePad Connected');
				//console.log(gamepad);
				//Button Down
				
				//Button Up 
				
				var buttons = gamepad.buttons;

				//Test
				if(buttons){
					//WINDOWS BINDINGS
					//TEST SEPARATELY ON MAC AND LINUX
					
					//this.startPressed = false; 
					//this.actionPressed = false; 
					
					/*
						Need to somehow manually track button up/down
						if on a new screen, we don't want an action to happen if the button was being held down from the previous screen
						
					*/
					
					//if setForGameplay
					if(InputControls.mode == 'game'){
					
						if( (buttons[0]['pressed'] || buttons[3]['pressed'])){ //X
							if(this.actionWasPressed){
								me.input.triggerKeyEvent(me.input.KEY.SPACE, false); //Start/Jump/Action
							}else{
								//console.log('x!');
								me.input.triggerKeyEvent(me.input.KEY.SPACE, true); //Start/Jump/Action
								this.actionPressed = true; 
								this.actionWasPressed = true; 
							}
						}else if(this.actionWasPressed){
							this.actionWasPressed = false;
							
						}
						
						if(buttons[1]['pressed'] || buttons[2]['pressed'] ){ //O
							this.cloakPressed = true;
							me.input.triggerKeyEvent(me.input.KEY.S, true); //Cloak
						}else if(this.cloakPressed){
							this.cloakPressed = false;
							me.input.triggerKeyEvent(me.input.KEY.S, false); //Cloak
						}
						
						if( buttons[1]['pressed'] || buttons[2]['pressed'] ){ //B OR X
							if(this.bPressed){
								me.input.triggerKeyEvent(me.input.KEY.S, false); //Cloak
							}else{
								me.input.triggerKeyEvent(me.input.KEY.S, true); //Cloak
								this.bPressed = true;  
							}
						}else if(this.bPressed){
							this.bPressed = false;
						}
						
						
						if(buttons[15]['pressed']){ //right
							this.rightPressed = true;
							me.input.triggerKeyEvent(me.input.KEY.D, true); //Move Right
						}else if(this.rightPressed){
							this.rightPressed = false;
							me.input.triggerKeyEvent(me.input.KEY.D, false); //Move Right
						}
						
						if(buttons[14]['pressed']){ //left
							this.leftPressed = true;
							me.input.triggerKeyEvent(me.input.KEY.A, true); //Move Left
						}else if(this.leftPressed){
							this.leftPressed = false;
							me.input.triggerKeyEvent(me.input.KEY.A, false); //Move Left
						}
						
						/*
						if( buttons[9] ){ // START (PAUSE)
							me.input.triggerKeyEvent(me.input.KEY.ESC, true); 
							this.startPressed = true; 
							console.log('start!');
						}else if(this.startPressed){
							this.startPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.ESC, false); 
						}
						*/
						
						if( buttons[9]['pressed'] ){ // START (PAUSE)
							if(this.startPressed){
								me.input.triggerKeyEvent(me.input.KEY.ESC, false); 
							}else{
								//console.log('x!');
								me.input.triggerKeyEvent(me.input.KEY.ESC, true); 
								this.startPressed = true; 
							}
						}else if(this.startPressed){
							this.startPressed = false;
						}
					
					}else if(InputControls.mode == 'menu'){
						//left		14
						//right		15
						//up		12
						//down		13
						//start		9
						//select	8
						//a/y		0/3
						//x/b		1/2
						//console.log('menu!');
						/*
						if( buttons[0]){ //A OR START
							me.input.triggerKeyEvent(me.input.KEY.ENTER, true); 
							this.actionPressed = true; 
							
						}else if(this.actionPressed){
							this.actionPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.ENTER, false); 
						} */
						
						if( buttons[0]['pressed'] ){ //X
							if(this.actionWasPressed){
								me.input.triggerKeyEvent(me.input.KEY.ENTER, false); //Start/Jump/Action
							}else{
								//console.log('x!');
								me.input.triggerKeyEvent(me.input.KEY.ENTER, true); //Start/Jump/Action
								this.actionPressed = true; 
								this.actionWasPressed = true; 
							}
						}else if(this.actionWasPressed){
							this.actionWasPressed = false;
							
						}
						
						/*
						if( buttons[1] || buttons[8] ){ //B OR SELECT
							me.input.triggerKeyEvent(me.input.KEY.ESC, true); 
							this.bPressed = true; 
						}else if(this.bPressed){
							this.bPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.ESC, false); 
						}
						*/
						
						if( buttons[1]['pressed'] || buttons[8]['pressed'] ){ //B OR SELECT
							if(this.bPressed){
								me.input.triggerKeyEvent(me.input.KEY.ESC, false); 
							}else{
								me.input.triggerKeyEvent(me.input.KEY.ESC, true); 
								this.bPressed = true;  
							}
						}else if(this.bPressed){
							this.bPressed = false;
						}
						
						
						if( buttons[14]['pressed'] ){ //LEFT
							me.input.triggerKeyEvent(me.input.KEY.LEFT, true); 
							this.leftPressed = true; 
						}else if(this.leftPressed){
							this.leftPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.LEFT, false); 
						}
						
						if( buttons[15]['pressed'] ){ //RIGHT
							me.input.triggerKeyEvent(me.input.KEY.RIGHT, true); 
							this.rightPressed = true; 
						}else if(this.rightPressed){
							this.rightPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.RIGHT, false); 
						}
						
						if( buttons[12]['pressed'] ){ //UP
							me.input.triggerKeyEvent(me.input.KEY.UP, true); 
							this.upPressed = true; 
							//console.log('up!');
						}else if(this.upPressed){
							this.upPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.UP, false); 
						}
						
						if( buttons[13]['pressed'] ){ //DOWN
							me.input.triggerKeyEvent(me.input.KEY.DOWN, true);
							this.downPressed = true; 
							//console.log('dwon!');
						}else if(this.downPressed){
							this.downPressed = false; 
							me.input.triggerKeyEvent(me.input.KEY.DOWN, false);
						}
						
						if( buttons[9]['pressed'] ){ // START (PAUSE)
							if(this.startPressed){
								me.input.triggerKeyEvent(me.input.KEY.ESC, false); 
							}else{
								//console.log('x!');
								me.input.triggerKeyEvent(me.input.KEY.ESC, true); 
								this.startPressed = true; 
							}
						}else if(this.startPressed){
							this.startPressed = false;
						}
						
					}
					//if setForMenu
					
					
					//4 - LB
					//5 - RB
					//9 - Start
					//12 - Up
					//13 - Down
					
					//14 right & 13 left on mac
					//15 right & 14 left on win
					
				}
			
			}else{
				//console.log('GamePad Not Connected');
			}
		}
		this.parent(this);
		return true;
	}
});