// 'use strict';

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


// /****************************************
//  *   *    *    *    *    *    *    *    *
//  *           Generate JSON              *
//  *   *    *    *    *    *    *    *    *
//  ****************************************/
// Blockly.Python['jsontool_generator_setup'] = function (block) {
//   var jsontool_declaration= "#include <ArduinoJson.h>";
//   Blockly.Python.addDeclaration("jsontool_declaration",jsontool_declaration);
//   var jsonname= block.getFieldValue('JSONNAME');
//   var code= "StaticJsonBuffer<200> JSONbuffer;\n" +
//             "JsonObject& " + jsonname + " = JSONbuffer.createObject();\n";
//   return code;
// };

// Blockly.Python['jsontool_generator_keyvalue'] = function (block) {
//   var jsonname= block.getFieldValue('JSONNAME');
//   var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
//   var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";

//   var code= ""+jsonname+"["+key+ "] = "+ value+";\n";
//   return code;
// };

// Blockly.Python['jsontool_generator_main_block'] = function (block) {
//   var jsontool_declaration= "#include <ArduinoJson.h>";
//   Blockly.Python.addDeclaration("jsontool_declaration",jsontool_declaration);
//   var jsonType = block.getFieldValue('jsontype') || "Object";
//   var jsonName = block.getFieldValue('NAME') || "jsonVar";
//   var code= "StaticJsonBuffer<240> JSONbuffer_"+jsonName+";\n";
//   if (jsonType == "Object") {
//     code += "JsonObject& "+jsonName+" = JSONbuffer_"+jsonName+".createObject();\n";
//   } else if (jsonType == "Array"){
//     code += "JsonArray& "+jsonName+" = JSONbuffer_"+jsonName+".createArray();\n";
//   }
//   var branch = Blockly.Python.statementToCode(block, 'STACK');
//   // console.log("jsontool.js-40: branch :\n"+ branch);
//   /* spilt the branch String into servral string according to tag "$"
//   * each string means a statement
//   * */
//   var statements_arr = branch.split("$");  
//   for (var i in statements_arr) {
//     /* spilt each statement string into two part according to tag "|"
//     * the first part is ["field"];
//     * the second part is real string of statement
//     */
//     if (statements_arr[i] == "") 
//       break; 
//     var item_arr = statements_arr[i].split("|");
//     if(item_arr[0] == "OBJECT") {
//       code += jsonName + item_arr[1]+ ";\n";
//     } else if (item_arr[0] == "ARRAY") {
//       code += jsonName + ".add("+ item_arr[1] + ");\n";
//     } else if (item_arr[0] == "NESTEDOBJECT") {
//       var item = item_arr[1].split("@");
//       var nestedJsonName = item[0];
//       if (jsonType == "Object")
//         code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject(\""+nestedJsonName+"\");\n"
//               + item[1];
//       else 
//         code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject();\n"
//               + item[1];
//     } else if (item_arr[0] == "NESTEDARRAY") {
//       var item = item_arr[1].split("@");
//       var nestedJsonName = item[0];
//       if (jsonType == "Object")
//         code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray(\""+nestedJsonName+"\");\n"
//               + item[1]+ "\n";
//       else 
//         code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray();\n"
//              + item[1];
//     }
//   }
//   return code;
// };

// Blockly.Python['jsontool_generator_nested_block'] = function (block) {
//   var jsonType = block.getFieldValue('jsontype') || "Object";
//   var jsonName = block.getFieldValue('NAME') || "jsonVar";
//   var code;
//   if (jsonType == "Object") {
//     code = "$NESTEDOBJECT|"+jsonName+"@";
//   } else if (jsonType == "Array"){
//     code = "$NESTEDARRAY|"+jsonName+"@";
//   }
//   var branch = Blockly.Python.statementToCode(block, 'STACK1');
//   /* spilt the branch String into servral string according to tag "$"
//   * each string means a statement
//   * */
//   var statements_arr = branch.split("$");  
//   for (var i in statements_arr) {
//     /* spilt each statement string into two part according to tag "|"
//     * the first part is ["field"];
//     * the second part is real string of statement
//     */
//     if (statements_arr[i] == "") 
//       break; 
//     var item_arr = statements_arr[i].split("|");

//     if(item_arr[0] == "OBJECT") {
//       code += jsonName + item_arr[1]+ ";\n";
//     } else if (item_arr[0] == "ARRAY") {
//       code += jsonName + ".add("+ item_arr[1] + ");\n";
//     } else if (item_arr[0] == "NESTEDOBJECT") {
//       var item = item_arr[1].split("@");
//       var nestedJsonName = item[0];
//       if (jsonType == "Object")
//         code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject(\""+nestedJsonName+"\");\n"
//               + item[1];
//       else 
//         code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject();\n"
//               + item[1];
//     } else if (item_arr[0] == "NESTEDARRAY") {
//       var item = item_arr[1].split("@");
//       var nestedJsonName = item[0];
//       if (jsonType == "Object")
//         code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray(\""+nestedJsonName+"\");\n"
//               + item[1];
//       else 
//         code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray();\n"
//              + item[1];
//     }
//   }
//   // console.log("nestedJson code"+ code);
//   return code;
// };
// Blockly.Python['jsontool_generator_object_keyValue'] = function (block) {
//   var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
//   var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";
//   var code = "$OBJECT|["+key+"]=" + value;
//   return code;
// }

// Blockly.Python['jsontool_generator_array_value'] = function (block) {
//   var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";
//   var code = "$ARRAY|" + value;
//   return code;
// }

// /****************************************
//  *   *    *    *    *    *    *    *    *
//  *             Parse JSON               *
//  *   *    *    *    *    *    *    *    *
//  ****************************************/
// Blockly.Python['jsontool_parse_setup'] = function (block) {
//   var jsontool_declaration= "#include <ArduinoJson.h>";
//   Blockly.Python.addInclude("jsontool_declaration",jsontool_declaration);
//   var object_size= Blockly.Python.valueToCode(block, 'OBJECT_SIZE', Blockly.Python.ORDER_ATOMIC) || "";
//   var array_size= Blockly.Python.valueToCode(block, 'ARRAY_SIZE', Blockly.Python.ORDER_ATOMIC) || "";
//   var response= Blockly.Python.valueToCode(block, 'RESPONSE', Blockly.Python.ORDER_ATOMIC) || "";
//   var jsonname= block.getFieldValue('JSONNAME');
//   var jsonParseCode= "const size_t capacity_"+jsonname +" = JSON_OBJECT_SIZE("+ object_size+
//                     ") + JSON_ARRAY_SIZE("+array_size+") + 60;\n" +
//                     "DynamicJsonBuffer jsonBuffer_"+jsonname +"(capacity_"+jsonname +");\n" +
//                     "JsonObject& "+jsonname+" = jsonBuffer_"+jsonname +".parseObject("+response+");\n";

//   return jsonParseCode;
// };

// Blockly.Python['jsontool_parse_begin'] = function(block) {
//   var jsonname = block.getFieldValue('JSONNAME');
//   var parseVar = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
//       Blockly.Variables.NAME_TYPE);
//   var varType = block.getFieldValue('JSONVAR_TYPE');
//   if (varType == "Text") {
//     varType = "String";
//   } 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 key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
//   var code = parseVar+"=("+varType+")"+ jsonname+""+key+";\n";
//   return code;
// };

// Blockly.Python['jsontool_parse_key'] = function(block) {
//   var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+key+"]";
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };
// Blockly.Python['jsontool_parse_arr_inobject'] = function(block) {
//   var array= Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
//   var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+array+"]["+index+"]";
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };
// Blockly.Python['jsontool_parse_arr_inarray'] = function(block) {
//   var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+index+"]";
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };
// Blockly.Python['jsontool_parse_key_nest'] = function(block) {
//   var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
//   var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+key+"]"+ nest;
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };
// Blockly.Python['jsontool_parse_arr_inobject_nest'] = function(block) {
//   var array= Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
//   var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
//   var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+array+"]["+index+"]"+nest;
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };

// Blockly.Python['jsontool_parse_arr_inarray_nest'] = function(block) {
//   var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
//   var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
//   var code="["+index+"]"+nest;
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };


// Blockly.Python['jsontool_parse_prettyPrint'] = function(block) {
//   var jsonname= block.getFieldValue('JSONNAME');
//   var precode = "char JSONmessageBuffer[240];\n";
//   Blockly.Python.addDeclaration("jsontool_prettyPrint",precode);
//   var code = ""+ jsonname+ ".prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n";
//   return code;
// };
// Blockly.Python['jsontool_parse_prettyPrint_display'] = function(block) {
//   var code = "JSONmessageBuffer";
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };

// Blockly.Python['jsontool_parse_isSuccess'] = function(block) {
//   var jsonname= block.getFieldValue('JSONNAME');
//   var code = ""+jsonname+".success()";
//   return [code, Blockly.Python.ORDER_ATOMIC];
// };