// generator_richshield1.js

ArduinoGenerator.hardwareScanners['rsv1_shield_detected'] = function(block) {
    if (block.type.startsWith('rsv1_')) {
        ArduinoGenerator.includes_.add(`#include <Shield_RichShield1.h>`);
        ArduinoGenerator.globals_.add(`Shield_RichShield1 rsv1;`);
        
        if (!ArduinoGenerator.autoSetup_.includes(`  rsv1.begin();\n`)) {
            ArduinoGenerator.autoSetup_.push(`  rsv1.begin();\n`);
        }
        if (!ArduinoGenerator.autoLoop_.includes(`  rsv1.update();\n`)) {
            ArduinoGenerator.autoLoop_.push(`  rsv1.update();\n`);
        }
    }
};

// --- TASTER ---
ArduinoGenerator.forBlock['rsv1_button_event'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    const event = block.getFieldValue('EVENT');
    const code = `(rsv1.${button}.getEvent() == ${event})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv1_button_counter'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    const code = `rsv1.${button}.getClickCount()`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv1_button_reset'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    return `  rsv1.${button}.resetClickCount();\n`;
};

// --- LEDs ---
ArduinoGenerator.forBlock['rsv1_led_digital'] = function(block) {
    const led = block.getFieldValue('LED');
    const state = block.getFieldValue('STATE');
    return `  rsv1.setLED(${led}, ${state});\n`;
};

ArduinoGenerator.forBlock['rsv1_led_pwm'] = function(block) {
    const led = block.getFieldValue('LED');
    const val = ArduinoGenerator.valueToCode(block, 'VALUE', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv1.setLEDPWM(${led}, map(${val}, 0, 100, 0, 255));\n`;
};

// --- SUMMER ---
ArduinoGenerator.forBlock['rsv1_buzzer_tone'] = function(block) {
    const freq = ArduinoGenerator.valueToCode(block, 'FREQ', ArduinoGenerator.PRECEDENCE) || '440';
    const dur = ArduinoGenerator.valueToCode(block, 'DURATION', ArduinoGenerator.PRECEDENCE) || '500';
    return `  rsv1.playTone(${freq}, ${dur});\n`;
};

// --- SENSOREN ---
ArduinoGenerator.forBlock['rsv1_read_analog'] = function(block) {
    const sensor = block.getFieldValue('SENSOR');
    let code = '0';
    if (sensor === 'POTI') code = 'rsv1.readPoti()';
    else if (sensor === 'LDR') code = 'rsv1.readLDR()';
    else if (sensor === 'NTC') code = 'rsv1.readNTC()';
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv1_read_dht'] = function(block) {
    const type = block.getFieldValue('TYPE');
    const code = (type === 'TEMP') ? 'rsv1.readTemperature()' : 'rsv1.readHumidity()';
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv1_read_voltage'] = function(block) {
    return ['rsv1.readVoltage()', ArduinoGenerator.PRECEDENCE];
};

// --- DISPLAY ---
ArduinoGenerator.forBlock['rsv1_display_number'] = function(block) {
    const val = ArduinoGenerator.valueToCode(block, 'VALUE', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv1.showNumber(${val});\n`;
};

ArduinoGenerator.forBlock['rsv1_display_word'] = function(block) {
    const c1 = block.getFieldValue('C1');
    const c2 = block.getFieldValue('C2');
    const c3 = block.getFieldValue('C3');
    const c4 = block.getFieldValue('C4');
    return `  rsv1.showWord(${c1}, ${c2}, ${c3}, ${c4});\n`;
};

ArduinoGenerator.forBlock['rsv1_display_clear'] = function(block) {
    return `  rsv1.clearDisplay();\n`;
};

// --- INFRAROT ---
ArduinoGenerator.forBlock['rsv1_ir_print'] = function(block) {
    // Sichert ab, dass der Serial Monitor im setup() läuft, falls der Nutzer ihn vergessen hat
    if (!ArduinoGenerator.autoSetup_.includes(`  Serial.begin(9600);\n`)) {
        ArduinoGenerator.autoSetup_.push(`  Serial.begin(9600);\n`);
    }
    return `  rsv1.printIRCode();\n`;
};

ArduinoGenerator.forBlock['rsv1_ir_check'] = function(block) {
    const hexCode = block.getFieldValue('HEXCODE').trim();
    // Falls Schüler das "0x" vergessen, baut der Generator es automatisch ein, um Compilerfehler zu vermeiden
    const formattedHex = (hexCode.toLowerCase().startsWith('0x')) ? hexCode : `0x${hexCode}`;
    const code = `rsv1.checkIRCode(${formattedHex})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

// Alle Blöcke registrieren
const rsv1Blocks = [
    'rsv1_button_event', 'rsv1_button_counter', 'rsv1_button_reset',
    'rsv1_led_digital', 'rsv1_led_pwm', 'rsv1_buzzer_tone',
    'rsv1_read_analog', 'rsv1_read_dht', 'rsv1_read_voltage',
    'rsv1_display_number', 'rsv1_display_word', 'rsv1_display_clear',
    'rsv1_ir_print', 'rsv1_ir_check'
];
rsv1Blocks.forEach(type => {
    ArduinoGenerator.hardwareScanners[type] = ArduinoGenerator.hardwareScanners['rsv1_shield_detected'];
});