// =======================================================================
// PROCESSING: BILDER & MEDIEN
// =======================================================================

Blockly.defineBlocksWithJsonArray([
    {
        "type": "processing_image_load",
        "message0": "Lade Bild %1 Datei %2",
        "args0": [
            {"type": "field_input", "name": "VAR", "text": "meinBild"},
            {"type": "field_input", "name": "FILE", "text": "maschine.png"}
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 70,
        "tooltip": "Laedt ein Bild in den Speicher. Muss in den SETUP-Bereich!"
    },
    {
        "type": "processing_image_show",
        "message0": "Zeige Bild %1 X %2 Y %3 Breite %4 Hoehe %5",
        "args0": [
            {"type": "field_input", "name": "VAR", "text": "meinBild"},
            {"type": "input_value", "name": "X", "check": "Number"},
            {"type": "input_value", "name": "Y", "check": "Number"},
            {"type": "input_value", "name": "W", "check": "Number"},
            {"type": "input_value", "name": "H", "check": "Number"}
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 70,
        "tooltip": "Zeichnet das geladene Bild."
    }
]);

// --- GENERATOREN ---

ProcessingGenerator.forBlock['processing_image_load'] = function(block) {
    const varName = block.getFieldValue('VAR');
    const fileName = block.getFieldValue('FILE');
    
    if (!ProcessingGenerator.globals_) ProcessingGenerator.globals_ = new Set();
    ProcessingGenerator.globals_.add(`PImage ${varName};`);
    
    return `  ${varName} = loadImage("${fileName}");\n`;
};

ProcessingGenerator.forBlock['processing_image_show'] = function(block) {
    const varName = block.getFieldValue('VAR');
    const x = ProcessingGenerator.valueToCode(block, 'X', ProcessingGenerator.ORDER_NONE) || '0';
    const y = ProcessingGenerator.valueToCode(block, 'Y', ProcessingGenerator.ORDER_NONE) || '0';
    const w = ProcessingGenerator.valueToCode(block, 'W', ProcessingGenerator.ORDER_NONE) || '100';
    const h = ProcessingGenerator.valueToCode(block, 'H', ProcessingGenerator.ORDER_NONE) || '100';
    
    return `  image(${varName}, ${x}, ${y}, ${w}, ${h});\n`;
};