// =======================================================================
// SIMULATION: AUTOMATIKTOR (SPS ZYKLUS & STEPPER)
// =======================================================================

// ---------------------------------------------------
// AUSGANG: NORMALER MOTOR
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_tor_motor",
  "message0": "🚪 Tor Motor %1 mit Speed (PWM) %2",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "DIR",
      "options": [ ["HOCH (Auf)", "UP"], ["RUNTER (Zu)", "DOWN"], ["STOPP", "STOP"] ]
    },
    { "type": "input_value", "name": "SPEED", "check": "Number" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_tor_motor'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const dir = block.getFieldValue('DIR');
    const speed = ArduinoGenerator.valueToCode(block, 'SPEED', ArduinoGenerator.PRECEDENCE) || '255';
    
    if (dir === 'UP') {
        return `  sim_out_0 = ${speed};\n  sim_out_1 = 0;\n`;
    } else if (dir === 'DOWN') {
        return `  sim_out_0 = 0;\n  sim_out_1 = ${speed};\n`;
    } else {
        return `  sim_out_0 = 0;\n  sim_out_1 = 0;\n`;
    }
};

// ---------------------------------------------------
// AUSGANG: STEPPER ZIEL
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_tor_stepper_target",
  "message0": "🚪 Tor Stepper | Gehe zu Schritt %1",
  "args0": [ { "type": "input_value", "name": "TARGET", "check": "Number" } ],
  "previousStatement": null, "nextStatement": null, "colour": 20,
  "tooltip": "Gibt die Zielposition für den Schrittmotor vor (0 - 5000)."
}]);

ArduinoGenerator.forBlock['sim_tor_stepper_target'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const target = ArduinoGenerator.valueToCode(block, 'TARGET', ArduinoGenerator.PRECEDENCE) || '0';
    return `  sim_out_a0 = ${target};\n`;
};

// ---------------------------------------------------
// AUSGANG: HMI TEXT
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_tor_hmi_text",
  "message0": "🚪 HMI Text senden | Zeile 1: %1 Zeile 2: %2",
  "args0": [ { "type": "input_value", "name": "L1", "check": "String" }, { "type": "input_value", "name": "L2", "check": "String" } ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_tor_hmi_text'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const l1 = ArduinoGenerator.valueToCode(block, 'L1', ArduinoGenerator.PRECEDENCE) || '""';
    const l2 = ArduinoGenerator.valueToCode(block, 'L2', ArduinoGenerator.PRECEDENCE) || '""';
    return `  sim_hmi_l1 = String(${l1});\n  sim_hmi_l2 = String(${l2});\n`;
};

// ---------------------------------------------------
// EINGÄNGE: SENSOREN & TASTER
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_stepper_pos", "message0": "🚪 Tor Stepper | Aktueller Schritt", "output": "Number", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_stepper_pos'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['sim_in_a0', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_ls", "message0": "🚪 Lichtschranke belegt?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_ls'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_0 == 1)', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_es_oben", "message0": "🚪 Tor ist ganz OBEN?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_es_oben'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_1 == 1)', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_es_unten", "message0": "🚪 Tor ist ganz UNTEN?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_es_unten'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_2 == 1)', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_btn_hoch", "message0": "🚪 HMI '▲' gedrückt?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_btn_hoch'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_3 == 1)', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_btn_stop", "message0": "🚪 HMI 'STOP' gedrückt?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_btn_stop'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_4 == 1)', 0]; };

Blockly.defineBlocksWithJsonArray([{ "type": "sim_tor_btn_runter", "message0": "🚪 HMI '▼' gedrückt?", "output": "Boolean", "colour": 20 }]);
ArduinoGenerator.forBlock['sim_tor_btn_runter'] = function(block) { if(typeof addSimCore !== 'undefined') addSimCore(block); return ['(sim_in_5 == 1)', 0]; };