import Blockly from 'blockly';
import { pythonGenerator } from "blockly/python";
import CategoryColors from './define_color'

Blockly.Python = pythonGenerator

// variables
Blockly.texts = {
    HUE: "#33cc99"
}

Blockly.Blocks['text'] = {
    /**
     * Block for text value.
     * @this Blockly.Block
     */
    init: function () {
        this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
        this.setColour(Blockly.texts.HUE);
        this.appendDummyInput()
            .appendField(this.newQuote_(true))
            .appendField(new Blockly.FieldTextInput(''), 'TEXT')
            .appendField(this.newQuote_(false));
        this.setOutput(true, 'String');
        // 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, '"');
    }
};

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.texts.HUE);
        this.itemCount_ = 2;
        this.updateShape_();
        this.setOutput(true, 'String');
        this.setMutator(new Blockly.icons.MutatorIcon(['text_create_join_item'], this));
        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);
                }
            }
        }
        // Remove deleted inputs.
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
    newQuote_: Blockly.Blocks['text'].newQuote_
};
Blockly.Blocks['text_split_string_by_delimiter'] = {
    init: function () {
        this.appendValueInput("string_input")
            .setCheck(null)
            .appendField(Blockly.Msg.text_additional_delimiter_string);
        this.appendDummyInput()
            .appendField(Blockly.Msg.text_additional_delimiter_deli)
            .appendField(new Blockly.FieldTextInput(","), "delimiter")
            .appendField(Blockly.Msg.text_additional_delimiter_end);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.texts.HUE);
        this.setTooltip(Blockly.Msg.Text_Split_String_By_Delimiter_TOOLTIP);
        this.setHelpUrl("");
    }
};
Blockly.Blocks.text_format = {
    init: function () {
        this.jsonInit({
            inputsInline: !1,
            output: "String",
            colour: Blockly.texts.HUE,
            helpUrl: Blockly.Msg.TEXT_FORMAT_HELPURL,
            tooltip: Blockly.Msg.TEXT_FORMAT_TOOLTIP,
            message0: Blockly.Msg.TEXT_FORMAT_MESSAGE0,
            args0: [{
                type: "input_value",
                name: "FORMAT",
                check: "String"
            }, {
                type: "input_value",
                name: "CONTENT"
            }]
        })
    }
};
Blockly.Blocks.text_format2 = {
    init: function () {
        this.jsonInit({
            inputsInline: !1,
            output: "String",
            colour: Blockly.texts.HUE,
            helpUrl: Blockly.Msg.TEXT_FORMAT2_HELPURL,
            tooltip: Blockly.Msg.TEXT_FORMAT2_TOOLTIP,
            message0: Blockly.Msg.TEXT_FORMAT2_MESSAGE0,
            args0: [{
                type: "input_value",
                name: "FORMAT",
                check: "String"
            }, {
                type: "input_value",
                name: "CONTENT"
            }]
        })
    }
};
Blockly.Blocks.tuple_create_with_items_insert = {
    init: function () {
        this.setHelpUrl(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_HELPURL);
        this.setColour(Blockly.texts.HUE);
        this.itemCount_ = 3;
        this.updateShape_();
        this.setOutput(!0);
        this.setMutator(new Blockly.icons.MutatorIcon(["tuple_create_with_item"],this));
        this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_TOOLTIP)
    },
    mutationToDom: function () {
        var a = document.createElement("mutation");
        a.setAttribute("items", this.itemCount_);
        return a
    },
    domToMutation: function (a) {
        this.itemCount_ = parseInt(a.getAttribute("items"), 10);
        this.updateShape_()
    },
    decompose: function (a) {
        var b = a.newBlock("tuple_create_with_container");
        b.initSvg();
        for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
            var d = a.newBlock("tuple_create_with_item");
            d.initSvg();
            c.connect(d.previousConnection);
            c = d.nextConnection
        }
        return b
    },
    compose: function (a) {
        var b = a.getInputTargetBlock("STACK");
        for (a = []; b;)
            a.push(b.valueConnection_),
                b = b.nextConnection && b.nextConnection.targetBlock();
        for (b = 0; b < this.itemCount_; b++) {
            var c = this.getInput("ADD" + b).connection.targetConnection;
            c && -1 == a.indexOf(c) && c.disconnect()
        }
        this.itemCount_ = a.length;
        this.updateShape_();
        for (b = 0; b < this.itemCount_; b++)
            Blockly.Mutator.reconnect(a[b], this, "ADD" + b)
    },
    saveConnections: function (a) {
        a = a.getInputTargetBlock("STACK");
        for (var b = 0; a;) {
            var c = this.getInput("ADD" + b);
            a.valueConnection_ = c && c.connection.targetConnection;
            b++;
            a = a.nextConnection && a.nextConnection.targetBlock()
        }
    },
    updateShape_: function () {
        this.itemCount_ && this.getInput("EMPTY") ? this.removeInput("EMPTY") : this.itemCount_ || this.getInput("EMPTY") || this.appendDummyInput("EMPTY").appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
        for (var a = 0; a < this.itemCount_; a++)
            if (!this.getInput("ADD" + a)) {
                var b = this.appendValueInput("ADD" + a);
                0 == a && b.appendField(Blockly.Msg.TUPLE_CREATE_WITH_INPUT_WITH)
            }
        for (; this.getInput("ADD" + a);)
            this.removeInput("ADD" + a),
                a++
    }
};
Blockly.Blocks.tuple_create_with_item = {
    init: function () {
        this.setColour(Blockly.texts.HUE);
        this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TITLE);
        this.setPreviousStatement(!0);
        this.setNextStatement(!0);
        this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TOOLTIP);
        this.contextMenu = !1
    }
};
Blockly.Blocks.tuple_create_with_container = {
    init: function () {
        this.setColour(Blockly.texts.HUE);
        this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TITLE_ADD);
        this.appendStatementInput("STACK");
        this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TOOLTIP);
        this.contextMenu = !1
    }
};
// Blockly.Blocks.tuple_create_with = {
//     init: function () {
//         this.jsonInit({
//             colour: Blockly.texts.HUE,
//             args0: [{
//                 type: "input_value",
//                 name: "tuple_name"
//             }, {
//                 type: "input_value",
//                 name: "tuple_items"
//             }],
//             inputsInline: !0,
//             nextStatement: null,
//             previousStatement: null,
//             helpUrl: Blockly.Msg.tuple_create_with_HELPURL,
//             tooltip: Blockly.Msg.tuple_create_with_TOOLTIP,
//             message0: Blockly.Msg.tuple_create_with_MESSAGE0
//         })
//     }
// };
export default Blockly;