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

Blockly.Python.iot_system_run = function (block) {
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
    var _code = "exec(open(" + _path + ").read())\n";
    return _code;
}
Blockly.Python.iot_system_create_file = function (block) {
    var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _code = "def _CREATE_TEXT_FILE(_path):\n"
    _code += "    f = open(_path, 'w')\n"
    _code += "    f.close()\n"
    Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
    _code = "_CREATE_TEXT_FILE(\"" + _path + _filename + "\")\n";
    return _code;
}
Blockly.Python.iot_system_open_file = function (block) {
    var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _code = "f = open(\"" + _path + _filename + "\", 'a')\n";
    return _code;
}

Blockly.Python.iot_system_write_file = function (block) {
    var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
    var _separator = block.getFieldValue('separator');
    var _code = ""
    if (_separator == "newline") {
        _code = "f.write(" + _wpath + " + '\\r\\n')\n";
    } else if (_separator == "comma") {
        _code = "f.write(" + _wpath + " + ',')\n";
    } else if (_separator == "space") {
        _code = "f.write(" + _wpath + " + ' ')\n";
    } else if (_separator == "semicolon") {
        _code = "f.write(" + _wpath + " + ';')\n";
    }
    return _code;
}
Blockly.Python.iot_system_close_file = function (block) {
    var _code = "f.close()\n";
    return _code;
}
Blockly.Python.iot_system_delete_file = function (block) {
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);

    Blockly.Python.definitions_['import os'] = `import os`;
    var _code = "os.remove(" + _path + ")\n";
    return _code;
}

Blockly.Python.iot_system_input_file = function (block) {
    var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
    var _separator = block.getFieldValue('separator');
    Blockly.Python.definitions_['_GET_LIST_FROM_FILE'] = `def _GET_LIST_FROM_FILE(_path, _sep):
    f = open(_path, 'r')
    result = f.read().split(_sep)
    f.close()
    return result
`;
    let _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", '\\r\\n')";
    if (_separator == "newline") {
        _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", '\\r\\n')";
    } else if (_separator == "comma") {
        _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ',')";
    } else if (_separator == "space") {
        _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ' ')";
    } else if (_separator == "semicolon") {
        _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ';')";
    }
    return [_code, Blockly.Python.ORDER_NONE];
}


export default Blockly;