/**
 * @license
 * abbozza!
 *
 * Copyright 2015 Michael Brinkmeier ( michael.brinkmeier@uni-osnabrueck.de )
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview This file defines the code generated by the blocks.
 * 
 * Each code template is given in the follwoing form
 * 
 * Abbozza.Code[type] = [ <template> , [ <arg1>, <arg2>, ...] ]
 *  
 * @author michael.brinkmeier@uni-osnabrueck.de (Michael Brinkmeier)
 */

Blockly.Block.prototype.parametersToCode = function(generator) {
    if (!this.symbols || this.symbols == null ) return "";
    var parameters = this.symbols.getParameters(true);
    return generator.symbolsToCode(parameters,",");
}

Blockly.Block.prototype.variablesToCode = function(generator) {
    if (!this.symbols || this.symbols == null ) return "";
    var variables = this.symbols.getVariables(true);
    return generator.symbolsToCode(variables,";");
}


/**
 * Code snippets for blocks
 */

AbbozzaCode = [];

AbbozzaCode['cond_if'] = [ 'if (#):\n#\n', [ "V_CONDITION" , "S_STATEMENTS"] ];
AbbozzaCode['cond_if_else'] = [ 'if (#):\n#\nelse:\n#', [ "V_CONDITION" , "S_STATEMENTS1" , "S_STATEMENTS2" ] ];

AbbozzaCode['loop_endless'] = ["while True:\n#",["S_STATEMENTS"]];
AbbozzaCode['loop_while'] = ["while (#):\n#",["V_CONDITION","S_STATEMENTS"]];
AbbozzaCode['loop_fixed'] = ["for __counter in range(#):\n#",["V_COUNT","S_STATEMENTS"]];
AbbozzaCode['loop_count'] = ["for # in range(#, #, #):\n#",["F_VAR","V_FROM","V_TO","V_STEP","S_STATEMENTS"]];
AbbozzaCode['loop_count_dir'] = ["for # in range(#, #, #):\n #",["F_VAR","V_FROM","V_TO",
                function(generator) {
                    var dir = generator.fieldToCode(this, "DIR");
                    var step = "1";
                    if ( dir == "DESC" ) {
                        step = "-1";
                    }
                    return step;
                },
                "S_STATEMENTS"
]];
AbbozzaCode['loop_count_dir_step'] = ["for # in range(#, #, #):\n #",["F_VAR","V_FROM","V_TO","V_STEP",
                "S_STATEMENTS"
]];
AbbozzaCode['loop_delay'] = ["sleep(#)",["V_TIME"]];
AbbozzaCode['loop_break'] = ["break",[]];

AbbozzaCode['text_const'] = ["#", [Abbozza.TextConstant.getText] ];







AbbozzaCode['var_assign'] = ["#=#",["V_LEFT","V_RIGHT"]];

AbbozzaCode['logic_not'] = ["not (#)",["V_ARG"]];
AbbozzaCode['logic_compare'] = ["(#)#(#)",["V_LEFT","F_OP","V_RIGHT"]];

AbbozzaCode['display_scroll'] = [ 'display.scroll(#)', ["V_TEXT"]];

AbbozzaCode['math_number'] = ["#", ["F_VALUE"] ];
AbbozzaCode['math_random'] = ["random.randrange(0,#)",["V_MAX"], function(generator) { generator.addLibrary(["","random"]); } ];
AbbozzaCode['math_random2'] = ["random.randint(#,#)",["V_MIN","V_MAX"], function(generator) { generator.addLibrary(["","random"]); } ];
AbbozzaCode['math_randomseed'] = ["random.seed(#)",["V_SEED"], function(generator) { generator.addLibrary(["","random"]); } ];
AbbozzaCode['math_millis'] = ["running_time()",[]];




AbbozzaCode['func_decl'] = ["def #:#",[ function(generator) {
        var signature = this.name + "(" + generator.parametersToCode(this.symbols,"    ") + ")";
        return signature;
    },"S_STATEMENTS"]];
AbbozzaCode['func_call'] = ["#(#)",["F_NAME", function(generator) {
        var code = "";        
        var no = 0;
        var par;
        var inp;
        while (inp = this.getInput("PAR" + no)) {
            if (inp.type == Blockly.INPUT_VALUE) {
                par = generator.valueToCode(this, "PAR" + no);
                if (no != 0)
                    code = code + ",";
                code = code + par;
            }
            no++;
        }
        return code;
    }]];
AbbozzaCode['func_return'] = ["return #",["V_VALUE"]];
