// generator_multifunction.js

// Der Hardware-Scanner sorgt dafür, dass die Library nur eingebunden wird, 
// wenn das Multi-Function Shield auch wirklich benutzt wird.
ArduinoGenerator.hardwareScanners['mfs_shield_detected'] = function(block) {
    if (block.type.startsWith('mfs_')) {
        ArduinoGenerator.includes_.add(`#include <LogicKiste_StarterShields.h>`);
        ArduinoGenerator.globals_.add(`Shield_MultiFunction mfs;`);
        
        // Füge begin() ins setup und update() in den loop ein
        if (!ArduinoGenerator.autoSetup_.includes(`  mfs.begin();\n`)) {
            ArduinoGenerator.autoSetup_.push(`  mfs.begin();\n`);
        }
        if (!ArduinoGenerator.autoLoop_.includes(`  mfs.update();\n`)) {
            ArduinoGenerator.autoLoop_.push(`  mfs.update();\n`);
        }
    }
};

// --- TASTER ---
ArduinoGenerator.forBlock['mfs_button_event'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    const event = block.getFieldValue('EVENT');
    // Nutzt unsere getEvent() Funktion vom KK_ButtonHandler
    const code = `(mfs.${button}.getEvent() == ${event})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

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

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

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

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

// --- SUMMER ---
ArduinoGenerator.forBlock['mfs_buzzer_digital'] = function(block) {
    const state = block.getFieldValue('STATE');
    return `  mfs.setBuzzer(${state});\n`;
};

ArduinoGenerator.forBlock['mfs_buzzer_tone'] = function(block) {
    const freq = ArduinoGenerator.valueToCode(block, 'FREQ', ArduinoGenerator.PRECEDENCE) || '440';
    const dur = ArduinoGenerator.valueToCode(block, 'DURATION', ArduinoGenerator.PRECEDENCE) || '1';
    return `  mfs.playTone(${freq}, ${dur});\n`;
};

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

ArduinoGenerator.forBlock['mfs_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 `  mfs.showWord(${c1}, ${c2}, ${c3}, ${c4});\n`;
};

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

// --- POTI & MATHE ---
ArduinoGenerator.forBlock['mfs_read_poti'] = function(block) {
    return [`mfs.readPoti()`, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['mfs_map_simple'] = function(block) {
    const val = ArduinoGenerator.valueToCode(block, 'VALUE', ArduinoGenerator.PRECEDENCE) || '0';
    const code = `map(${val}, 0, 1023, 0, 255)`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

// --- ERWEITERUNGEN ---
ArduinoGenerator.forBlock['mfs_ext_digital'] = function(block) {
    const state = block.getFieldValue('STATE');
    const pin = block.getFieldValue('PIN');
    ArduinoGenerator.usedPinsOutput.add(pin);
    return `  digitalWrite(pin${pin}, ${state});\n`;
};

ArduinoGenerator.forBlock['mfs_ext_servo'] = function(block) {
    const pin = block.getFieldValue('PIN');
    const angle = ArduinoGenerator.valueToCode(block, 'ANGLE', ArduinoGenerator.PRECEDENCE) || '90';
    
    ArduinoGenerator.includes_.add(`#include <Servo.h>`);
    ArduinoGenerator.globals_.add(`Servo servo_${pin};`);
    
    if (!ArduinoGenerator.autoSetup_.includes(`  servo_${pin}.attach(pin${pin});\n`)) {
        ArduinoGenerator.usedPinsOutput.add(pin);
        ArduinoGenerator.autoSetup_.push(`  servo_${pin}.attach(pin${pin});\n`);
    }
    
    return `  servo_${pin}.write(${angle});\n`;
};

// Den Scanner für alle MFS-Blöcke registrieren
const mfsBlocks = [
    'mfs_button_event', 'mfs_button_counter', 'mfs_button_reset',
    'mfs_led_digital', 'mfs_led_pwm', 'mfs_buzzer_digital', 'mfs_buzzer_tone',
    'mfs_display_number', 'mfs_display_word', 'mfs_display_clear',
    'mfs_read_poti', 'mfs_map_simple', 'mfs_ext_digital', 'mfs_ext_servo'
];
mfsBlocks.forEach(type => {
    ArduinoGenerator.hardwareScanners[type] = ArduinoGenerator.hardwareScanners['mfs_shield_detected'];
});