// =======================================================================
// SIMULATION: INDUSTRIEPRESSE
// =======================================================================

// ---------------------------------------------------
// AUSGÄNGE: AKTIONEN
// ---------------------------------------------------
Blockly.defineBlocksWithJsonArray([{
  "type": "sim_presse_motor",
  "message0": "💥 Presse | Förderband Motor %1",
  "args0": [ { "type": "field_dropdown", "name": "STATE", "options": [ ["AN (1)", "1"], ["AUS (0)", "0"] ] } ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

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

Blockly.defineBlocksWithJsonArray([{
  "type": "sim_presse_stempel",
  "message0": "💥 Presse | Stempel fährt %1",
  "args0": [ { "type": "field_dropdown", "name": "STATE", "options": [ ["STOP (0)", "0"], ["ABWÄRTS (1)", "1"], ["AUFWÄRTS (2)", "2"] ] } ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

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

Blockly.defineBlocksWithJsonArray([{
  "type": "sim_presse_zeit",
  "message0": "💥 Presse | Zeige Presszeit %1",
  "args0": [ { "type": "input_value", "name": "TIME", "check": "Number" } ],
  "previousStatement": null, "nextStatement": null, "colour": 20
}]);

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

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

// --- Lichtschranke ---
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_ls", 
    "message0": "💥 Presse | Lichtschranke %1 belegt?", 
    "args0": [ { "type": "field_dropdown", "name": "POS", "options": [ ["Start", "0"], ["Mitte (Presse)", "2"], ["Ende", "1"] ] } ], 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_ls'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block); 
    const pos = block.getFieldValue('POS');
    return [`(sim_in_${pos} == 1)`, 0]; 
};

// --- Endschalter ---
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_es_oben", 
    "message0": "💥 Presse | Endschalter OBEN erreicht?", 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_es_oben'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block); 
    return [`(sim_in_3 == 1)`, 0]; 
};

Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_es_unten", 
    "message0": "💥 Presse | Endschalter UNTEN erreicht?", 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_es_unten'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block); 
    return [`(sim_in_4 == 1)`, 0]; 
};

// --- Not-Aus Taster ---
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_notaus", 
    "message0": "💥 Presse | Not-Aus betätigt?", 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_notaus'] = function(block) { 
    if(typeof addSimCore !== 'undefined') addSimCore(block); 
    return [`(sim_in_5 == 1)`, 0]; 
};

// --- Hardware Zweihandtaster ---
Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_taster_l", 
    "message0": "💥 Presse | Zweihand-Taster LINKS gedrückt? PIN %1", 
    "args0": [ { "type": "field_input", "name": "PIN", "text": "A1" } ], 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_taster_l'] = function(block) { 
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, INPUT_PULLUP);\n`);
    const loopCode = `  sim_out_2 = !digitalRead(${pin});\n`;
    if (!ArduinoGenerator.autoLoop_.includes(loopCode)) {
        ArduinoGenerator.autoLoop_.push(loopCode);
    }
    return [`(!digitalRead(${pin}))`, 0]; 
};

Blockly.defineBlocksWithJsonArray([{ 
    "type": "sim_presse_taster_r", 
    "message0": "💥 Presse | Zweihand-Taster RECHTS gedrückt? PIN %1", 
    "args0": [ { "type": "field_input", "name": "PIN", "text": "A4" } ], 
    "output": "Boolean", 
    "colour": 20 
}]);
ArduinoGenerator.forBlock['sim_presse_taster_r'] = function(block) { 
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.autoSetup_.push(`  pinMode(${pin}, INPUT_PULLUP);\n`);
    const loopCode = `  sim_out_3 = !digitalRead(${pin});\n`;
    if (!ArduinoGenerator.autoLoop_.includes(loopCode)) {
        ArduinoGenerator.autoLoop_.push(loopCode);
    }
    return [`(!digitalRead(${pin}))`, 0]; 
};