
Blockly.defineBlocksWithJsonArray([
    // 1. Block: Motor Fahren
    {
        "type": "l298_shield_motor",
        "message0": "Shield L298 Motor %1 | %2 | Tempo (0-255) %3",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "MOTOR",
                "options": [
                    ["A", "A"],
                    ["B", "B"]
                ]
            },
            {
                "type": "field_dropdown",
                "name": "DIR",
                "options": [
                    ["Fährt Vorwärts", "HIGH"],
                    ["Fährt Rückwärts", "LOW"]
                ]
            },
            {
                "type": "input_value",
                "name": "SPEED",
                "check": "Number"
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 210,
        "tooltip": "Steuert Geschwindigkeit und Richtung von Motor A oder B. Löst automatisch die Bremse."
    },
    
    // 2. Block: Motor Bremsen/Stoppen
    {
        "type": "l298_shield_stop",
        "message0": "Shield L298 Motor %1 | %2",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "MOTOR",
                "options": [
                    ["A", "A"],
                    ["B", "B"]
                ]
            },
            {
                "type": "field_dropdown",
                "name": "MODE",
                "options": [
                    ["Vollbremsung (Blockieren)", "HIGH"],
                    ["Ausrollen (Freilauf)", "LOW"]
                ]
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 210,
        "tooltip": "Stoppt den Motor entweder sofort (Vollbremsung) oder lässt ihn ausrollen."
    },

    // 3. Block: Auto Steuerung
    {
        "type": "l298_shield_robot",
        "message0": "Shield L298 %1 | Tempo (0-255) %2",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "DIR",
                "options": [
                    ["Fährt Vorwärts", "FWD"],
                    ["Fährt Rückwärts", "BWD"],
                    ["Dreht Links", "LEFT"],
                    ["Dreht Rechts", "RIGHT"],
                    ["Stopp (Bremsen)", "STOP"]
                ]
            },
            {
                "type": "input_value",
                "name": "SPEED",
                "check": "Number"
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 210,
        "tooltip": "Steuert beide Motoren gleichzeitig für ein Auto. (Nimmt an: Motor A = Links, Motor B = Rechts)"
    },

    // 4. Block: Stromsensor
    {
        "type": "l298_shield_current",
        "message0": "Shield L298 Strom / Last Motor %1",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "MOTOR",
                "options": [
                    ["A", "A"],
                    ["B", "B"]
                ]
            }
        ],
        "output": "Number",
        "colour": 210,
        "tooltip": "Liest den analogen Wert des Stromsensors (A0 für Motor A, A1 für Motor B). Perfekt zur Blockade-Erkennung."
    }
]);

// ==============================================================================
// 1. MOTOR FAHREN
// ==============================================================================
ArduinoGenerator.hardwareScanners['l298_shield_motor'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    if (motor === 'A') {
        ArduinoGenerator.usedPinsOutput.add('12'); // Dir A
        ArduinoGenerator.usedPinsOutput.add('3');  // PWM A
        ArduinoGenerator.usedPinsOutput.add('9');  // Brake A
    } else {
        ArduinoGenerator.usedPinsOutput.add('13'); // Dir B
        ArduinoGenerator.usedPinsOutput.add('11'); // PWM B
        ArduinoGenerator.usedPinsOutput.add('8');  // Brake B
    }
};

ArduinoGenerator.forBlock['l298_shield_motor'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    const dir = block.getFieldValue('DIR');
    const speed = ArduinoGenerator.valueToCode(block, 'SPEED', 0) || '0';

    let code = "";
    if (motor === 'A') {
        code += `  digitalWrite(pin9, LOW);
`; 
        code += `  digitalWrite(pin12, ${dir});
`;
        code += `  analogWrite(pin3, ${speed});
`;
    } else {
        code += `  digitalWrite(pin8, LOW);
`; 
        code += `  digitalWrite(pin13, ${dir});
`;
        code += `  analogWrite(pin11, ${speed});
`;
    }
    return code;
};

// ==============================================================================
// 2. MOTOR STOPPEN / BREMSEN
// ==============================================================================
ArduinoGenerator.hardwareScanners['l298_shield_stop'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    if (motor === 'A') {
        ArduinoGenerator.usedPinsOutput.add('3');  // PWM A
        ArduinoGenerator.usedPinsOutput.add('9');  // Brake A
    } else {
        ArduinoGenerator.usedPinsOutput.add('11'); // PWM B
        ArduinoGenerator.usedPinsOutput.add('8');  // Brake B
    }
};

ArduinoGenerator.forBlock['l298_shield_stop'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    const mode = block.getFieldValue('MODE'); // HIGH = Bremsen, LOW = Ausrollen
    
    let code = "";
    if (motor === 'A') {
        code += `  digitalWrite(pin9, ${mode});
`;
        code += `  analogWrite(pin3, 0);
`;
    } else {
        code += `  digitalWrite(pin8, ${mode});
`;
        code += `  analogWrite(pin11, 0);
`;
    }
    return code;
};

// ==============================================================================
// 3. ROBOTER AUTO STEUERUNG
// ==============================================================================
ArduinoGenerator.hardwareScanners['l298_shield_robot'] = function(block) {
    // Beide Motoren werden registriert
    ArduinoGenerator.usedPinsOutput.add('12'); // Dir A
    ArduinoGenerator.usedPinsOutput.add('3');  // PWM A
    ArduinoGenerator.usedPinsOutput.add('9');  // Brake A
    
    ArduinoGenerator.usedPinsOutput.add('13'); // Dir B
    ArduinoGenerator.usedPinsOutput.add('11'); // PWM B
    ArduinoGenerator.usedPinsOutput.add('8');  // Brake B
};

ArduinoGenerator.forBlock['l298_shield_robot'] = function(block) {
    const dir = block.getFieldValue('DIR');
    const speed = ArduinoGenerator.valueToCode(block, 'SPEED', 0) || '0';

    let code = "";
    
    if (dir === 'STOP') {
        // Vollbremsung beider Motoren
        code += `  digitalWrite(pin9, HIGH);
`;
        code += `  analogWrite(pin3, 0);
`;
        code += `  digitalWrite(pin8, HIGH);
`;
        code += `  analogWrite(pin11, 0);
`;
    } else {
        // Bremse lösen
        code += `  digitalWrite(pin9, LOW);
`;
        code += `  digitalWrite(pin8, LOW);
`;
        
        if (dir === 'FWD') {
            code += `  digitalWrite(pin12, HIGH);
`; // Motor A Vor
            code += `  digitalWrite(pin13, HIGH);
`; // Motor B Vor
        } else if (dir === 'BWD') {
            code += `  digitalWrite(pin12, LOW);
`;  // Motor A Zurück
            code += `  digitalWrite(pin13, LOW);
`;  // Motor B Zurück
        } else if (dir === 'LEFT') {
            code += `  digitalWrite(pin12, LOW);
`;  // Motor A Zurück
            code += `  digitalWrite(pin13, HIGH);
`; // Motor B Vor
        } else if (dir === 'RIGHT') {
            code += `  digitalWrite(pin12, HIGH);
`; // Motor A Vor
            code += `  digitalWrite(pin13, LOW);
`;  // Motor B Zurück
        }
        
        code += `  analogWrite(pin3, ${speed});
`;
        code += `  analogWrite(pin11, ${speed});
`;
    }
    
    return code;
};

// ==============================================================================
// 4. MOTOR STROMSENSOR
// ==============================================================================
ArduinoGenerator.hardwareScanners['l298_shield_current'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    if (motor === 'A') {
        ArduinoGenerator.usedPinsAnalog.add('A0');
    } else {
        ArduinoGenerator.usedPinsAnalog.add('A1');
    }
};

ArduinoGenerator.forBlock['l298_shield_current'] = function(block) {
    const motor = block.getFieldValue('MOTOR');
    if (motor === 'A') {
        return ['analogRead(pinA0)', ArduinoGenerator.PRECEDENCE];
    } else {
        return ['analogRead(pinA1)', ArduinoGenerator.PRECEDENCE];
    }
};
