// 'use strict';

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


// /****************************************
//  *   *    *    *    *    *    *    *    *
//  *     Simple version for CocoCloud     *
//  *           Generate JSON              *
//  *   *    *    *    *    *    *    *    *
//  ****************************************/
// Blockly.Python['clouddata_json_generator'] = function(block) {
//     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 VarName = block.getFieldValue('VAR') || "json";
//     var code = "StaticJsonBuffer<240> JSONbuffer_" + VarName + ";\n" +
//         "JsonObject& " + VarName + " = JSONbuffer_" + VarName + ".createObject();\n";
//     var size = block.itemCount_;
//     var item_key = '',
//         item_value = '';
//     for (var n = 0; n < this.itemCount_; n++) {
//         item_key = block.getFieldValue('KEY' + n);
//         item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
//             Blockly.Python.ORDER_NONE) || '';
//         code += VarName + '[\"' + item_key + '\"]=' + item_value + ";\n";
//     }
//     code += "" + VarName + ".prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n";
//     return code;
// }

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

// /****************************************
//  *   *    *    *    *    *    *    *    *
//  *     Simple version for CocoCloud     *
//  *              Parse JSON              *
//  *   *    *    *    *    *    *    *    *
//  ****************************************/
// Blockly.Python['clouddata_json_parser_setup'] = function(block) {
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);
//     var payload = Blockly.Python.valueToCode(this, 'PAYLOAD', Blockly.Python.ORDER_NONE) || '';
//     var data = block.getFieldValue('DATA') || '';
//     var code = "StaticJsonBuffer<240> jsonBuffer_" + data + ";\n" +
//         "JsonObject& cloud_" + data + " = jsonBuffer_" + data + ".parseObject(" + payload + ");\n";
//     return code;
// }

// Blockly.Python['clouddata_json_parser'] = function(block) {
//     var data = block.getFieldValue('DATA') || '';
//     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";
//     }
//     var code = "cloud_" + data + "[\"data\"][0][\"" + key + "\"].as<" + varType + ">()";
//     return [code, Blockly.Python.ORDER_ATOMIC];
// }

// Blockly.Python['cococloud_simple_post'] = function(block) {
//     var size = block.itemCount_;
//     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("wifi_httpclient_setup", httpclient);
//     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
//     // console.log(block.eventTag);
//     block.eventTag = item_url;
//     var item_data = Blockly.Python.valueToCode(this, 'ADD0',
//         Blockly.Python.ORDER_NONE) || '';
//     code += "http.begin(\"" + item_url + "\");\n" +
//         "  http.addHeader(\"Content-Type\", \"application/json\");\n" +
//         "  int httpCode = http.POST(" + item_data + ");\n" +
//         "  String payload = http.getString();\n" +
//         "  Serial.println(httpCode);\n" +
//         "  Serial.println(payload);\n" +
//         "  http.end();\n";
//     return code;
// }

// Blockly.Python['cococloud_simple_get'] = function(block) {
//     var size = block.itemCount_;
//     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("wifi_httpclient_setup", httpclient);
//     var cococloud_Response = "String cococloud_response;";
//     Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
//     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
//     code += "http.begin(\"" + item_url + "\");\n" +
//         "  http.addHeader(\"Content-Type\", \"application/json\");\n" +
//         "  int httpCode = http.GET();\n" +
//         "  String payload = http.getString();\n" +
//         "  cococloud_response = payload;\n" +
//         "  Serial.println(httpCode);\n" +
//         "  Serial.println(payload);\n" +
//         "  http.end();\n";
//     return code;
// }

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

// /* 
//  *  World time
//  *  setup
//  */
// Blockly.Python['cloud_time_setup'] = function(block) {
//     Blockly.Python.addInclude('wire', "#include <Wire.h>");
//     Blockly.Python.addInclude('RTClib', "#include \"RTClib.h\"");
//     var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
//     Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
//     var json_include = "#include <ArduinoJson.h>";
//     Blockly.Python.addInclude("json_declaration", json_include);

//     var HTTPResponse = "String http_response;";
//     Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
//     var HTTPCode = "int httpCode;";
//     Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
//     var HTTPClient = "HTTPClient http;\n";
//     Blockly.Python.addDeclaration("wifi_httpclient_setup", HTTPClient);
//     var cloud_time = "DateTime worldtime;\n";
//     Blockly.Python.addDeclaration("cloud_time", cloud_time);

//     let serial_setup = "Serial.begin(9600);";
//     Blockly.Python.addSetup('serial_serial', serial_setup);

//     var code = "http.begin(\"http://api.cocorobo.cn/iot/time\");\n" +
//         "  httpCode = http.GET();\n" +
//         "  Serial.print(\"HTTP code: \");\n" +
//         "  Serial.println(httpCode);\n" +
//         "  http_response = http.getString();\n" +
//         "  http.end();\n" +
//         "  delay(300);\n" +
//         "  StaticJsonBuffer<240> jsonBuffer_Data\n;" +
//         "  JsonObject& cloud_Data = jsonBuffer_Data.parseObject(http_response);\n" +
//         "  String temptime = cloud_Data[\"time\"].as<char*>();\n" +
//         "  String temp_time = temptime.substring(0,temptime.length()-3);\n" +
//         "  DateTime world_time(atol(temp_time.c_str())+ 3600*8);\n";
//     return code;
// }

// /* 
//  *  World time
//  *  get Date
//  */
// Blockly.Python['cloud_time_getDate'] = function(block) {
//     var detail = block.getFieldValue("date_detail");
//     var code = "";
//     switch (detail) {
//         case "year":
//             code = "world_time.year()";
//             break;
//         case "month":
//             code = "world_time.month()";
//             break;
//         case "day":
//             code = "world_time.day()";
//             break;
//         default:
//             break;
//     }
//     return [code, Blockly.Python.ORDER_ATOMIC];
// }

// /* 
//  *  get Time
//  */
// Blockly.Python['cloud_time_getTime'] = function(block) {
//     var detail = block.getFieldValue("time_detail");
//     var code = "";
//     switch (detail) {
//         case "hour":
//             code = "world_time.hour()";
//             break;
//         case "minute":
//             code = "world_time.minute()";
//             break;
//         case "second":
//             code = "world_time.second()";
//             break;
//         default:
//             break;
//     }
//     return [code, Blockly.Python.ORDER_ATOMIC];
// }