// 'use strict';

// goog.provide('Blockly.Python.easymode');
// goog.require('Blockly.Python');

// Blockly.Python['wifiEasymode_network'] = function(block) {
//     var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
//     var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
//     Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
//     Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
//     var ssid = block.getFieldValue("wifi_ssid");
//     var pw = block.getFieldValue("wifi_pw");

//     var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti\n";
//     Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
//     var code = "  delay(10)\n" +
//         // Start by connecting to a WiFi network
//         "  WiFiMulti.addAP(\"" + ssid + "\",\"" + pw + "\")\n" +
//         "  Serial.print(\"Wait for WiFi... \")\n" +
//         "  while(WiFiMulti.run() != WL_CONNECTED) {\n" +
//         "    Serial.print(\".\")\n" +
//         "    delay(500)\n" +
//         "  }\n" +
//         "  Serial.println(\"\")\n" +
//         "  Serial.println(\"WiFi connected\");";
//     Blockly.Python.addSetup("wifinetwork_connect", code);
//     return '';
// };

// Blockly.Python['wifiEasymode_thingspeak'] = function(block) {
//     var key = block.getFieldValue("API_KEY");
//     var item_field = '',
//         item_value = '';
//     var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + key;
//     for (var n = 0; n < this.itemCount_; n++) {
//         item_field = "field" + (n + 1);
//         item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
//             Blockly.Python.ORDER_NONE) || '';
//         thingspeak_url += '&' + item_field + '=\"+String(' + item_value + ')+\"';
//     }
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
//     Blockly.Python.addDeclaration("httpclient", "HTTPClient http;");
//     var code = "http.begin(\"" + thingspeak_url + "\")\n" +
//         "http.GET()\n" +
//         "http.end()\n";
//     return code;
// };

// Blockly.Python['wifiEasymode_cococloud_post'] = function(block) {
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
//     var HTTPResponse = "String http_response;";
//     Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     let serial_setup = "Serial.begin(9600);";
//     Blockly.Python.addSetup('serial_serial', serial_setup);
//     var httpclient = "HTTPClient http;";
//     Blockly.Python.addDeclaration("httpclient", httpclient);
//     Blockly.Python.addDeclaration("payload", "String payload;");
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);
//     var precode = "char JSONmessageBuffer[240]\n";
//     Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);

//     var code = "";
//     var item_url = block.getFieldValue("event0");
//     var item_data = Blockly.Python.valueToCode(this, 'dataA',
//         Blockly.Python.ORDER_NONE) || '';
//     var code = "StaticJsonBuffer<240> JSONbuffer_easymode\n" +
//         "JsonObject& Json_easymode = JSONbuffer_easymode.createObject()\n" +
//         "Json_easymode[\"EM_data\"]=" + item_data + "\n" +
//         "Json_easymode.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
//         "http.begin(\"" + item_url + "\")\n" +
//         "http.addHeader(\"Content-Type\", \"application/json\")\n" +
//         "httpCode = http.POST(JSONmessageBuffer)\n" +
//         "payload = http.getString()\n" +
//         "Serial.println(httpCode)\n" +
//         "Serial.println(payload)\n" +
//         "http.end()\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_cococloud_get'] = function(block) {
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
//     var HTTPResponse = "String http_response;";
//     Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     let serial_setup = "Serial.begin(9600);";
//     Blockly.Python.addSetup('serial_serial', serial_setup);
//     var httpclient = "HTTPClient http;";
//     Blockly.Python.addDeclaration("httpclient", httpclient);
//     var cococloud_Response = "String cococloud_response;";
//     Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
//     Blockly.Python.addDeclaration("payload", "String payload;");
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);

//     var code = "";
//     var item_url = block.getFieldValue("event0");
//     var dataGet = Blockly.Python.variableDB_.getName(
//         block.getFieldValue('DATAVAR'), Blockly.Variables.NAME_TYPE);

//     code += "http.begin(\"" + item_url + "\")\n" +
//         "http.addHeader(\"Content-Type\", \"application/json\")\n" +
//         "httpCode = http.GET()\n" +
//         "payload = http.getString()\n" +
//         "cococloud_response = payload\n" +
//         "Serial.println(httpCode)\n" +
//         "Serial.println(payload)\n" +
//         "http.end()\n" +
//         "StaticJsonBuffer<240> jsonBuffer_easymode\n" +
//         "JsonObject& cloud_data  =  jsonBuffer_easymode.parseObject(cococloud_response)\n" +
//         "" + dataGet + " = cloud_data[\"data\"][0][\"EM_data\"].as<char*>();";
//     return code;
// }


// Blockly.Python['wifiEasymode_cococloud2_post'] = function(block) {
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
//     var HTTPResponse = "String http_response;";
//     Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     let serial_setup = "Serial.begin(9600);";
//     Blockly.Python.addSetup('serial_serial', serial_setup);
//     var httpclient = "HTTPClient http;";
//     Blockly.Python.addDeclaration("httpclient", httpclient);
//     Blockly.Python.addDeclaration("payload", "String payload;");
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);
//     var precode = "char JSONmessageBuffer[240]\n";
//     Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);

//     var code = "";
//     // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
//     var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
//     var code = "StaticJsonBuffer<240> JSONbuffer_easymode\n" +
//         "JsonObject& Json_easymode = JSONbuffer_easymode.createObject()\n";
//     for (var n = 0; n < this.itemCount_; n++) {
//         var item_field = block.getFieldValue("field" + n);
//         var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
//             Blockly.Python.ORDER_NONE) || '0';
//         code += "Json_easymode[\"" + item_field + "\"]=" + item_value + "\n";
//     }
//     code += "Json_easymode.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
//         "http.begin(\"" + item_url + "\")\n" +
//         "http.addHeader(\"Content-Type\", \"application/json\")\n" +
//         "httpCode = http.POST(JSONmessageBuffer)\n" +
//         "payload = http.getString()\n" +
//         "Serial.println(httpCode)\n" +
//         "Serial.println(payload)\n" +
//         "http.end()\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_cococloud2_get'] = function(block) {
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
//     var HTTPResponse = "String http_response;";
//     Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     let serial_setup = "Serial.begin(9600);";
//     Blockly.Python.addSetup('serial_serial', serial_setup);
//     var httpclient = "HTTPClient http;";
//     Blockly.Python.addDeclaration("httpclient", httpclient);
//     var cococloud_Response = "String cococloud_response;";
//     Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
//     Blockly.Python.addDeclaration("payload", "String payload;");
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);

//     var code = "";
//     // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
//     var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
//     var dataGet, item_value;

//     code += "http.begin(\"" + item_url + "\")\n" +
//         "http.addHeader(\"Content-Type\", \"application/json\")\n" +
//         "httpCode = http.GET()\n" +
//         "payload = http.getString()\n" +
//         "cococloud_response = payload\n" +
//         "Serial.println(httpCode)\n" +
//         "Serial.println(payload)\n" +
//         "http.end()\n" +
//         "StaticJsonBuffer<240> jsonBuffer_easymode\n" +
//         "JsonObject& cloud_data  =  jsonBuffer_easymode.parseObject(cococloud_response)\n";
//     for (var n = 0; n < this.itemCount_; n++) {
//         dataGet = Blockly.Python.variableDB_.getName(
//             block.getFieldValue('DATA' + n), Blockly.Variables.NAME_TYPE);
//         item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
//             Blockly.Python.ORDER_NONE) || '""';
//         code += "" + dataGet + "=" + item_value + "\n";
//     }
//     return code;
// }

// Blockly.Python['wifiEasymode_cococloud2_get_data'] = function(block) {
//     var code = "";
//     var key = block.getFieldValue("KEY");
//     // var varType = block.getFieldValue('VAR_TYPE');
//     // if (varType == "Text") {
//     //     varType = "char*";
//     // } else if (varType == "Number") {
//     //     varType = "int";
//     // } else if (varType == "Boolean") {
//     //     varType = "boolean";
//     // } else if (varType == "Large Number") {
//     //     varType = "long";
//     // } else if (varType == "Decimal") {
//     //     varType = "float";
//     // }
//     code = "cloud_data[\"data\"][0][\"" + key + "\"].as<char*>()";
//     return [code, Blockly.Python.ORDER_ATOMIC];
// }

// Blockly.Python['wifiEasymode_IFTTT'] = function(block) {
//     var key = block.getFieldValue('IFTTT_KEY').replace(/\"/g, '') || "";
//     var event = block.getFieldValue('IFTTT_EVENT').replace(/\"/g, '') || "";
//     var value1 = Blockly.Python.valueToCode(block, 'dataA', Blockly.Python.ORDER_ATOMIC) || "";
//     // var value2 = Blockly.Python.valueToCode(block, 'VALUE2', Blockly.Python.ORDER_ATOMIC) || "";
//     // var value3 = Blockly.Python.valueToCode(block, 'VALUE3', Blockly.Python.ORDER_ATOMIC) || "";
//     var jsontool_declaration = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("jsontool_declaration", jsontool_declaration);
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
//     var httpclient = "HTTPClient http;";
//     Blockly.Python.addDeclaration("httpclient", httpclient);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     Blockly.Python.addDeclaration("payload", "String payload;");
//     var precode = "char JSONmessageBuffer[240]\n";
//     Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
//     var url = "http://maker.ifttt.com/trigger/" + event + "/with/key/" + key;
//     var code = "StaticJsonBuffer<240> JSONbuffer_ifttt\n" +
//         "JsonObject& JSONencoder = JSONbuffer_ifttt.createObject()\n" +
//         "JSONencoder[\"value1\"] = String(" + value1 + ")\n" +
//         "JSONencoder[\"value2\"] = \"0\"\n" +
//         "JSONencoder[\"value3\"] = \"0\"\n" +
//         "JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
//         "Serial.println(JSONmessageBuffer)\n" +
//         "if (WiFi.status() == WL_CONNECTED) {\n" +
//         "  http.begin(\"" + url + "\")\n" +
//         "  http.addHeader(\"Content-Type\", \"application/json\")\n" +
//         "  httpCode = http.POST(JSONmessageBuffer)\n" +
//         "  payload = http.getString()\n" +
//         "  Serial.println(httpCode)\n" +
//         "  Serial.println(payload)\n" +
//         "  http.end()\n" +
//         "} else {\n" +
//         "  Serial.println(\"Error in WiFi connection\")\n" +
//         "}\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_transfer_send'] = function(block) {
//     var dataMain = Blockly.Python.variableDB_.getName(
//         block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
//     Blockly.Python.addSetup('serial', "Serial.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" +
//         "Serial.write(DataOut_, " + dataLength + ")\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_transfer_receive'] = function(block) {
//     var dataMain = Blockly.Python.variableDB_.getName(
//         block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
//     Blockly.Python.addSetup('serial', "Serial.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 (Serial.available() > 0) {\n" +
//         "    int inChar = Serial.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" +
//         "if (!receiveMsgsuccess) {\n" +
//         "  Serial.println(\"Error: Fail to receive data!\")\n" +
//         "  return\n" +
//         "}\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_transfer2_send'] = function(block) {
//     var dataMain = block.getFieldValue('DATAMAIN');
//     var dataMain_list = "int " + dataMain + "[" + this.itemCount_ + "] = {0";
//     for (var i = 1; i < this.itemCount_; i++) {
//         dataMain_list += ",0";
//     }
//     dataMain_list += "}\n";
//     Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
//     Blockly.Python.addSetup('serial', "Serial.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 = "";

//     var item_value = '';
//     for (var n = 0; n < this.itemCount_; n++) {
//         item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
//             Blockly.Python.ORDER_NONE) || '0';
//         code += dataMain + "[" + n + "] = " + item_value + "\n";
//     }
//     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" +
//         "Serial.write(DataOut_, " + dataLength + ")\n";
//     return code;
// }

// Blockly.Python['wifiEasymode_transfer2_receive'] = function(block) {
//     var dataMain = block.getFieldValue('DATAMAIN');
//     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('serial', "Serial.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 (Serial.available() > 0) {\n" +
//         "    int inChar = Serial.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" +
//         "if (!receiveMsgsuccess_) {\n" +
//         "  Serial.println(\"Error: Fail to receive data!\")\n" +
//         "  return\n" +
//         "}\n";
//     return code;
// }

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