var isRunning = false;
var gamePadId = 0;
var gamepad = {};

let lastCheckTime = 0;



function checkGamePad() {
			if(isRunning  == false){return;}

			const stepSize = "0.01";
			const stepSizeXYZ = "0.5"; // 3 ist auch ok
			const stepSizeE = "0.01";
			var gp = navigator.getGamepads()[gamePadId]
			var buttons = gp.buttons 

			var xyzSpeed = 10; // 100 geht auch

			var psi = gp.axes[0];
			var z = gp.axes[1];
			var x = gp.axes[2];
			var y = gp.axes[3];


			// Dreieck zum Dreieck-Setzen
			if (buttons[3].pressed) {				
				socketDriver.send(`G90 G1 X0 Y300 Z0 A${Math.PI/2} B${-1.0*Math.PI/2} C0 F${xyzSpeed}`);
			}
			if (buttons[4].pressed) {
				//console.log("x=" + robot.x + "  y=" + robot.y + " z=" + robot.z);
			}


			
			// X Button setzt eine Marke
			if(buttons[0].pressed && (Date.now() - lastCheckTime > 500)){
				lastCheckTime = Date.now()
				console.log('FPoint!');
				socketDriver.send('FPoint');
				socketDriver.send('FShow');
			}

			// L1 und R1 Button to forward-backward in Point-List
			if(gp.buttons[4].pressed && (Date.now() - lastCheckTime > 500)){
				lastCheckTime = Date.now()
				socketDriver.send('FMinus');
				socketDriver.send('FShow');
			}
			if(gp.buttons[5].pressed && (Date.now() - lastCheckTime > 500)){
				lastCheckTime = Date.now()
				socketDriver.send('FPlus');
				socketDriver.send('FShow');
			}
			
			if (x < -0.2) { socketDriver.send(`G91 G1 X+${stepSizeXYZ} F${xyzSpeed}`);}
			if (x > 0.2) { socketDriver.send(`G91 G1 X-${stepSizeXYZ} F${xyzSpeed}`);}

			if (y < -0.2) { socketDriver.send(`G91 G1 Y${stepSizeXYZ} F${xyzSpeed}`); }		
			if (y > 0.2) { socketDriver.send(`G91 G1 Y-${stepSizeXYZ} F${xyzSpeed}`);}

			if (z < -0.2) { socketDriver.send(`G91 G1 Z${stepSizeXYZ} F${xyzSpeed}`); }
            if (z > 0.2) { socketDriver.send(`G91 G1 Z-${stepSizeXYZ} F${xyzSpeed}`); }

			
			// Greif-Richtung
			// LeftRight 
			if(buttons[14].pressed){ socketDriver.send(`G91 G1 A${stepSize} F${xyzSpeed}`);}
			if(buttons[15].pressed){ socketDriver.send(`G91 G1 A-${stepSize} F${xyzSpeed}`);}
			// Up - Down
			if(buttons[12].pressed){ socketDriver.send(`G91 G1 B${stepSize} F${xyzSpeed}`);}
			if(buttons[13].pressed){ socketDriver.send(`G91 G1 B-${stepSize} F${xyzSpeed}`);}
			// Drehung
			if (psi < -0.2) { socketDriver.send(`G91 G1 C${stepSize} F${xyzSpeed}`); }
            if (psi > 0.2) { socketDriver.send(`G91 G1 C-${stepSize} F${xyzSpeed}`); }

			// Trigger-Buttons für Öffnen und Schliessen
			if(buttons[6].pressed){socketDriver.send(`G91 G1 E-${stepSizeE} F${xyzSpeed}`);}
			if(buttons[7].pressed){socketDriver.send(`G91 G1 E${stepSizeE} F${xyzSpeed}`);}

			if (isRunning) { setTimeout(checkGamePad, 15);}
}

function gamepadHandler(event, connecting) {
		
			gamepad = event.gamepad;
			if (typeof gamepad === `undefined`) {
				isRunning = false;
				gamePadId = 0;
				console.warn("GamePad kann nicht gefunden werden");
				return;
            		}

			if (connecting) {
				console.log("GamePad " + event.gamepad.index + " connected");
                		gamePadId = gamepad.index;
				isRunning = true;
				setTimeout(checkGamePad, 20);
			} else {
				console.log("GamePad " +gamePadId +" disconnected");
				isRunning = false;
				gamePadId = 0;
			}
}

		
window.addEventListener("gamepadconnected", function (e) { gamepadHandler(e, true); }, false);
window.addEventListener("gamepaddisconnected", function (e) { gamepadHandler(e, false); }, false);

document.addEventListener("touchstart", e => { console.log("TouchStart"); })