import * as Blockly from 'blockly';
import { pythonGenerator } from "blockly/python";

Blockly.Python = pythonGenerator || { Msg: Object.create(null) }


Blockly.Python['serial_comm_print'] = function (block) {
    var value_name = Blockly.Python.valueToCode(block, 'serial_comm_input', Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = 'print(' + value_name + ')\n';
    return code;
};

Blockly.Python['serial_write_data'] = function (block) {
    // var api = block.getFieldValue('t_api');
    var number_uart_bps = block.getFieldValue('uart_bps');
    var ai_uart_data = Blockly.Python.valueToCode(this, "ADD1", Blockly.Python.ORDER_NONE);
    Blockly.Python.definitions_['v831_import_serial'] = `import serial
SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
    `
    var code = `uart_data = bytes(str(${ai_uart_data})+"\\n","utf-8")
SERIAL.write(uart_data)
`
    return code;
};
Blockly.Python['serial_write_data_coco'] = function (block) {
    // var api = block.getFieldValue('t_api');
    var number_uart_bps = block.getFieldValue('uart_bps');
    var item_value = '';
    var ai_uart_data = "";
    for (var n = 0; n < this.itemCount_; n++) {
        item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
        ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
    }
    var ai_uart_data_final = ai_uart_data + "";
    Blockly.Python.definitions_['v831_import_serial'] = `import serial
SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
    `

    var code = `uart_data = bytes("SOF|"+${ai_uart_data_final.slice(0, -8)}+"|\\r\\n","utf-8")
SERIAL.write(uart_data)
`
    return code;
};
Blockly.Python['serial_read_data_setup'] = function (block) {
    var number_uart_bps = block.getFieldValue('uart_bps');
    // TODO: Assemble Python into code variable.
    Blockly.Python.definitions_['v831_import_serial'] = `import serial
SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
`;
    Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
    if read_data != None:
        read_str = ""
        try:
            read_str = str(read_data.decode("utf-8")).split(split)[index]
        except:
            read_str = str(read_data).split(split)[index]
        return read_str
`
    var code = "";
    return code;
};

Blockly.Python['serial_read_data_all'] = function (block) {
    var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
    return [code, Blockly.Python.ORDER_NONE];
};


Blockly.Python['serial_read_data'] = function (block) {
    var index = parseInt(block.getFieldValue("INDEX")) + 1;
    // var type = block.getFieldValue("TYPE")
    // var type_1 = block.getFieldValue("TYPE_1")
    Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
    if read_data != None:
        read_str = ""
        try:
            read_str = str(read_data.decode("utf-8")).split(split)[index]
        except:
            read_str = str(read_data).split(split)[index]
        return read_str
`
    var code = `_read_serial_data(SERIAL.readline().decode("UTF-8","ignore").strip(),"|",${index})`;

    return [code, Blockly.Python.ORDER_NONE];
};
Blockly.Python['serial_send_data_to_microbit'] = function (block) {
    var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
    // TODO: Assemble Python into code variable.

    Blockly.Python.definitions_["831_import_serial"] = `import serial
SERIAL = serial.Serial("/dev/ttyS1",115200)`
    var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
SERIAL.write(uart_data)
`;
    return code

};
Blockly.Python['serial_send_data_to_arduino'] = function (block) {
    var number_uart_bps = block.getFieldValue('uart_bps');
    var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
    // TODO: Assemble Python into code variable.
    Blockly.Python.definitions_["v831_import_os"] = `import os`
    Blockly.Python.definitions_["v831_import_serial_ardino_read"] = `import serial
SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})`
    var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
SERIAL.write(uart_data)
`;
    return code
};

Blockly.Python['serial_send_data_to_control_panel'] = function (block) {
    var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
    // TODO: Assemble Python into code variable.
    Blockly.Python.definitions_["831_import_serial"] = `import serial
SERIAL = serial.Serial("/dev/ttyS1",9600)`
    var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
SERIAL.write(uart_data)
`;
    return code;
};

Blockly.Python['serial_read_data_clear'] = function (block) {
    // TODO: Assemble Python into code variable.
    var code = "SERIAL.flushInput()\n";
    return code;
};
export default Blockly;