/**
 * @license Licensed under the Apache License, Version 2.0 (the "License"):
 *          http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @fileoverview Code generator for the test 2 blocks.
 */
'use strict';

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

Blockly.Python['blynk_setup'] = function(block) {
	var auth = block.getFieldValue('AUTH');
	var ssid = block.getFieldValue('SSID');
	var pw = block.getFieldValue('PW');
	Blockly.Python.addDeclaration('blynk_define','#define BLYNK_PRINT Serial\n');
	Blockly.Python.addInclude('blynk_setup','#include <ESP8266WiFi.h>\n'+
		'#include <BlynkSimpleEsp8266.h>\n');
	var blynk_init = 'char auth[] = "'+ auth + '";\n' +
		'char ssid[] = "' + ssid + '";\n' +
		'char pass[] = "' + pw + '";\n';
	Blockly.Python.addDeclaration('Blynk_init', blynk_init);
	Blockly.Python.addSetup("Blynk_begin","Blynk.begin(auth,ssid,pass);\n");
	var code = 'Blynk.run();\n';
	return code;
};

Blockly.Python['blynk_write']= function(block) {
	var pin = block.getFieldValue("PIN");
	var select = block.getFieldValue("MOD")
	var code = 'BLYNK_WRITE('+ pin + ')\n{\n';
		// '  int pinValue['+length+']= {0};\n';
	// if  (select == "zeRGBa") {
	// 	code += '  int blynk_receive_[3]= {0};\n'
	// 	code += '  blynk_receive_[0] = param[0].asInt();\n'+
	// 		'  blynk_receive_[1] = param[1].asInt();\n'+
	// 		'  blynk_receive_[2] = param[2].asInt();\n'
	// } else {
	// 	for (let i = 0; i < length; i++) {
	// 		code += '  '+variable+'['+ i +'] = param['+i+'].asInt()';
	// 	}
	// }
	let statement = Blockly.Python.statementToCode(block, 'STATEMENT');
	code += statement +'\n}\n';
	Blockly.Python.addFunction("Blynk_Write"+pin, code);
	return '';
};

Blockly.Python['blynk_write_getData'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var code = '';
	var select = block.getFieldValue("MOD")
	if  (select == "zeRGBa" || select == "Joystick") {
		let index = block.getFieldValue("SELINDEX");
		code = "param["+ index+ "].asInt()";
	} else  {
		code = "param.asInt()";
	}
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['blynk_write_getData_joystick'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var code = '';
	var select = block.getFieldValue("MOD")
	if  (select == "zeRGBa" || select == "Joystick") {
		let index = block.getFieldValue("SELINDEX");
		code = "param["+ index+ "].asInt()";
	} else  {
		code = "param.asInt()";
	}
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['blynk_write_getData_zeRGBa'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var code = '';
	var select = block.getFieldValue("MOD")
	if  (select == "zeRGBa" || select == "Joystick") {
		let index = block.getFieldValue("SELINDEX");
		code = "param["+ index+ "].asInt()";
	} else  {
		code = "param.asInt()";
	}
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['blynk_write_getData_GraOrAcc'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	let index = block.getFieldValue("SELINDEX");
	var code = "param["+ index+ "].asFloat()";
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['blynk_write_getData_GPS'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	let index = block.getFieldValue("SELINDEX");
	var code = "param["+ index+ "].asFloat()";
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['blynk_read']= function(block) {
	var pin = block.getFieldValue("PIN");
	var code = 'BLYNK_READ('+ pin + ')\n{\n';
	for (var n = 0; n < this.itemCount_; n++) {
		var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
        	    Blockly.Python.ORDER_NONE) || '0';
		if (block.itemCount_ == 1) {
			code += '  Blynk.virtualWrite(' + pin + ',' + item_value + ');\n'
		} else {
			if (n == 0) code += '  Blynk.virtualWrite(' + pin + ',' +item_value;
			else code +=  ',' + item_value;
			if (n == block.itemCount_-1) code += ');\n';
		}
	}
	code += '\n}\n';
	Blockly.Python.addFunction("Blynk_Read"+pin, code);
	return '';
};

Blockly.Python['blynk_virtualWrite'] = function(block) {
	var pin = block.getFieldValue("PIN");
	var code = '';
	for (var n = 0; n < this.itemCount_; n++) {
		var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
        	    Blockly.Python.ORDER_NONE) || '0';
		if (block.itemCount_ == 1) {
			code += 'Blynk.virtualWrite(' + pin + ',' + item_value + ');\n'
		} else {
			if (n == 0) code += 'Blynk.virtualWrite(' + pin + ',' +item_value;
			else code +=  ',' + item_value;
			if (n == block.itemCount_-1) code += ');\n';
		}
	}
	return code;
};

Blockly.Python['blynk_timer'] = function(block) {
	var time = block.getFieldValue("TIME");
	var statement = Blockly.Python.statementToCode(block, 'DO');
	Blockly.Python.addDeclaration("Blynk_timer_dec", "BlynkTimer timer;\n")
	Blockly.Python.addFunction("Blynk_timer_func"+time, "void BF_T"+time+ "() {\n"+ statement + "}\n");
	Blockly.Python.addSetup("Blynk_timer_setup_"+time, "timer.setInterval("+ time+"L, BF_T"+ time +");\n")
	var code = "timer.run();\n"
	return code;
};


Blockly.Python['blynk_connect']= function(block) {
	var content = Blockly.Python.valueToCode(block, 'STATEMENT', Blockly.Python.ORDER_ATOMIC);
	var code = "BLYNK_CONNECT() {\n"+ content + "\n"+ "}\n";
	Blockly.Python.addFunction("Blynk_Connect", code);
	return '';
};


Blockly.Python['blynk_log'] = function(block) {
	var content = Blockly.Python.valueToCode(block, 'STATEMENT', Blockly.Python.ORDER_ATOMIC);
	var code = "BLYNK_LOG() {\n"+ content + "\n"+ "}\n";
	return code;
};