// =======================================================================
// SIMULATION: WC ABLUFT & LICHT
// =======================================================================

// ---------------------------------------------------
// 1. LICHT (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_wc_licht_softw",
  "message0": "💻 SIM | WC Licht %1",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "STATE",
      "options": [ ["AN (1)", "1"], ["AUS (0)", "0"] ]
    }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_wc_licht_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const state = block.getFieldValue('STATE');
    return `  sim_out_0 = ${state};\n`;
};

// ---------------------------------------------------
// 2. LICHT (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_wc_licht_pin",
  "message0": "🔌 PIN | WC Licht %1 Pin %2",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "STATE",
      "options": [ ["AN (1)", "1"], ["AUS (0)", "0"] ]
    },
    { "type": "field_input", "name": "PIN", "text": "10" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_wc_licht_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const state = block.getFieldValue('STATE');
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, OUTPUT);\n`);
    return `  sim_out_0 = ${state};\n  digitalWrite(${pin}, ${state});\n`;
};

// ---------------------------------------------------
// 3. LÜFTER (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_wc_luefter_softw",
  "message0": "💻 SIM | WC Lüfter %1",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "STATE",
      "options": [ ["AN (1)", "1"], ["AUS (0)", "0"] ]
    }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_wc_luefter_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const state = block.getFieldValue('STATE');
    return `  sim_out_1 = ${state};\n`;
};

// ---------------------------------------------------
// 4. LÜFTER (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_wc_luefter_pin",
  "message0": "🔌 PIN | WC Lüfter %1 Pin %2",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "STATE",
      "options": [ ["AN (1)", "1"], ["AUS (0)", "0"] ]
    },
    { "type": "field_input", "name": "PIN", "text": "11" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_wc_luefter_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const state = block.getFieldValue('STATE');
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, OUTPUT);\n`);
    return `  sim_out_1 = ${state};\n  digitalWrite(${pin}, ${state});\n`;
};

// ---------------------------------------------------
// 5. SCHALTER (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_wc_schalter_softw", 
    "message0": "💻 SIM | Lichtschalter ist AN?", 
    "output": "Boolean", 
    "colour": 20 
}]);

ArduinoGenerator.forBlock['sim_wc_schalter_softw'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    return ['(sim_in_0 == 1)', 0]; 
};

// ---------------------------------------------------
// 6. SCHALTER (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_wc_schalter_pin", 
    "message0": "🔌 PIN | Lichtschalter ist AN? PIN %1",
    "args0": [
        { "type": "field_input", "name": "PIN", "text": "2" }
    ],
    "output": "Boolean", 
    "colour": 20 
}]);

ArduinoGenerator.forBlock['sim_wc_schalter_pin'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, INPUT_PULLUP);\n`);
    
    const loopCode = `  sim_out_a0 = !digitalRead(${pin});\n`;
    if (!ArduinoGenerator.autoLoop_.includes(loopCode)) {
        ArduinoGenerator.autoLoop_.push(loopCode);
    }
    
    return [`((sim_in_0 == 1) || (!digitalRead(${pin})))`, 0]; 
};

// ---------------------------------------------------
// 7. HMI TEXT (DISPLAYS)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_wc_hmi_text",
  "message0": "🚽 Anzeige senden | TON: %1 TOF: %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_wc_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`;
};