Blockly.JavaScript['sendtoarduino'] = function(block) {
  var value_message = Blockly.JavaScript.valueToCode(block, 'MESSAGE', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'sendString("' + value_message + ';"); \n';
  return code;
};

Blockly.JavaScript['turn_led'] = function(block) {
  var dropdown_val = block.getFieldValue('val');
  // TODO: Assemble JavaScript into code variable.
  var code = 'sendString("LED_'+ dropdown_val + ';");\n';
  return code;
};

Blockly.JavaScript['arduino_wait'] = function(block) {
  var value_time = Blockly.JavaScript.valueToCode(block, 'time', Blockly.JavaScript.ORDER_ATOMIC);
  let time_ms = Math.round(value_time*1000);
  var code = 'sendString("DELAY_'+time_ms+';");\n waitForUnlockFromArduino();\n';
  return code;
};


Blockly.JavaScript['turn_io'] = function(block) {
  var value_io = Blockly.JavaScript.valueToCode(block, 'IO', Blockly.JavaScript.ORDER_ATOMIC);
  var value_val = Blockly.JavaScript.valueToCode(block, 'VAL', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'sendString("IO_'+ value_io + '_' + value_val +  ';");\n';
  return code;
};





/* ACTIVITE 4 */

Blockly.JavaScript['activate_camera'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var code = 'alert("Impossible de démarrer la diffusion ! Quelque chose ne va pas ! Le SAS est ouvert ? La caméra est en place ? Le signal est envoyé vers la Terre ?");';
  if (hatch_opened && moon_signal_activated && camera_setup) {
    code = ' sendString("IO_3_1;");\n displayConsignes("desc_2"); \n';
  }
  return code;
};

Blockly.JavaScript['setup_camera_module'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  camera_setup = true;
  var code = 'camera_setup = true;\n';
  return code;
};

Blockly.JavaScript['open_hatch'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  hatch_opened = true;
  var code = 'hatch_opened = true;\n';
  return code;
};


Blockly.JavaScript['activate_moon_signal'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  moon_signal_activated = true;
  var code = 'moon_signal_activated = true;\n';
  return code;
};


Blockly.JavaScript['establish_telecoms'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var dropdown_val = block.getFieldValue('val');
  var code = 'alert('+ dropdown_val + ');\n';
  return code;
};


Blockly.JavaScript['call_phone'] = function(block) {
  var value_num = Blockly.JavaScript.valueToCode(block, 'NUM', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'sendString("RING_'+ value_num + ';");\n';
  return code;
};

Blockly.JavaScript['count_letter_in_string'] = function(block) {
  var text_letter = block.getFieldValue('letter');
  var value_text = Blockly.JavaScript.valueToCode(block, 'text', Blockly.JavaScript.ORDER_ATOMIC);
  var count = value_text.match(new RegExp(text_letter, "g")).length;
  var code = count;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ADDITION];
};

Blockly.JavaScript['is_telecom_working'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var code = false;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};
