Blockly.defineBlocksWithJsonArray([
    // TEXT EINGABE
    {
        "type": "var_text_literal",
        "message0": "\" %1 \"",
        "args0": [
            {
                "type": "field_input",
                "name": "TEXT",
                "text": ""
            }
        ],
        "output": "String",
        "colour": 160,
        "tooltip": "Ein einfacher Textbaustein. Schreibe hier deinen Text rein."
    },
    
    // ZAHLEN EINGABE
    {
        "type": "var_number_literal",
        "message0": "%1",
        "args0": [
            {
                "type": "field_number",
                "name": "NUM",
                "value": 0
            }
        ],
        "output": "Number",
        "colour": 230,
        "tooltip": "Ein einfacher Zahlenwert."
    },

    // ERZWUNGENES LEERZEICHEN
    {
        "type": "var_space_literal",
        "message0": "␣ Leerzeichen",
        "output": "String",
        "colour": 160,
        "tooltip": "Fügt ein einzelnes, unsichtbares Leerzeichen ein."
    },
    
    // TEXT VERKNUEPFEN (JOIN) 2-FACH
    {
        "type": "ard_text_join",
        "message0": "Verbinde %1",
        "args0": [{"type": "input_value", "name": "TEXT1"}],
        "message1": "+ %1",
        "args1": [{"type": "input_value", "name": "TEXT2"}],
        "inputsInline": false,
        "output": "String",
        "colour": 160,
        "tooltip": "Verbindet 2 Werte zu einem einzigen Text."
    },

    // TEXT VERKNUEPFEN (JOIN) 3-FACH
    {
        "type": "ard_text_join_3",
        "message0": "Verbinde %1",
        "args0": [{"type": "input_value", "name": "TEXT1"}],
        "message1": "+ %1",
        "args1": [{"type": "input_value", "name": "TEXT2"}],
        "message2": "+ %1",
        "args2": [{"type": "input_value", "name": "TEXT3"}],
        "inputsInline": false,
        "output": "String",
        "colour": 160,
        "tooltip": "Verbindet 3 Werte zu einem einzigen Text."
    },

    // TEXT VERKNUEPFEN (JOIN) 4-FACH
    {
        "type": "ard_text_join_4",
        "message0": "Verbinde %1",
        "args0": [{"type": "input_value", "name": "TEXT1"}],
        "message1": "+ %1",
        "args1": [{"type": "input_value", "name": "TEXT2"}],
        "message2": "+ %1",
        "args2": [{"type": "input_value", "name": "TEXT3"}],
        "message3": "+ %1",
        "args3": [{"type": "input_value", "name": "TEXT4"}],
        "inputsInline": false,
        "output": "String",
        "colour": 160,
        "tooltip": "Verbindet 4 Werte zu einem einzigen Text."
    }
]);


// =======================================================================
// C++ GENERATOREN
// =======================================================================

ArduinoGenerator.forBlock['var_text_literal'] = function(block) {
    const textValue = block.getFieldValue('TEXT');
    const code = '"' + textValue + '"'; 
    return [code, 0];
};

ArduinoGenerator.forBlock['var_number_literal'] = function(block) {
    const numberValue = block.getFieldValue('NUM');
    return [String(numberValue), 0];
};

ArduinoGenerator.forBlock['var_space_literal'] = function(block) {
    return ['" "', 0];
};

ArduinoGenerator.forBlock['ard_text_join'] = function(block) {
    const text1 = ArduinoGenerator.valueToCode(block, 'TEXT1', 0) || '""';
    const text2 = ArduinoGenerator.valueToCode(block, 'TEXT2', 0) || '""';
    
    const code = 'String(' + text1 + ') + String(' + text2 + ')';
    return [code, 0];
};

ArduinoGenerator.forBlock['ard_text_join_3'] = function(block) {
    const text1 = ArduinoGenerator.valueToCode(block, 'TEXT1', 0) || '""';
    const text2 = ArduinoGenerator.valueToCode(block, 'TEXT2', 0) || '""';
    const text3 = ArduinoGenerator.valueToCode(block, 'TEXT3', 0) || '""';
    
    const code = 'String(' + text1 + ') + String(' + text2 + ') + String(' + text3 + ')';
    return [code, 0];
};

ArduinoGenerator.forBlock['ard_text_join_4'] = function(block) {
    const text1 = ArduinoGenerator.valueToCode(block, 'TEXT1', 0) || '""';
    const text2 = ArduinoGenerator.valueToCode(block, 'TEXT2', 0) || '""';
    const text3 = ArduinoGenerator.valueToCode(block, 'TEXT3', 0) || '""';
    const text4 = ArduinoGenerator.valueToCode(block, 'TEXT4', 0) || '""';
    
    const code = 'String(' + text1 + ') + String(' + text2 + ') + String(' + text3 + ') + String(' + text4 + ')';
    return [code, 0];
};