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

Blockly.Python = pythonGenerator || { Msg: Object.create(null) };
const color = '#22398e';

Blockly.Blocks['system_poweroff'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.system_poweroff);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
};
Blockly.Python['system_poweroff'] = function (block) {
    return 'os.system("poweroff")\n';
};

Blockly.Blocks['system_reboot'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.system_restart);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
};
Blockly.Python['system_reboot'] = function (block) {
    return 'os.system("reboot")\n';
};

Blockly.Blocks['system_get_cpu_temperature'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('系统重启');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
};
Blockly.Python['system_get_cpu_temperature'] = function (block) {
    return 'os.system("reboot")\n';
};

// 设置线程1
Blockly.Blocks['system_set_thread1'] = {
    init: function () {
        this.appendDummyInput()
            .appendField('设置线程')
            .appendField(new Blockly.FieldDropdown([
                ['1', '1'],
                ['2', '2'],
                ['3', '3'],
                ['4', '4'],
                ['5', '5']
            ]), 'THREAD_COUNT');
        this.appendStatementInput("input")
            .setCheck(null)
            .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
};

Blockly.Python['system_set_thread1'] = function (block) {
    var dropdown_thread_count = block.getFieldValue('THREAD_COUNT');
    var value_input = Blockly.Python.statementToCode(block, 'input');
    Blockly.Python.definitions_['import_threading'] = `import threading`;
    let allBlocks = block.workspace.getAllBlocks();
    let global = ""
    try {
        global = allBlocks[0].workspace.variableList.toString()
    }
    catch (e) {
        console.log(e)
    }
    Blockly.Python.definitions_['system_set_thread' + dropdown_thread_count] = `def system_set_thread${dropdown_thread_count}():
  global ${global}
${value_input}

Thread${dropdown_thread_count} = threading.Thread(target=system_set_thread${dropdown_thread_count})
`;
    var code = '';
    return code;
}

Blockly.Blocks["system_start_thread"] = {
    init: function () {
        this.appendDummyInput()
            .appendField('启动线程')
            .appendField(new Blockly.FieldDropdown([
                ['1', '1'],
                ['2', '2'],
                ['3', '3'],
                ['4', '4'],
                ['5', '5']
            ]), 'THREAD_COUNT');
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
};

Blockly.Python['system_start_thread'] = function (block) {
    var dropdown_thread_count = block.getFieldValue('THREAD_COUNT');
    Blockly.Python.definitions_['import_threading'] = `import threading`;
    var code = `Thread${dropdown_thread_count}.start()\n`;
    return code;
}

export default Blockly;