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

// --- TASTER ---
ArduinoGenerator.forBlock['ks_button_event'] = function(block) {
    const btn = block.getFieldValue('BUTTON');
    const evt = block.getFieldValue('EVENT');
    const code = `(ks.${btn}.getEvent() == ${evt})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

// --- LEDs & SUMMER ---
ArduinoGenerator.forBlock['ks_single_led'] = function(block) {
    const led = block.getFieldValue('LED');
    const state = block.getFieldValue('STATE');
    return `  ks.setSingleLED(${led}, ${state});\n`;
};

ArduinoGenerator.forBlock['ks_rgb_led'] = function(block) {
    const r = ArduinoGenerator.valueToCode(block, 'R', ArduinoGenerator.PRECEDENCE) || '0';
    const g = ArduinoGenerator.valueToCode(block, 'G', ArduinoGenerator.PRECEDENCE) || '0';
    const b = ArduinoGenerator.valueToCode(block, 'B', ArduinoGenerator.PRECEDENCE) || '0';
    return `  ks.setRGB(${r}, ${g}, ${b});\n`;
};

ArduinoGenerator.forBlock['ks_buzzer'] = function(block) {
    const freq = ArduinoGenerator.valueToCode(block, 'FREQ', ArduinoGenerator.PRECEDENCE) || '440';
    const dur = ArduinoGenerator.valueToCode(block, 'DUR', ArduinoGenerator.PRECEDENCE) || '500';
    return `  ks.playTone(${freq}, ${dur});\n`;
};

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

ArduinoGenerator.forBlock['ks_read_dht'] = function(block) {
    const type = block.getFieldValue('TYPE');
    const code = (type === 'TEMP') ? 'ks.readDHTTemp()' : 'ks.readDHTHum()';
    return [code, ArduinoGenerator.PRECEDENCE];
};

// --- INFRAROT ---
ArduinoGenerator.forBlock['ks_ir_print'] = function(block) {
    if (!ArduinoGenerator.autoSetup_.includes(`  Serial.begin(9600);\n`)) {
        ArduinoGenerator.autoSetup_.push(`  Serial.begin(9600);\n`);
    }
    return `  ks.printIRCode();\n`;
};

ArduinoGenerator.forBlock['ks_ir_check'] = function(block) {
    const hex = block.getFieldValue('HEXCODE').trim();
    const formattedHex = (hex.toLowerCase().startsWith('0x')) ? hex : `0x${hex}`;
    return [`ks.checkIRCode(${formattedHex})`, ArduinoGenerator.PRECEDENCE];
};

const ksBlocks = [
    'ks_button_event', 'ks_single_led', 'ks_rgb_led', 'ks_buzzer',
    'ks_read_analog', 'ks_read_dht', 'ks_ir_print', 'ks_ir_check'
];
ksBlocks.forEach(type => {
    ArduinoGenerator.hardwareScanners[type] = ArduinoGenerator.hardwareScanners['ks_shield_detected'];
});