/**
 * @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.basic101');
goog.require('Blockly.Python');

Blockly.Python['read_button'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');

	var code = 'digitalRead('+pinNum+')';
	return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Python['basic101_dropdown_button']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['read_knob'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	Blockly.Python.addSetup('basic101_setup_knob'+pinNum, 'pinMode(' + pinNum + ', INPUT);');

	var code = 'analogRead('+pinNum+')';
	return [code, Blockly.Python.ORDER_ATOMIC]; 
};
Blockly.Python['basic101_dropdown_knob']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['analog_light'] = function(block) {
	//var pinsNum = block.getFieldValue('PINSNUM');
	var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	var bright = Blockly.Python.valueToCode(block, 'BRIGHTNESS', Blockly.Python.ORDER_ATOMIC);
	var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
	Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);

	var code = 'analogWrite(' + pinsNum + ', ' + bright + ');\n';
	return code;
};
Blockly.Python['basic101_dropdown_analog_light']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};


Blockly.Python['digital_light'] = function(block) {
	//var pinsNum = block.getFieldValue('PINSNUM');
	var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
	Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);

	var code = 'digitalWrite(' + pinsNum + ', HIGH);\n';
	return code;
};
Blockly.Python['basic101_dropdown_digital_light']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};


Blockly.Python['nolight'] = function(block) {
	//var pinsNum = block.getFieldValue('PINSNUM');
	var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
	Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);

	var code = 'digitalWrite(' + pinsNum + ', LOW);\n';
	return code;
};
Blockly.Python['basic101_dropdown_nolight']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};


Blockly.Python['tone'] = function(block) {
	//var pinsNum = block.getFieldValue('PINSNUM');
	var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	var freq = Blockly.Python.valueToCode(block, 'FREQUENCY', Blockly.Python.ORDER_ATOMIC);
	var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
	Blockly.Python.addSetup('basic101_setup_tone' + pinsNum, pinSetupCode, false);

	var code = 'tone(' + pinsNum + ',' + freq + ');\n';
	return code;
};
Blockly.Python['basic101_dropdown_tone']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['notone'] = function(block) {
	//var pinsNum = block.getFieldValue('PINSNUM');
	var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
	Blockly.Python.addSetup('io_' + pinsNum, pinSetupCode, false);

	var code = 'noTone(' + pinsNum + ');\n';
	return code;
};
Blockly.Python['basic101_dropdown_notone']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['io_dropdown_digital']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Python['io_dropdown_analog']= function(block) {
	  var code = block.getFieldValue('SELECTPIN');
	  return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Python['io_dropdown_pwm']= function(block) {
	  var code = block.getFieldValue('SELECTPIN');
	  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['counter_button_setup'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var pinNum = block.getFieldValue('PINSNUM');
	Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');
	Blockly.Python.addDeclaration('basic101_declaration_counter','int counter = 0;\n  int triggle = 0;\n');
	var code =	'  int signal = digitalRead('+pinNum+');\n'+ 
				'  if ((triggle == 0) && (signal==1)) {\n' +
				'    counter++;\n'+
				'    triggle=1;\n'+
				'  }\n'+
				'  if ((triggle == 1) && (signal==0)) {\n' +
				'    triggle=0;\n'+
				'  }\n';
	return code;
};
Blockly.Python['read_counter'] = function(block) {
	//var pinNum = block.getFieldValue('PINSNUM');
	var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
	Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');

	var code = 'counter';
	return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Python['basic101_counter_dropdown_button']= function(block) {
	var code = block.getFieldValue('SELECTPIN');
	return [code, Blockly.Python.ORDER_ATOMIC];
};