'use strict';

goog.provide('Blockly.Python.mainwifi');
goog.require('Blockly.Python');

Blockly.Python['mainwifi_setup'] = function (block) {
    let serial_setup = "Serial.begin(9600);";
    Blockly.Python.addSetup('serial_9600', serial_setup);
    return '';
};

Blockly.Python['mainwifi_sendMsg'] = function (block) {
    var checkbit = "#define SOP '<'\n" +
        "#define EOP '>'\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
    var datavar = Blockly.Python.variableDB_.getName(
        block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
    var length = "sizeof(" + datavar + ")/sizeof(" + datavar + "[0])";
    var dataLength = length + "+2";
    var code = "unsigned char DataOut_[" + dataLength + "]\n" +
        "DataOut_[0]= SOP\n" +
        "DataOut_[" + length + "+1]= EOP\n" +
        "for (int i=0; i<" + length + ";i++) {\n" +
        "  DataOut_[i+1]=" + datavar + "[i]\n" +
        "}\n" +
        "Serial1.write(DataOut_, " + dataLength + ")\n";
    return code;
};

Blockly.Python['mainwifi_receiveMsg'] = function (block) {
    var checkbit = "#define SOP '<'\n" +
        "#define EOP '>'\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
    var MsgReceiveStatus = "bool started = false\n" +
        "bool ended = false\n" +
        "int index_i\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
    var datavar = Blockly.Python.variableDB_.getName(
        block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
    var length = 'sizeof(' + datavar + ')/sizeof(' + datavar + '[0])' || 1;
    var dataIn_function = "int handleDataIn() {\n" +
        "  while (Serial1.available() > 0) {\n" +
        "    int inChar = Serial1.read()\n" +
        "    if(inChar == SOP) {\n" +
        "      index_i = 0\n" +
        "      started = true\n" +
        "      ended = false\n" +
        "    } else if(inChar == EOP) {\n" +
        "      ended = true\n" +
        "      break\n" +
        "    }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
        "      " + datavar + "[index_i] = inChar\n" +
        "      index_i++\n" +
        "    }\n" +
        "  }\n" +
        "  if(started && ended) {\n" +
        "    return 1\n" +
        "  } else {\n" +
        "    return 0\n" +
        "  }\n" +
        "}\n";
    Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
    var code = "boolean receiveMsgsuccess_ = handleDataIn()\n";
    return code;
};

Blockly.Python['mainwifi_receiveMsg_isSuccessed'] = function (block) {
    var code = "receiveMsgsuccess_";
    return [code, Blockly.Python.ORDER_ATOMIC];
};

// easymode

Blockly.Python['MainEasymode_transfer_send'] = function (block) {
    var dataMain = Blockly.Python.variableDB_.getName(
        block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
    Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
    var checkbit = "#define SOP '<'\n" +
        "#define EOP '>'\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
    var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
    var dataLength = length + "+2";
    var code = "unsigned char DataOut_[" + dataLength + "]\n" +
        "DataOut_[0]= SOP\n" +
        "DataOut_[" + length + "+1]= EOP\n" +
        "for (int i=0; i<" + length + ";i++) {\n" +
        "  DataOut_[i+1]=" + dataMain + "[i]\n" +
        "}\n" +
        "Serial1.write(DataOut_, " + dataLength + ")\n";
    return code;
}

Blockly.Python['MainEasymode_transfer_receive'] = function (block) {
    var dataMain = Blockly.Python.variableDB_.getName(
        block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
    Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
    var checkbit = "#define SOP '<'\n" +
        "#define EOP '>'\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
    var MsgReceiveStatus = "bool started = false\n" +
        "bool ended = false\n" +
        "int index_i\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
    var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
    var dataIn_function = "int handleDataIn() {\n" +
        "  while (Serial1.available() > 0) {\n" +
        "    int inChar = Serial1.read()\n" +
        "    if(inChar == SOP) {\n" +
        "      index_i = 0\n" +
        "      started = true\n" +
        "      ended = false\n" +
        "    } else if(inChar == EOP) {\n" +
        "      ended = true\n" +
        "      break\n" +
        "    }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
        "      " + dataMain + "[index_i] = inChar\n" +
        "      index_i++\n" +
        "    }\n" +
        "  }\n" +
        "  if(started && ended) {\n" +
        "    return 1\n" +
        "  } else {\n" +
        "    return 0\n" +
        "  }\n" +
        "}\n";
    Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
    var code = "boolean receiveMsgsuccess_ = handleDataIn()\n";
    return code;
}

/*************************
 *       version 2       *
 *       last version    *
 *************************/

Blockly.Python['MainEasymode_transfer2_send_old'] = function (block) {
    var _ocde = "uart = UART(1, 115200)" +
        "uart.init(115200, bits=8, parity=None, stop=1)" +
        'uart_data_set = str(pa.value()) + "," + str(pb.value())+ "," + str(pl.value())+ "," + str(pm.value())+ "," + str(pr.value())' +
        "uart.write(button_data_set)"


    return Blockly.Python.definitions_.import_camare = "from machine import UART",
        [_ocde, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python['MainEasymode_transfer2_receive_old'] = function (block) {
    var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
    var list_length = block.getFieldValue("LENGTH");
    var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
    for (var i = 1; i < list_length; i++) {
        dataMain_list += ",0";
    }
    dataMain_list += "}\n";
    Blockly.Python.addDeclaration("dataMain_list", dataMain_list);

    Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
    var checkbit = "#define SOP '<'\n" +
        "#define EOP '>'\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
    var MsgReceiveStatus = "bool started = false\n" +
        "bool ended = false\n" +
        "int index_i\n";
    Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
    var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
    var dataIn_function = "int handleDataIn() {\n" +
        "  while (Serial1.available() > 0) {\n" +
        "    int inChar = Serial1.read()\n" +
        "    if(inChar == SOP) {\n" +
        "      index_i = 0\n" +
        "      started = true\n" +
        "      ended = false\n" +
        "    } else if(inChar == EOP) {\n" +
        "      ended = true\n" +
        "      break\n" +
        "    }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
        "      " + dataMain + "[index_i] = inChar\n" +
        "      index_i++\n" +
        "    }\n" +
        "  }\n" +
        "  delay(10)\n" +
        "  if(started && ended) {\n" +
        "    return 1\n" +
        "  } else {\n" +
        "    return 0\n" +
        "  }\n" +
        "}\n";
    Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
    var code = "boolean receiveMsgsuccess_ = handleDataIn()\n" +
        "if (!receiveMsgsuccess_) {\n" +
        "  Serial.println(\"Error: Fail to receive data!\")\n" +
        "  return\n" +
        "}\n";
    return code;
}

Blockly.Python['MainEasymode_transfer2_receive_getValue_old'] = function (block) {
    var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
    var list_length = block.getFieldValue("INDEX");
    var code = "" + dataMain + "[" + list_length + "]";
    return [code, Blockly.Python.ORDER_ATOMIC];
}

/*************************
 *       version 3       *
 *       current         *
 *************************/

Blockly.Python['MainEasymode_transfer2_send'] = function (block) {
    var item_value = '';
    for (var n = 0; n < this.itemCount_; n++) {
        item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
        if (n != this.itemCount_ - 1) {
            item_value += " + ',' + ";
        }
    }
    var _ocde = "uart = UART(1, 115200) \n" +
        "uart.init(115200, bits=8, parity=None, stop=1) \n" +
        'uart_data_set = ' + item_value + " \n" +
        "uart.write(uart_data_set) \n";
    return Blockly.Python.definitions_.import_camare = "from machine import UART",
        [_ocde, Blockly.Python.ORDER_ATOMIC];
}


/*************************
 *       version 3       *
 *       current         *
 *************************/

Blockly.Python['AiEasymode_transfer2_send'] = function (block) {
    var item_value = '';
    for (var n = 0; n < this.itemCount_; n++) {
        item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
        if (n != this.itemCount_ - 1) {
            item_value += " + ',' + ";
        }
    }
    var _ocde = "uart = UART(1, 115200) \n" +
        "uart.init(115200, bits=8, parity=None, stop=1) \n" +
        'uart_data_set = ' + item_value + " \n" +
        "uart.write(uart_data_set) \n";
    return Blockly.Python.definitions_.import_camare = "from machine import UART",
        [_ocde, Blockly.Python.ORDER_ATOMIC];
}
Blockly.Python['MainEasymode_transfer2_receive'] = function (block) {
    var item_value = '';

    var _len =  block.getField('LENGTH').text_;
    //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
    for (var n = 0; n < _len; n++) {
        item_value += "0";
        if (n != _len - 1) {
            item_value += ",";
        }
    }
    var _code = 'uart = UART(1, 115200) \n' +
        'uart.init(115200, bits=8, parity=None, stop=1) \n' +
        "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
        'read_data = uart.read() \n' +
        'if read_data != None: \n' +
        "    read_str = read_data.decode('utf-8') \n" +
        'elif read_data == None: \n' +
        "    read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
    return Blockly.Python.definitions_.import_camare = "from machine import UART",
        [_code, Blockly.Python.ORDER_ATOMIC];
}
Blockly.Python['AiEasymode_transfer2_receive'] = function (block) {
    var item_value = '';

    var _len =  block.getField('LENGTH').text_;
    //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
    for (var n = 0; n < _len; n++) {
        item_value += "0";
        if (n != _len - 1) {
            item_value += ",";
        }
    }
    var _code = 'uart = UART(1, 115200) \n' +
        'uart.init(115200, bits=8, parity=None, stop=1) \n' +
        "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
        'read_data = uart.read() \n' +
        'if read_data != None: \n' +
        "    read_str = read_data.decode('utf-8') \n" +
        'elif read_data == None: \n' +
        "    read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
    return Blockly.Python.definitions_.import_camare = "from machine import UART",
        [_code, Blockly.Python.ORDER_ATOMIC];
}
Blockly.Python['MainEasymode_transfer2_receive_getValue'] = function (block) {
    var index = parseInt(block.getFieldValue("INDEX")) * 2;
    var code = "read_str[" + index + "] \n";
    return [code, Blockly.Python.ORDER_ATOMIC];
}

Blockly.Python['AiEasymode_transfer2_receive_getValue'] = function (block) {
    var index = parseInt(block.getFieldValue("INDEX")) * 2;
    var code = "read_str[" + index + "] \n";
    return [code, Blockly.Python.ORDER_ATOMIC];
}