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

/**
 * @fileoverview Code generator for Arduino Digital and Analogue input/output.
 *     Arduino built in function docs: http://arduino.cc/en/Reference/HomePage
 */
'use strict';

goog.provide('Blockly.Python.IO');

goog.require('Blockly.Python');


/**
 * Function for 'set pin' (X) to a state (Y).
 * Arduino code: setup { pinMode(X, OUTPUT); }
 *               loop  { digitalWrite(X, Y); }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {string} Completed code.
 */
Blockly.Python['io_digitalwrite'] = function(block) {
  var pin = Blockly.Python.valueToCode(
    block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '1';
  var stateOutput = Blockly.Python.valueToCode(
      block, 'STATE', Blockly.Python.ORDER_ATOMIC) || 'LOW';

  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.OUTPUT, 'Digital Write');

  var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'digitalWrite(' + pin + ', ' + stateOutput + ')\n';
  return code;
};

/**
 * Function for reading a digital pin (X).
 * Arduino code: setup { pinMode(X, INPUT); }
 *               loop  { digitalRead(X)     }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['io_digitalread'] = function(block) {
  var pin = Blockly.Python.valueToCode(
        block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '1';
  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.INPUT, 'Digital Read');

  var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'digitalRead(' + pin + ')';
  return [code, Blockly.Python.ORDER_ATOMIC];
};

/**
 * Function for setting the state (Y) of a built-in LED (X).
 * Arduino code: setup { pinMode(X, OUTPUT); }
 *               loop  { digitalWrite(X, Y); }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {string} Completed code.
 */
Blockly.Python['io_builtin_led'] = function(block) {
  var pin = block.getFieldValue('BUILT_IN_LED');
  var stateOutput = Blockly.Python.valueToCode(
      block, 'STATE', Blockly.Python.ORDER_ATOMIC) || 'LOW';

  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.OUTPUT, 'Set LED');

  var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'digitalWrite(' + pin + ', ' + stateOutput + ')\n';
  return code;
};

/**
 * Function for setting the state (Y) of an analogue output (X).
 * Arduino code: setup { pinMode(X, OUTPUT); }
 *               loop  { analogWrite(X, Y);  }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {string} Completed code.
 */
Blockly.Python['io_analogwrite'] = function(block) {
  var pin = Blockly.Python.valueToCode(
        block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';
  var stateOutput = Blockly.Python.valueToCode(
      block, 'ANALOGNUM', Blockly.Python.ORDER_ATOMIC) || '0';
  var reg=/A[0-9]/g;
  var isAnalogInput = true;
  isAnalogInput = reg.test(pin);
  // console.log("io.js-input isAnalogInput: "+ isAnalogInput);
  var pinI='';
  if (!isAnalogInput){
    Blockly.Python.addDeclaration('io_analogPinList','const int analogPIN[8]={A0,A1,A2,A3,A4,A5,A6,A7};'); 
    Blockly.Python.addSetup('io_A0','pinMode(A0, OUTPUT);');
    Blockly.Python.addSetup('io_A1','pinMode(A1, OUTPUT);');
    Blockly.Python.addSetup('io_A2','pinMode(A2, OUTPUT);');
    Blockly.Python.addSetup('io_A3','pinMode(A3, OUTPUT);');
    Blockly.Python.addSetup('io_A4','pinMode(A4, OUTPUT);');
    Blockly.Python.addSetup('io_A5','pinMode(A5, OUTPUT);');
    Blockly.Python.addSetup('io_A6','pinMode(A6, OUTPUT);');
    Blockly.Python.addSetup('io_A7','pinMode(A7, OUTPUT);');
 
    pinI = "analogPIN["+pin+"]";
    
  } else {
     Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.OUTPUT, 'Analogue Write');
    var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
    Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  }
  // Warn if the input value is out of range
  if ((stateOutput < 0) || (stateOutput > 255)) {
    block.setWarningText('The analogue value set must be between 0 and 255',
                         'pwm_value');
  } else {
    block.setWarningText(null, 'pwm_value');
  }
  if (isAnalogInput) {
  var code = 'analogWrite(' + pin + ', ' + stateOutput + ')\n';
  } else {
    var code = 'analogWrite(' + pinI + ', ' + stateOutput + ')\n';
  }
  return code;
};

/**
 * Function for reading an analogue pin value (X).
 * Arduino code: setup { pinMode(X, INPUT); }
 *               loop  { analogRead(X)      }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['io_analogread'] = function(block) {
  var pin = Blockly.Python.valueToCode(
        block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';

  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.INPUT, 'Analogue Read');

  var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'analogRead(' + pin + ')';
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['io_pwmwrite'] = function(block) {
  var pin = Blockly.Python.valueToCode(
        block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';
  var stateOutput = Blockly.Python.valueToCode(
      block, 'PWMVALUE', Blockly.Python.ORDER_ATOMIC) || '0';
  var reg=/[0-9]/g;
  var isVarInput = false;
  isVarInput = !reg.test(pin);
  // console.log("io.js-input isPwmInput: "+ isVarInput);
  Blockly.Python.reservePin(
    block, pin, Blockly.Python.PinTypes.OUTPUT, 'PWM Write');
  var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  var code = 'analogWrite(' + pin + ', ' + stateOutput + ')\n';
  return code;
};

/**
 * Value for defining a digital pin state.
 * Arduino code: loop { HIGH / LOW }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['io_highlow'] = function(block) {
  var code = block.getFieldValue('STATE');
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['io_pulsein'] = function(block) {
  var pin = block.getFieldValue("PULSEPIN");
  var type = Blockly.Python.valueToCode(block, "PULSETYPE", Blockly.Python.ORDER_ATOMIC);

  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.INPUT, 'Pulse Pin');

  var pinSetupCode = 'pinMode(' + pin + ', INPUT)\n';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'pulseIn(' + pin + ', ' + type + ')';

  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['io_pulsetimeout'] = function(block) {
  var pin = block.getFieldValue("PULSEPIN");
  var type = Blockly.Python.valueToCode(block, "PULSETYPE", Blockly.Python.ORDER_ATOMIC);
  var timeout = Blockly.Python.valueToCode(block, "TIMEOUT", Blockly.Python.ORDER_ATOMIC);

  Blockly.Python.reservePin(
      block, pin, Blockly.Python.PinTypes.INPUT, 'Pulse Pin');

  var pinSetupCode = 'pinMode(' + pin + ', INPUT)\n';
  Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);

  var code = 'pulseIn(' + pin + ', ' + type + ', ' + timeout + ')';

  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['text_input'] = function(block) {
  var message = block.getFieldValue('MESSAGE');
  var code = 'input('+Blockly.Python.quote_(message)+')';
  return [code, Blockly.JavaScript.ORDER_NONE];
};