import { pythonGenerator } from "blockly/python";
// import Blockly from "./python";
const Blockly = { Python: pythonGenerator || { Msg: Object.create(null) } }

Blockly.Python['esp32_main_controller_io_from_digital_pin'] = function (block) {
    var value_name = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);

    var code = '';

    Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
        'from machine import Pin\n' +
        '';
    Blockly.Python.definitions_['import_from_digital_pin_' + value_name] = `
_PIN_IN_${value_name} = Pin(${value_name}, Pin.IN)
`
    code = '_PIN_IN_' + value_name + '.value()'

    return [code, Blockly.Python.ORDER_CONDITIONAL];
};

Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function (block) {
    var high_low = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC).slice(1, 2);
    var value_name = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
        'from machine import Pin\n' +
        '';
    Blockly.Python.definitions_['import_from_digital_pin_' + value_name] = `
_PIN_OUT_${value_name} = Pin(${value_name}, Pin.OUT)
`
    let code = `_PIN_OUT_${value_name}.value(${high_low})\n`;
    return code;
};
Blockly.Python['esp32_main_controller_io_high_low'] = function (block) {
    var dropdown_name = block.getFieldValue('high_low');
    // TODO: Assemble Python into code variable.
    var code = dropdown_name;
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Python['esp32_main_controller_io_from_analog_pin'] = function (block) {
    // var value_name = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
    var value_gpio = block.getFieldValue('gpio');
    // TODO: Assemble Python into code variable.
    //Blockly.Python.addVariable('_PWM_PIN_IN_' + value_name, '_PWM_PIN_IN_' + value_name + '=None', true);

    Blockly.Python.definitions_['import_setup'] = '' +
        'import machine\n' +
        'from machine import Pin, ADC\n\n' +
        '';

    Blockly.Python.definitions_['import_' + value_gpio + '_setup'] = '' +
        '_PWM_PIN_IN_' + value_gpio + ' = ADC(Pin(' + value_gpio + '))\n' +
        '_PWM_PIN_IN_' + value_gpio + '.atten(ADC.ATTN_11DB)\n' +
        '_PWM_PIN_IN_' + value_gpio + '.width(ADC.WIDTH_10BIT)\n' +
        '';

    var code = '_PWM_PIN_IN_' + value_gpio + '.read()';
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_CONDITIONAL];
};

export default Blockly;