/*!
 *   Game Pad Bindings for MelonJS
 *
 **/

 
 var gamepad = new Gamepad();
 
 console.log("Game Pad Loaded");
 
 console.log(gamepad);
 
 gamepad.bind(Gamepad.Event.TICK, function(gamepads) {
    // gamepads were updated (around 60 times a second)
	console.log("Game Pad Tick");
});


 gamepad.bind(Gamepad.Event.CONNECTED, function(device) {
    // a new gamepad connected
	console.log(device);
	
});

gamepad.bind(Gamepad.Event.DISCONNECTED, function(device) {
    // gamepad disconnected
	console.log(device);
});

gamepad.bind(Gamepad.Event.UNSUPPORTED, function(device) {
    // an unsupported gamepad connected (add new mapping)
	console.log(device);
});


 //Simulate Key Presses
 gamepad.bind(Gamepad.Event.BUTTON_DOWN, function(e) {
		
		console.log('Button Pressed! Index: ' + e.gamepad.index + ' Control: ' + e.control + ' Mapping: ' + e.mapping);
		
		switch(e.gamepad.index){
			case "START_FORWARD":
				me.input.triggerKeyEvent(me.input.KEY.SPACE, true); //Start/Jump/Action
			break;
			case "DPAD_LEFT":
				me.input.triggerKeyEvent(me.input.KEY.A, true); //Move Left
			break;
			case "DPAD_RIGHT":
				me.input.triggerKeyEvent(me.input.KEY.D, true); //Move Right
			break;
			case "FACE_1":
				me.input.triggerKeyEvent(me.input.KEY.SPACE, true); //Jump
			break;
			case "FACE_2":
				me.input.triggerKeyEvent(me.input.KEY.S, true); //Cloak
			break;
		}
});

//Simulate Key Release
gamepad.bind(Gamepad.Event.BUTTON_UP, function(e) {
	//e.gamepad.index
		switch(e.gamepad.index){
			case "START_FORWARD":
				me.input.triggerKeyEvent(me.input.KEY.SPACE, false); //Start/Jump/Action
			break;
			case "DPAD_LEFT":
				me.input.triggerKeyEvent(me.input.KEY.A, false); //Move Left
			break;
			case "DPAD_RIGHT":
				me.input.triggerKeyEvent(me.input.KEY.D, false); //Move Right
			break;
			case "FACE_1":
				me.input.triggerKeyEvent(me.input.KEY.SPACE, false); //Jump
			break;
			case "FACE_2":
				me.input.triggerKeyEvent(me.input.KEY.S, false); //Cloak
			break;
		}
});


//Check for Browser Support
 if (!gamepad.init()) {
	//browser does not support controller
	console.log('browser does not support controller');
 }else{
	console.log('Browser good to go!');
 }
 