// =======================================================================
// SIMULATION: FÖRDERBAND (SOFTWARE & HARDWARE MODUS)
// =======================================================================

// ---------------------------------------------------
// 1. MOTOR (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_motor_softw",
  "message0": "💻 SIM | Förderband Motor PWM %1",
  "args0": [ { "type": "input_value", "name": "SPEED", "check": "Number" } ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_fband_motor_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const speed = ArduinoGenerator.valueToCode(block, 'SPEED', ArduinoGenerator.PRECEDENCE) || '0';
    return `  sim_out_0 = ${speed};\n`;
};

// ---------------------------------------------------
// 2. MOTOR (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_motor_pin",
  "message0": "🔌 PIN | Förderband Motor PWM %1 Pin %2",
  "args0": [
    { "type": "input_value", "name": "SPEED", "check": "Number" },
    { "type": "field_input", "name": "PIN", "text": "9" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_fband_motor_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const speed = ArduinoGenerator.valueToCode(block, 'SPEED', ArduinoGenerator.PRECEDENCE) || '0';
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, OUTPUT);\n`);
    return `  sim_out_0 = ${speed};\n  analogWrite(${pin}, ${speed});\n`;
};

// ---------------------------------------------------
// 3. AMPEL (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_ampel_softw",
  "message0": "💻 SIM | Förderband Ampel %1",
  "args0": [
    { "type": "field_dropdown", "name": "COLOR", "options": [ ["🔴 ROT", "RED"], ["🟢 GRÜN", "GREEN"], ["⚫ AUS", "OFF"] ] }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_fband_ampel_softw'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const color = block.getFieldValue('COLOR');
    if (color === 'RED') { return `  sim_out_1 = 1;\n  sim_out_2 = 0;\n`; } 
    else if (color === 'GREEN') { return `  sim_out_1 = 0;\n  sim_out_2 = 1;\n`; } 
    else { return `  sim_out_1 = 0;\n  sim_out_2 = 0;\n`; }
};

// ---------------------------------------------------
// 4. AMPEL (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_ampel_pin",
  "message0": "🔌 PIN | Förderband Ampel %1 ROT Pin %2 GRÜN Pin %3",
  "args0": [
    { "type": "field_dropdown", "name": "COLOR", "options": [ ["🔴 ROT", "RED"], ["🟢 GRÜN", "GREEN"], ["⚫ AUS", "OFF"] ] },
    { "type": "field_input", "name": "PIN_ROT", "text": "10" },
    { "type": "field_input", "name": "PIN_GRU", "text": "11" }
  ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

ArduinoGenerator.forBlock['sim_fband_ampel_pin'] = function(block) {
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    const color = block.getFieldValue('COLOR');
    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`);
    
    if (color === 'RED') {
        return `  sim_out_1 = 1;\n  sim_out_2 = 0;\n  digitalWrite(${pinRot}, 1);\n  digitalWrite(${pinGru}, 0);\n`;
    } else if (color === 'GREEN') {
        return `  sim_out_1 = 0;\n  sim_out_2 = 1;\n  digitalWrite(${pinRot}, 0);\n  digitalWrite(${pinGru}, 1);\n`;
    } else {
        return `  sim_out_1 = 0;\n  sim_out_2 = 0;\n  digitalWrite(${pinRot}, 0);\n  digitalWrite(${pinGru}, 0);\n`;
    }
};

// ---------------------------------------------------
// 5. TASTER (SOFTWARE)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_taster_softw",
  "message0": "💻 SIM | Bedienpult Taster %1 gedrückt?",
  "args0": [
    { "type": "field_dropdown", "name": "TASTER", "options": [ ["🟢 START", "3"], ["🔴 STOP", "4"] ] }
  ],
  "output": "Boolean",
  "colour": 20
}]);

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

// ---------------------------------------------------
// 6. TASTER (HARDWARE + SIM)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_taster_pin",
  "message0": "🔌 PIN | Bedienpult Taster %1 gedrückt? PIN %2",
  "args0": [
    { "type": "field_dropdown", "name": "TASTER", "options": [ ["🟢 START", "3"], ["🔴 STOP", "4"] ] },
    { "type": "field_input", "name": "PIN", "text": "2" }
  ],
  "output": "Boolean",
  "colour": 20
}]);

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

// ---------------------------------------------------
// 7. LICHTSCHRANKEN & ZÄHLER (Bleiben wie sie sind)
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_ls",
  "message0": "🏭 Lichtschranke %1 belegt?",
  "args0": [
    { "type": "field_dropdown", "name": "SENSOR", "options": [ ["START (LS1)", "0"], ["MITTE (LS2)", "1"], ["ENDE (LS3)", "2"] ] }
  ],
  "output": "Boolean", "colour": 20
}]);
ArduinoGenerator.forBlock['sim_fband_ls'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    return [`(sim_in_${block.getFieldValue('SENSOR')} == 1)`, 0]; 
};

Blockly.defineBlocksWithJsonArray([{
  "type": "sim_fband_counter",
  "message0": "🏭 Pakete auf Palette (Zähler)",
  "output": "Number", "colour": 20
}]);
ArduinoGenerator.forBlock['sim_fband_counter'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block);
    return ['sim_in_a0', 0]; 
};
