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 1 */

Blockly.JavaScript['load_fuel'] = function(block) {
  var dropdown_fuel_type = block.getFieldValue('fuel_type');
  // TODO: Assemble JavaScript into code variable.
  var code = 'loadFuel("' + dropdown_fuel_type + '"); \n';
  return code;
};

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

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

Blockly.JavaScript['is_tank_full'] = function(block) {
  var dropdown_fuel_type = block.getFieldValue('fuel_type');
  // TODO: Assemble JavaScript into code variable.
  var code = 'isTankFull("' + dropdown_fuel_type + '")';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};



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

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

Blockly.JavaScript['fix_leak'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var code = 'fixLeak(); \n alert("Les techniciens ont réussi à fixer la fuite, la mission peut reprendre !");\n';
  return code;
};

Blockly.JavaScript['pause_launch'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var code = 'alert("Le décompte de lancement a été mis en pause !");\n';
  return code;
};

Blockly.JavaScript['go_lift'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  var code = 'alert("Les astronautes sont arrivés au 320e étage et vont embarquer !");\n';
  return code;
};

/* ACTIVITE 2 */

Blockly.JavaScript['launch_rocket'] = function(block) {
  var code = 'launchRocket();\n';
  return code;
};

Blockly.JavaScript['engine_power_to'] = function(block) {
  var number_power = block.getFieldValue('power');
  number_power = number_power/100.0;
  var code = 'changePower('+  number_power +', 200);\n';
  return code;
};

Blockly.JavaScript['separate_stage'] = function(block) {
  var code = 'changeRocketStage();\n';
  return code;
};

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

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

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

Blockly.JavaScript['enable_manual_mode'] = function(block) {
  // TODO: Assemble JavaScript into code variable.

  var code = 'enableManual();\n';
  return code;
};



/* ACTIVITE 3 */

Blockly.JavaScript['set_lander_power_to'] = function(block) {
  var number_power = block.getFieldValue('lander_power');
  number_power = number_power/100.0;
  var code = 'changePower('+  number_power +', 200);\n';
  return code;
};

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

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


/* 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_0;");\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 = 'sendString("IO_'+ dropdown_val + '_0;");\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 = 'var s = "RING_" + '+ value_num  +' +";";\n '+ 'sendString(s);\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];
};
