/**
 * @license
 * Visual Blocks Editor
 *
 * Copyright 2012 Google Inc.
 * https://developers.google.com/blockly/
 *
 * 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 Text blocks for Blockly.
 * @author fraser@google.com (Neil Fraser)
 */
'use strict';

goog.provide('Blockly.Blocks.texts');

goog.require('Blockly.Blocks');
goog.require('Blockly.Types');


/**
 * Common HSV hue for all blocks in this category.
 */
Blockly.Blocks.texts.HUE = 160;

Blockly.Blocks['char'] = {
    /**
     * Block for char value.
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl(Blockly.Msg.TEXT_CHAR_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendDummyInput()
            .appendField(this.newQuote_(true))
            .appendField(new Blockly.FieldTextInput(''), 'CHAR')
            .appendField(this.newQuote_(false));
        this.setOutput(true, Blockly.Types.CHARACTER.output);
        // Assign 'this' to a variable for use in the tooltip closure below.
        var thisBlock = this;
        // Text block is trivial.  Use tooltip of parent block if it exists.
        this.setTooltip(function () {
            var parent = thisBlock.getParent();
            return (parent && parent.getInputsInline() && parent.tooltip) ||
                Blockly.Msg.TEXT_CHAR_TOOLTIP;
        });
    },
    /**
     * Create an image of an open or closed quote.
     * @param {boolean} open True if open quote, false if closed.
     * @return {!Blockly.FieldImage} The field image of the quote.
     * @this Blockly.Block
     * @private
     */
    newQuote_: function (open) {
        if (open == this.RTL) {
            var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAKCAYAAACXDi8zAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAZUlEQVR42mNgAIL////7AHEJEIcAMSMDVBDEQQZTYRJt/zGBAEgiE4uEBkiCEYgTgHg2ED+GSmgyIAOgwG2oBCeyoCQQ/wXibmRBQajK70DMBhMsA+JfQPwSiLmQVXcBcTkDGgAAYj6aljihuoYAAAAASUVORK5CYII=';
        } else {
            var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAKCAYAAACXDi8zAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAXklEQVR42mNgQAL///8XA+KtQOyOLKj5HwLeA7EZTFAFKniOAc2IT0D8EYiZkAW9oKrD0FUXQSVWAvE0ILaESRT8xwQKIAk/LBLOIAlGLBKCMON4gTgDiEuAWBEkBgDRwppuX2Kh2gAAAABJRU5ErkJggg==';
        }
        return new Blockly.FieldImage(file, 6, 12, '"');
    },
    /** @return {!character} Type of the block, text block always a character. */
    getBlockType: function () {
        return Blockly.Types.CHARACTER;
    }
};

Blockly.Blocks['text'] = {
    /**
     * Block for text value.
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendDummyInput()
            .appendField(this.newQuote_(true))
            .appendField(new Blockly.FieldTextInput(''), 'TEXT')
            .appendField(this.newQuote_(false));
        this.setOutput(true, Blockly.Types.STRING.output);
        // Assign 'this' to a variable for use in the tooltip closure below.
        var thisBlock = this;
        // Text block is trivial.  Use tooltip of parent block if it exists.
        this.setTooltip(function () {
            var parent = thisBlock.getParent();
            return (parent && parent.getInputsInline() && parent.tooltip) ||
                Blockly.Msg.TEXT_TEXT_TOOLTIP;
        });
    },
    /**
     * Create an image of an open or closed quote.
     * @param {boolean} open True if open quote, false if closed.
     * @return {!Blockly.FieldImage} The field image of the quote.
     * @this Blockly.Block
     * @private
     */
    newQuote_: function (open) {
        if (open == this.RTL) {
            var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
        } else {
            var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
        }
        return new Blockly.FieldImage(file, 12, 12, '"');
    },
    /** @return {!string} Type of the block, text block always a string. */
    getBlockType: function () {
        return Blockly.Types.STRING;
    }
};

Blockly.Blocks['text_join'] = {
    /**
     * Block for creating a string made up of any number of elements of any type.
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.itemCount_ = 2;
        this.updateShape_();
        // Keep the concatenation inputs on one row.  Without this flag the
        // renderer puts the second slot (and its + marker) on a new line,
        // which makes the block harder to read and use in the flyout.
        this.setInputsInline(true);
        this.setOutput(true, Blockly.Types.STRING.output);
        if (Blockly.icons && Blockly.icons.MutatorIcon) {
            this.setMutator(new Blockly.icons.MutatorIcon(['text_create_join_item'], this));
        } else if (typeof Blockly.Mutator === 'function') {
            // Blockly 9 and older exposed the legacy Mutator constructor.
            this.setMutator(new Blockly.Mutator(['text_create_join_item']));
        }
        this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP);
    },
    /**
     * Create XML to represent number of text inputs.
     * @return {!Element} XML storage element.
     * @this Blockly.Block
     */
    mutationToDom: function () {
        var container = document.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
    },
    /**
     * Parse XML to restore the text inputs.
     * @param {!Element} xmlElement XML storage element.
     * @this Blockly.Block
     */
    domToMutation: function (xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },
    /**
     * Populate the mutator's dialog with this block's components.
     * @param {!Blockly.Workspace} workspace Mutator's workspace.
     * @return {!Blockly.Block} Root block in mutator.
     * @this Blockly.Block
     */
    decompose: function (workspace) {
        var containerBlock = workspace.newBlock('text_create_join_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('text_create_join_item');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },
    /**
     * Reconfigure this block based on the mutator dialog's components.
     * @param {!Blockly.Block} containerBlock Root block in mutator.
     * @this Blockly.Block
     */
    compose: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        // Count number of inputs.
        var connections = [];
        while (itemBlock) {
            connections.push(itemBlock.valueConnection_);
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
        // Disconnect any children that don't belong.
        for (var i = 0; i < this.itemCount_; i++) {
            var connection = this.getInput('ADD' + i).connection.targetConnection;
            if (connection && connections.indexOf(connection) == -1) {
                connection.disconnect();
            }
        }
        this.itemCount_ = connections.length;
        this.updateShape_();
        // Reconnect any child blocks.
        for (var i = 0; i < this.itemCount_; i++) {
            Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
        }
    },
    /**
     * Store pointers to any connected child blocks.
     * @param {!Blockly.Block} containerBlock Root block in mutator.
     * @this Blockly.Block
     */
    saveConnections: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        var i = 0;
        while (itemBlock) {
            var input = this.getInput('ADD' + i);
            itemBlock.valueConnection_ = input && input.connection.targetConnection;
            i++;
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
    },
    /**
     * Modify this block to have the correct number of inputs.
     * @private
     * @this Blockly.Block
     */
    updateShape_: function () {
        if (this.itemCount_ && this.getInput('EMPTY')) {
            this.removeInput('EMPTY');
        } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
            this.appendDummyInput('EMPTY')
                .appendField(this.newQuote_(true))
                .appendField(this.newQuote_(false));
        }
        // Add new inputs.
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input = this.appendValueInput('ADD' + i);
                if (i == 0) {
                    input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
                } else {
                    // Make the concatenation operation explicit in the stacked
                    // layout, especially when the block is shown in the flyout.
                    input.appendField('+');
                }
            }
        }
        // Remove deleted inputs.
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
    newQuote_: Blockly.Blocks['text'].newQuote_,
    /** @return {!string} Type of the block, text join always a string. */
    getBlockType: function () {
        return Blockly.Types.STRING;
    }
};

Blockly.Blocks['text_create_join_container'] = {
    /**
     * Mutator block for container.
     * @this Blockly.Block
     */
    init: function () {
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN);
        this.appendStatementInput('STACK');
        this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP);
        this.contextMenu = false;
    }
};

Blockly.Blocks['text_create_join_item'] = {
    /**
     * Mutator block for add items.
     * @this Blockly.Block
     */
    init: function () {
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP);
        this.contextMenu = false;
    }
};

Blockly.Blocks['text_append'] = {
    /**
     * Block for appending to a variable in place.
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl(Blockly.Msg.TEXT_APPEND_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('TEXT')
            .appendField(Blockly.Msg.TEXT_APPEND_TO)
            .appendField(new Blockly.FieldVariable(
                Blockly.Msg.TEXT_APPEND_VARIABLE), 'VAR')
            .appendField(Blockly.Msg.TEXT_APPEND_APPENDTEXT);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        // Assign 'this' to a variable for use in the tooltip closure below.
        var thisBlock = this;
        this.setTooltip(function () {
            return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1',
                thisBlock.getFieldValue('VAR'));
        });
    },
    /**
     * Set's the type of the variable selected in the drop down list. As there is
     * only one possible option, the variable input is not really checked.
     * @param {!string} varName Name of the variable to check type.
     * @return {string} String to indicate the variable type.
     */
    getVarType: function (varName) {
        return Blockly.Types.STRING;
    }
};

Blockly.Blocks['text_length'] = {
    /**
     * Block for string length.
     * @this Blockly.Block
     */
    init: function () {
        this.jsonInit({
            "message0": Blockly.Msg.TEXT_LENGTH_TITLE,
            "args0": [
                {
                    "type": "input_value",
                    "name": "VALUE",
                    "check": Blockly.Types.STRING.checkList.concat('Array'),
                }
            ],
            "output": Blockly.Types.NUMBER.output,
            "colour": Blockly.Blocks.texts.HUE,
            "tooltip": Blockly.Msg.TEXT_LENGTH_TOOLTIP,
            "helpUrl": Blockly.Msg.TEXT_LENGTH_HELPURL
        });
    },
    /** @return {!string} Type of the block, text length always an integer. */
    getBlockType: function () {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['text_isEmpty'] = {
    /**
     * Block for is the string null?
     * @this Blockly.Block
     */
    init: function () {
        this.jsonInit({
            "message0": Blockly.Msg.TEXT_ISEMPTY_TITLE,
            "args0": [
                {
                    "type": "input_value",
                    "name": "VALUE",
                    "check": Blockly.Types.STRING.checkList.concat('Array'),
                }
            ],
            "output": Blockly.Types.BOOLEAN.output,
            "colour": Blockly.Blocks.texts.HUE,
            "tooltip": Blockly.Msg.TEXT_ISEMPTY_TOOLTIP,
            "helpUrl": Blockly.Msg.TEXT_ISEMPTY_HELPURL
        });
    },
    /** @return {!string} Type of the block, check always returns a boolean. */
    getBlockType: function () {
        return Blockly.Types.BOOLEAN;
    }
};

Blockly.Blocks['text_indexOf'] = {
    /**
     * Block for finding a substring in the text.
     * @this Blockly.Block
     */
    init: function () {
        var OPERATORS =
            [[Blockly.Msg.TEXT_INDEXOF_OPERATOR_FIRST, 'FIRST'],
                [Blockly.Msg.TEXT_INDEXOF_OPERATOR_LAST, 'LAST']];
        this.setHelpUrl(Blockly.Msg.TEXT_INDEXOF_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.appendValueInput('VALUE')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(Blockly.Msg.TEXT_INDEXOF_INPUT_INTEXT);
        this.appendValueInput('FIND')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
        if (Blockly.Msg.TEXT_INDEXOF_TAIL) {
            this.appendDummyInput().appendField(Blockly.Msg.TEXT_INDEXOF_TAIL);
        }
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.TEXT_INDEXOF_TOOLTIP);
    }
};

Blockly.Blocks['text_charAt'] = {
    /**
     * Block for getting a character from the string.
     * @this Blockly.Block
     */
    init: function () {
        this.WHERE_OPTIONS =
            [[Blockly.Msg.TEXT_CHARAT_FROM_START, 'FROM_START'],
                [Blockly.Msg.TEXT_CHARAT_FROM_END, 'FROM_END'],
                [Blockly.Msg.TEXT_CHARAT_FIRST, 'FIRST'],
                [Blockly.Msg.TEXT_CHARAT_LAST, 'LAST'],
                [Blockly.Msg.TEXT_CHARAT_RANDOM, 'RANDOM']];
        this.setHelpUrl(Blockly.Msg.TEXT_CHARAT_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.setOutput(true, Blockly.Types.STRING.output);
        this.appendValueInput('VALUE')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(Blockly.Msg.TEXT_CHARAT_INPUT_INTEXT);
        this.appendDummyInput('AT');
        this.setInputsInline(true);
        this.updateAt_(true);
        this.setTooltip(Blockly.Msg.TEXT_CHARAT_TOOLTIP);
    },
    /**
     * Create XML to represent whether there is an 'AT' input.
     * @return {!Element} XML storage element.
     * @this Blockly.Block
     */
    mutationToDom: function () {
        var container = document.createElement('mutation');
        var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
        container.setAttribute('at', isAt);
        return container;
    },
    /**
     * Parse XML to restore the 'AT' input.
     * @param {!Element} xmlElement XML storage element.
     * @this Blockly.Block
     */
    domToMutation: function (xmlElement) {
        // Note: Until January 2013 this block did not have mutations,
        // so 'at' defaults to true.
        var isAt = (xmlElement.getAttribute('at') != 'false');
        this.updateAt_(isAt);
    },
    /**
     * Create or delete an input for the numeric index.
     * @param {boolean} isAt True if the input should exist.
     * @private
     * @this Blockly.Block
     */
    updateAt_: function (isAt) {
        // Destroy old 'AT' and 'ORDINAL' inputs.
        this.removeInput('AT');
        this.removeInput('ORDINAL', true);
        // Create either a value 'AT' input or a dummy input.
        if (isAt) {
            this.appendValueInput('AT').setCheck(Blockly.Types.NUMBER.checkList);
            if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
                this.appendDummyInput('ORDINAL')
                    .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
            }
        } else {
            this.appendDummyInput('AT');
        }
        if (Blockly.Msg.TEXT_CHARAT_TAIL) {
            this.removeInput('TAIL', true);
            this.appendDummyInput('TAIL')
                .appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
        }
        var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function (value) {
            var newAt = (value == 'FROM_START') || (value == 'FROM_END');
            // The 'isAt' variable is available due to this function being a closure.
            if (newAt != isAt) {
                var block = this.sourceBlock_;
                block.updateAt_(newAt);
                // This menu has been destroyed and replaced.  Update the replacement.
                block.setFieldValue(value, 'WHERE');
                return null;
            }
            return undefined;
        });
        this.getInput('AT').appendField(menu, 'WHERE');
    }
};

Blockly.Blocks['text_getSubstring'] = {
    /**
     * Block for getting substring.
     * @this Blockly.Block
     */
    init: function () {
        this['WHERE_OPTIONS_1'] =
            [[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_START, 'FROM_START'],
                [Blockly.Msg.TEXT_GET_SUBSTRING_START_FIRST, 'FIRST']];
        this['WHERE_OPTIONS_2'] =
            [[Blockly.Msg.TEXT_GET_SUBSTRING_LENGTH, 'LENGTH'],
                [Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_START, 'FROM_START'],
                [Blockly.Msg.TEXT_GET_SUBSTRING_END_LAST, 'LAST']];
        this.setHelpUrl(Blockly.Msg.TEXT_GET_SUBSTRING_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('STRING')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_INPUT_IN_TEXT);
        this.appendDummyInput('AT1');
        this.appendDummyInput('AT2');
        if (Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
            this.appendDummyInput('TAIL')
                .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
        }
        this.setInputsInline(true);
        this.setOutput(true, Blockly.Types.STRING.output);
        this.updateAt_(1, true);
        this.updateAt_(2, true);
        this.setTooltip(Blockly.Msg.TEXT_GET_SUBSTRING_TOOLTIP);
    },
    /**
     * Create XML to represent whether there are 'AT' inputs.
     * @return {!Element} XML storage element.
     * @this Blockly.Block
     */
    mutationToDom: function () {
        var container = document.createElement('mutation');
        var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
        container.setAttribute('at1', isAt1);
        var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
        container.setAttribute('at2', isAt2);
        return container;
    },
    /**
     * Parse XML to restore the 'AT' inputs.
     * @param {!Element} xmlElement XML storage element.
     * @this Blockly.Block
     */
    domToMutation: function (xmlElement) {
        var isAt1 = (xmlElement.getAttribute('at1') == 'true');
        var isAt2 = (xmlElement.getAttribute('at2') == 'true');
        this.updateAt_(1, isAt1);
        this.updateAt_(2, isAt2);
    },
    /**
     * Create or delete an input for a numeric index.
     * This block has two such inputs, independant of each other.
     * @param {number} n Specify first or second input (1 or 2).
     * @param {boolean} isAt True if the input should exist.
     * @private
     * @this Blockly.Block
     */
    updateAt_: function (n, isAt) {
        // Create or delete an input for the numeric index.
        // Destroy old 'AT' and 'ORDINAL' inputs.
        this.removeInput('AT' + n);
        this.removeInput('ORDINAL' + n, true);
        // Create either a value 'AT' input or a dummy input.
        if (isAt) {
            this.appendValueInput('AT' + n).setCheck(Blockly.Types.NUMBER.checkList);
            if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
                this.appendDummyInput('ORDINAL' + n)
                    .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
            }
        } else {
            this.appendDummyInput('AT' + n);
        }
        // Move tail, if present, to end of block.
        if (n == 2 && Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
            this.removeInput('TAIL', true);
            this.appendDummyInput('TAIL')
                .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
        }
        var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
            function (value) {
                var newAt = (value == 'FROM_START') || (value == 'FROM_END') || (value == 'LENGTH');
                // The 'isAt' variable is available due to this function being a closure.
                if (newAt != isAt) {
                    var block = this.sourceBlock_;
                    block.updateAt_(n, newAt);
                    // This menu has been destroyed and replaced.  Update the replacement.
                    block.setFieldValue(value, 'WHERE' + n);
                    return null;
                }
                return undefined;
            });
        this.getInput('AT' + n)
            .appendField(menu, 'WHERE' + n);
        if (n == 1) {
            this.moveInputBefore('AT1', 'AT2');
        }
    }
};

Blockly.Blocks['text_changeCase'] = {
    /**
     * Block for changing capitalization.
     * @this Blockly.Block
     */
    init: function () {
        var OPERATORS =
            [[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_UPPERCASE, 'UPPERCASE'],
                [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_LOWERCASE, 'LOWERCASE'],
                [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_TITLECASE, 'TITLECASE']];
        this.setHelpUrl(Blockly.Msg.TEXT_CHANGECASE_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('TEXT')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(new Blockly.FieldDropdown(OPERATORS), 'CASE');
        this.setOutput(true, Blockly.Types.STRING.output);
        this.setTooltip(Blockly.Msg.TEXT_CHANGECASE_TOOLTIP);
    }
};

Blockly.Blocks['text_trim'] = {
    /**
     * Block for trimming spaces.
     * @this Blockly.Block
     */
    init: function () {
        var OPERATORS =
            [[Blockly.Msg.TEXT_TRIM_OPERATOR_BOTH, 'BOTH'],
                [Blockly.Msg.TEXT_TRIM_OPERATOR_LEFT, 'LEFT'],
                [Blockly.Msg.TEXT_TRIM_OPERATOR_RIGHT, 'RIGHT']];
        this.setHelpUrl(Blockly.Msg.TEXT_TRIM_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('TEXT')
            .setCheck(Blockly.Types.STRING.checkList)
            .appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
        this.setOutput(true, Blockly.Types.STRING.output);
        this.setTooltip(Blockly.Msg.TEXT_TRIM_TOOLTIP);
    },
    /** Assigns a type to the block, trim always takes and returns a string. */
    getBlockType: function () {
        return Blockly.Types.STRING;
    }
};

Blockly.Blocks['text_print'] = {
    /**
     * Block for print statement.
     * @this Blockly.Block
     */
    init: function () {
        this.jsonInit({
            "message0": Blockly.Msg.TEXT_PRINT_TITLE,
            "args0": [
                {
                    "type": "input_value",
                    "name": "TEXT"
                }
            ],
            "previousStatement": null,
            "nextStatement": null,
            "colour": Blockly.Blocks.texts.HUE,
            "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
            "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
        });
    }
};

Blockly.Blocks['text_prompt_ext'] = {
    /**
     * Block for prompt function (external message).
     * @this Blockly.Block
     */
    init: function () {
        var TYPES =
            [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, Blockly.Types.STRING.output],
                [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, Blockly.Types.NUMBER.output]];
        this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        // Assign 'this' to a variable for use in the closures below.
        var thisBlock = this;
        var dropdown = new Blockly.FieldDropdown(TYPES, function (newOp) {
            thisBlock.updateType_(newOp);
        });
        this.appendValueInput('TEXT')
            .appendField(dropdown, 'TYPE');
        this.setOutput(true, Blockly.Types.STRING.output);
        this.setTooltip(function () {
            return (thisBlock.getFieldValue('TYPE') == Blockly.Types.STRING.output) ?
                Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
                Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
        });
    },
    /**
     * Modify this block to have the correct output type.
     * @param {string} newOp Either 'TEXT' or 'NUMBER'.
     * @private
     * @this Blockly.Block
     */
    updateType_: function (newOp) {
        this.outputConnection.setCheck(newOp == Blockly.Types.NUMBER.output ?
            Blockly.Types.NUMBER.checkList : Blockly.Types.STRING.checkList);
    },
    /**
     * Create XML to represent the output type.
     * @return {!Element} XML storage element.
     * @this Blockly.Block
     */
    mutationToDom: function () {
        var container = document.createElement('mutation');
        container.setAttribute('type', this.getFieldValue('TYPE'));
        return container;
    },
    /**
     * Parse XML to restore the output type.
     * @param {!Element} xmlElement XML storage element.
     * @this Blockly.Block
     */
    domToMutation: function (xmlElement) {
        this.updateType_(xmlElement.getAttribute('type'));
    },
    /** @return {!string} Type of the block, prompt always returns a string. */
    getBlockType: function () {
        return (this.getFieldValue('TYPE') == Blockly.Types.STRING.output) ?
            Blockly.Types.STRING : Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['text_prompt'] = {
    /**
     * Block for prompt function (internal message).
     * The 'text_prompt_ext' block is preferred as it is more flexible.
     * @this Blockly.Block
     */
    init: function () {
        var TYPES =
            [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, Blockly.Types.STRING.output],
                [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, Blockly.Types.NUMBER.output]];
        // Assign 'this' to a variable for use in the closure below.
        var thisBlock = this;
        this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
        this.setColour(Blockly.Blocks.texts.HUE);
        var dropdown = new Blockly.FieldDropdown(TYPES, function (newOp) {
            thisBlock.updateType_(newOp);
        });
        this.appendDummyInput()
            .appendField(dropdown, 'TYPE')
            .appendField(this.newQuote_(true))
            .appendField(new Blockly.FieldTextInput(''), 'TEXT')
            .appendField(this.newQuote_(false));
        this.setOutput(true, Blockly.Types.STRING.output);
        this.setTooltip(function () {
            return (thisBlock.getFieldValue('TYPE') == Blockly.Types.STRING.output) ?
                Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
                Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
        });
    },
    newQuote_: Blockly.Blocks['text'].newQuote_,
    updateType_: Blockly.Blocks['text_prompt_ext'].updateType_,
    mutationToDom: Blockly.Blocks['text_prompt_ext'].mutationToDom,
    domToMutation: Blockly.Blocks['text_prompt_ext'].domToMutation,
    /** Assigns a type to the block, prompt always returns a string. */
    getBlockType: function () {
        return (this.getFieldValue('TYPE') == Blockly.Types.NUMBER.output) ?
            Blockly.Types.NUMBER : Blockly.Types.STRING;
    }
};

Blockly.Blocks['string_hex'] = {
    init: function () {
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput("CONTENT", Number);
        this.appendDummyInput()
            .appendField(Blockly.Msg.TEXT_TO_HEX)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.TEXT_HEX, "HEX"],
                [Blockly.Msg.TEXT_BIN, "BIN"],
                [Blockly.Msg.TEXT_DEC, "DEC"]
            ]), "STAT")
            .appendField(Blockly.Msg.VARIABLES_TYPE_STRING);
        this.setOutput(true, null);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.TEXT_TO_HEX_TOOLTIP);
    },
    /** Assigns a type to the block, prompt always returns a string. */
    getBlockType: function () {
        return Blockly.Types.STRING;
    }
};

Blockly.Blocks['text_toint'] = {
    init: function () {
        this.appendValueInput("TEXT")
            .appendField(Blockly.Msg.VARIABLES_TYPE_STRING);
        this.appendDummyInput()
            .appendField(Blockly.Msg.VARIABLES_AS + Blockly.Msg.VARIABLES_TYPE_NUMBER);
        this.setOutput(true, null);
        this.setInputsInline(true);
        this.setColour(Blockly.Blocks.texts.HUE);
        this.setTooltip('');
    },
    /** Assigns a type to the block, prompt always returns a string. */
    getBlockType: function () {
        return Blockly.Types.NUMBER;
    }
};
