import * as SDFText from './sdftext';
import Props from './props';
import viewer from './viewer';

const [leftController, rightController] = viewer.controllers;

const textCreator = SDFText.creator();

const rhand = Props.controller.clone();
const lhand = Props.controller.clone();
const rText = textCreator.create('');
const lText = textCreator.create('');
rhand.add(rText);
lhand.add(lText);

let leftPress;
let rightPress;

leftController.addEventListener('thumbpaddown', () => {
  if (leftPress) leftPress();
});

rightController.addEventListener('thumbpaddown', () => {
  if (rightPress) rightPress();
});

export default {
  update({ left, right } = {}) {
    if (left) {
      lText.updateLabel(left.text);
      leftPress = left.onPress;
    } else {
      lText.updateLabel('');
      leftPress = null;
    }

    if (right) {
      rText.updateLabel(right.text);
      rightPress = right.onPress;
    } else {
      rText.updateLabel('');
      rightPress = null;
    }
  },

  add() {
    leftController.add(lhand);
    rightController.add(rhand);
  },

  remove() {
    rightPress = leftPress = null;
    leftController.remove(lhand);
    rightController.remove(rhand);
  },
};

@@ -720,24 +720,145 @@
  leftPress =
+ () =%3E %7B%0A        if (left.removeOnPress) %7B%0A          lText.updateLabel('');%0A          leftPress = null;%0A        %7D%0A       
  left.onPres
@@ -850,32 +850,43 @@
     left.onPress
+();%0A      %7D
 ;%0A    %7D else %7B%0A 
@@ -1016,16 +1016,139 @@
 tPress =
+ () =%3E %7B%0A        if (right.removeOnPress) %7B%0A          rText.updateLabel('');%0A          rightPress = null;%0A        %7D%0A       
  right.o
@@ -1153,16 +1153,27 @@
 .onPress
+();%0A      %7D
 ;%0A    %7D 

implement removeOnPress for controllers which removes text and onPress when the button is pressed