// =======================================================================
// SIMULATION: FUSSGÄNGER-BEDARFSAMPEL (SOFTWARE & HARDWARE MODUS)
// =======================================================================

// ---------------------------------------------------
// 1. AUTO AMPEL (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_auto_softw",
  "message0": "💻 SIM | Auto Ampel | ROT %1 | ORANGE %2",
  "args0": [
    { "type": "field_dropdown", "name": "ROT", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_dropdown", "name": "ORANGE", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_auto_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const rot = block.getFieldValue('ROT');
    const orange = block.getFieldValue('ORANGE');
    return `  sim_out_0 = ${rot};\n  sim_out_1 = ${orange};\n`;
};

// ---------------------------------------------------
// 2. AUTO AMPEL (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_auto_pin",
  "message0": "🔌 PIN | Auto Ampel | ROT %1 Pin %2 | ORANGE %3 Pin %4",
  "args0": [
    { "type": "field_dropdown", "name": "ROT", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_input", "name": "PIN_ROT", "text": "10" },
    { "type": "field_dropdown", "name": "ORANGE", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_input", "name": "PIN_ORG", "text": "11" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_auto_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const rot = block.getFieldValue('ROT');
    const orange = block.getFieldValue('ORANGE');
    const pinRot = block.getFieldValue('PIN_ROT');
    const pinOrg = block.getFieldValue('PIN_ORG');
    
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pinRot}, OUTPUT);\n`);
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pinOrg}, OUTPUT);\n`);
    
    return `  sim_out_0 = ${rot};\n  sim_out_1 = ${orange};\n  digitalWrite(${pinRot}, ${rot});\n  digitalWrite(${pinOrg}, ${orange});\n`;
};

// ---------------------------------------------------
// 3. FUSSGÄNGER AMPEL (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_fuss_softw",
  "message0": "💻 SIM | Fußgänger Ampel | ROT %1 | GRÜN %2",
  "args0": [
    { "type": "field_dropdown", "name": "ROT", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_dropdown", "name": "GRÜN", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_fuss_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const rot = block.getFieldValue('ROT');
    const gruen = block.getFieldValue('GRÜN');
    return `  sim_out_2 = ${rot};\n  sim_out_3 = ${gruen};\n`;
};

// ---------------------------------------------------
// 4. FUSSGÄNGER AMPEL (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_fuss_pin",
  "message0": "🔌 PIN | Fußgänger Ampel | ROT %1 Pin %2 | GRÜN %3 Pin %4",
  "args0": [
    { "type": "field_dropdown", "name": "ROT", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_input", "name": "PIN_ROT", "text": "12" },
    { "type": "field_dropdown", "name": "GRÜN", "options": [ ["AUS (0)", "0"], ["AN (1)", "1"] ] },
    { "type": "field_input", "name": "PIN_GRU", "text": "13" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_fuss_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const rot = block.getFieldValue('ROT');
    const gruen = block.getFieldValue('GRÜN');
    const pinRot = block.getFieldValue('PIN_ROT');
    const pinGru = block.getFieldValue('PIN_GRU');
    
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pinRot}, OUTPUT);\n`);
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pinGru}, OUTPUT);\n`);
    
    return `  sim_out_2 = ${rot};\n  sim_out_3 = ${gruen};\n  digitalWrite(${pinRot}, ${rot});\n  digitalWrite(${pinGru}, ${gruen});\n`;
};

// ---------------------------------------------------
// 5. TASTER (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_taster_softw",
  "message0": "💻 SIM | Anforderungs-Taster %1 gedrückt?",
  "args0": [
    { "type": "field_dropdown", "name": "SEITE", "options": [ ["LINKS", "0"], ["RECHTS", "1"] ] }
  ],
  "output": "Boolean",
  "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_taster_softw'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const seite = block.getFieldValue('SEITE');
    return [`(sim_in_${seite} == 1)`, 0]; 
};

// ---------------------------------------------------
// 6. TASTER (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_ampel_taster_pin",
  "message0": "🔌 PIN | Anforderungs-Taster %1 gedrückt? PIN %2",
  "args0": [
    { "type": "field_dropdown", "name": "SEITE", "options": [ ["LINKS", "0"], ["RECHTS", "1"] ] },
    { "type": "field_input", "name": "PIN", "text": "2" }
  ],
  "output": "Boolean",
  "colour": 20
}]);

ArduinoGenerator.forBlock['sim_ampel_taster_pin'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const seite = block.getFieldValue('SEITE');
    const pin = block.getFieldValue('PIN');
    
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, INPUT_PULLUP);\n`);
    
    // Status an die Python GUI übertragen, damit der Taster dort auch leuchtet
    if (seite === '0') {
        const loopCode = `  sim_out_a0 = !digitalRead(${pin});\n`;
        if (!ArduinoGenerator.autoLoop_.includes(loopCode)) {
            ArduinoGenerator.autoLoop_.push(loopCode);
        }
    } else if (seite === '1') {
        const loopCode = `  sim_out_a1 = !digitalRead(${pin});\n`;
        if (!ArduinoGenerator.autoLoop_.includes(loopCode)) {
            ArduinoGenerator.autoLoop_.push(loopCode);
        }
    }
    
    // Gibt TRUE zurück, wenn die Software ODER die Hardware gedrückt ist
    return [`((sim_in_${seite} == 1) || (!digitalRead(${pin})))`, 0]; 
};