/* globals AFRAME */
AFRAME.registerComponent('auto-detect-controllers', {
    schema: {
        hand: { type: 'string', default: 'right' },
        trackedcontrols: { type: 'boolean', default: false },
        model: { type: 'boolean', default: true },
        singlehand: { type: 'string', default: 'right' }
    },

    init: function () {
        var el = this.el;
        var self = this;
        this.rescheduleCheck = true;
        this.injectedControls = false;
        this.checkControllerType = this.checkControllerType.bind(this);
        this.rescheduleControllerTypeCheck = this.rescheduleControllerTypeCheck.bind(this);
        // allow mock for testing
        this.getGamepads = function () { return navigator.getGamepads && navigator.getGamepads(); };
    },

    update: function () {
        var data = this.data;
        var el = this.el;
    	// TODO
    },

    play: function () {
        this.rescheduleControllerTypeCheck();
    },

    pause: function () {
        this.rescheduleCheck = false;
        if (this.rescheduleCheckTimeout) {
            clearTimeout(this.rescheduleCheckTimeout);
            delete this.rescheduleCheckTimeout;
        }
    },

    injectOculusTouch: function () {
        var component = this.data.trackedcontrols ? 'tracked-controls' : 'oculus-touch-controls';
        if (this.data.trackedcontrols) {
            this.el.setAttribute('tracked-controls', 'id', 'Oculus Touch ' + (this.data.hand === 'left' ? '(Left)' : '(Right)'));
            this.el.setAttribute('tracked-controls', 'controller', '0');
        } else {
            this.el.setAttribute(component, 'model', this.data.model);
	}
        this.el.setAttribute(component, 'hand', this.data.hand); // although tracked-controls doesn't use this yet
        this.injectedControls = true;
    },

    injectVive: function () {
        var component = this.data.trackedcontrols ? 'tracked-controls' : 'vive-controls';
        if (this.data.trackedcontrols) {
            this.el.setAttribute('tracked-controls', 'id', 'OpenVR Gamepad');
            this.el.setAttribute('tracked-controls', 'controller', this.data.hand === 'left' ? '1' : '0');
        } else {
            this.el.setAttribute(component, 'model', this.data.model);
        }
        this.el.setAttribute(component, 'hand', this.data.hand); // although tracked-controls doesn't use this yet
        this.injectedControls = true;
    },

    injectGearVR: function () {
        if (this.data.hand === this.data.singlehand) {
            this.el.setAttribute('gearvr-controls', 'hand', this.data.hand);
        }
        this.injectedControls = true;
    },

    checkControllerType: function () {
        if (this.injectedControls || !this.getGamepads) { return; }

        var gamepads = this.getGamepads();
        if (gamepads) {
            for (var i = 0; i < gamepads.length; i++) {
                var gamepad = gamepads[i];
                if (gamepad) {
                    if (gamepad.id.indexOf('Oculus Touch') === 0) {
                        this.injectOculusTouch();
                        break;
                    }
                    if (gamepad.id.indexOf('OpenVR Gamepad') === 0) {
                        this.injectVive();
                        break;
                    }
                }
            }
        }

        if (AFRAME.utils.gearVRControls && AFRAME.utils.gearVRControls.isControllerPresent()) {
            this.injectGearVR();
        }
    },

    rescheduleControllerTypeCheck: function () {
        this.checkControllerType();

        this.rescheduleCheck = !this.injectedControls;
        if (this.rescheduleCheck) {
            this.rescheduleCheckTimeout = setTimeout(this.rescheduleControllerTypeCheck, 1000);
        }
    }
});

@@ -1305,32 +1305,51 @@
 ouch-controls';%0A
+%09var options = '';%0A
         if (this
@@ -1377,68 +1377,28 @@
 ) %7B%0A
-            this.el.setAttribute('tracked-controls', 'id', '
+%09    options += 'id:
 Ocul
@@ -1460,17 +1460,16 @@
 Right)')
-)
 ;%0A      
@@ -1478,50 +1478,21 @@
     
-this.el.setAttribute('tracked-controls',
+options +=
  '
+;
 cont
@@ -1501,15 +1501,11 @@
 ller
-', '
+:
 0'
-)
 ;%0A  
@@ -1535,48 +1535,29 @@
     
-this.el.setAttribute(component,
+options +=
  'model
-',
+:' +
  thi
@@ -1572,12 +1572,18 @@
 odel
-)
 ;%0A
-%09
+        
 %7D%0A  
@@ -1592,47 +1592,29 @@
     
-this.el.setAttribute(component,
+options +=
  '
+;
 hand
-',
+:' +
  thi
@@ -1616,33 +1616,32 @@
 + this.data.hand
-)
 ; // although tr
@@ -1668,32 +1668,82 @@
 't use this yet%0A
+        this.el.setAttribute(component, options);%0A
         this.inj
@@ -1892,16 +1892,35 @@
 trols';%0A
+%09var options = '';%0A
         
@@ -1956,68 +1956,28 @@
 ) %7B%0A
-            this.el.setAttribute('tracked-controls', 'id', '
+%09    options += 'id:
 Open
@@ -1987,17 +1987,16 @@
 Gamepad'
-)
 ;%0A      
@@ -2005,50 +2005,21 @@
     
-this.el.setAttribute('tracked-controls',
+options +=
  '
+;
 cont
@@ -2024,19 +2024,23 @@
 ntroller
-', 
+:' +  (
 this.dat
@@ -2063,17 +2063,13 @@
 ' ? 
-'1' : '0'
+1 : 0
 );%0A 
@@ -2100,48 +2100,29 @@
     
-this.el.setAttribute(component,
+options +=
  'model
-',
+:' +
  thi
@@ -2133,17 +2133,16 @@
 ta.model
-)
 ;%0A      
@@ -2157,47 +2157,29 @@
     
-this.el.setAttribute(component,
+options +=
  '
+;
 hand
-',
+:' +
  thi
@@ -2189,17 +2189,16 @@
 ata.hand
-)
 ; // alt
@@ -2233,32 +2233,82 @@
 't use this yet%0A
+        this.el.setAttribute(component, options);%0A
         this.inj

set component options all at once