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

/**
 * @fileoverview Generating Arduino code for the logic blocks.
 */
'use strict';

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

goog.require('Blockly.Python');


/**
 * Code generator to create if/if else/else statement.
 * Arduino code: loop { if (X)/else if ()/else { X } }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {string} Completed code.
 */
Blockly.Python['controls_if'] = function(block) {
    var n = 0;
    var argument = Blockly.Python.valueToCode(block, 'IF' + n,
        Blockly.Python.ORDER_NONE) || 'false';
    var branch = Blockly.Python.statementToCode(block, 'DO' + n);
    var code = 'if (' + argument + ') {\n' + branch + '}';
    for (n = 1; n <= block.elseifCount_; n++) {
        argument = Blockly.Python.valueToCode(block, 'IF' + n,
            Blockly.Python.ORDER_NONE) || 'false';
        branch = Blockly.Python.statementToCode(block, 'DO' + n);
        code += ' else if (' + argument + ') {\n' + branch + '}';
    }
    if (block.elseCount_) {
        branch = Blockly.Python.statementToCode(block, 'ELSE');
        code += ' else {\n' + branch + '}';
    }
    return code + '\n';
};

Blockly.Python['switch_statement'] = function(block) {
    var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('switch_var'), Blockly.Variables.NAME_TYPE);
    var code = "switch (" + variable_name + ") {\n";
    var argument, branch;
    for (var n = 0; n <= block.caseCount_; n++) {
        argument = Blockly.Python.valueToCode(block, 'CASE' + n,
            Blockly.Python.ORDER_NONE) || n;
        branch = Blockly.Python.statementToCode(block, 'DO' + n);
        code += "  case " + argument + ":\n" + branch + "  break;\n";
    }
    if (block.defaultCount_) {
        branch = Blockly.Python.statementToCode(block, 'DEFAULT');
        code += '  default:\n' + branch + '  break;\n';
    }
    return code + '}\n';
}

/**
 * Code generator for the comparison operator block.
 * Arduino code: loop { X operator Y }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['logic_compare'] = function(block) {
    var OPERATORS = {
        'EQ': '==',
        'NEQ': '!=',
        'LT': '<',
        'LTE': '<=',
        'GT': '>',
        'GTE': '>='
    };
    var operator = OPERATORS[block.getFieldValue('OP')];
    var order = (operator == '==' || operator == '!=') ?
        Blockly.Python.ORDER_EQUALITY : Blockly.Python.ORDER_RELATIONAL;
    var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
    var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
    var code = argument0 + ' ' + operator + ' ' + argument1;
    return [code, order];
};

/**
 * Code generator for the logic operator block.
 * Arduino code: loop { X operator Y }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['logic_operation'] = function(block) {
    var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
    var order = (operator == '&&') ? Blockly.Python.ORDER_LOGICAL_AND :
        Blockly.Python.ORDER_LOGICAL_OR;
    var argument0 = Blockly.Python.valueToCode(block, 'A', order) || 'false';
    var argument1 = Blockly.Python.valueToCode(block, 'B', order) || 'false';
    if (!argument0 && !argument1) {
        // If there are no arguments, then the return value is false.
        argument0 = 'false';
        argument1 = 'false';
    } else {
        // Single missing arguments have no effect on the return value.
        var defaultArgument = (operator == '&&') ? 'true' : 'false';
        if (!argument0) {
            argument0 = defaultArgument;
        }
        if (!argument1) {
            argument1 = defaultArgument;
        }
    }
    var code = argument0 + ' ' + operator + ' ' + argument1;
    return [code, order];
};

/**
 * Code generator for the logic negate operator.
 * Arduino code: loop { !X }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['logic_negate'] = function(block) {
    var order = Blockly.Python.ORDER_UNARY_PREFIX;
    var argument0 = Blockly.Python.valueToCode(block, 'BOOL', order) || 'false';
    var code = '!' + argument0;
    return [code, order];
};

/**
 * Code generator for the boolean values true and false.
 * Arduino code: loop { true/false }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['logic_boolean'] = function(block) {
    var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

/**
 * Code generator for the null value.
 * Arduino code: loop { X ? Y : Z }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 */
Blockly.Python['logic_null'] = function(block) {
    var code = 'NULL';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

/**
 * Code generator for the ternary operator.
 * Arduino code: loop { NULL }
 * @param {!Blockly.Block} block Block to generate the code from.
 * @return {array} Completed code with order of operation.
 *
 * TODO: Check types of THEN and ELSE blocks and add warning to this block if
 *       they are different from each other.
 */
Blockly.Python['logic_ternary'] = function(block) {
    var valueIf = Blockly.Python.valueToCode(block, 'IF',
        Blockly.Python.ORDER_CONDITIONAL) || 'false';
    var valueThen = Blockly.Python.valueToCode(block, 'THEN',
        Blockly.Python.ORDER_CONDITIONAL) || 'null';
    var valueElse = Blockly.Python.valueToCode(block, 'ELSE',
        Blockly.Python.ORDER_CONDITIONAL) || 'null';
    var code = valueIf + ' ? ' + valueThen + ' : ' + valueElse;
    return [code, Blockly.Python.ORDER_CONDITIONAL];
};