'use strict';

goog.provide('Blockly.Blocks.nbiot');
goog.require('Blockly.Blocks');

Blockly.Blocks.nbiot.HUE="#6da383";

Blockly.Blocks['NBIoT_setup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_SETUP);
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}


Blockly.Blocks['NBIoT_http'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_TITLE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_APIKEY)
            .appendField(new Blockly.FieldTextInput("yourCloudEventAPIKey"),"APIKEY")
        this.appendValueInput("ADD"+0)
            .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
            .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
            .setAlign(Blockly.ALIGN_RIGHT);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.itemCount_ = 1;
        this.updateShape_();
        this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    mutationToDom: function() {
        var container = document.createElement('mutation'); 
        container.setAttribute('items', this.itemCount_);
        return container;
    },

    domToMutation: function(xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },

    decompose: function(workspace) {
        var containerBlock = workspace.newBlock('NBIoT_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('NBIoT_create_with_item');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },

    compose: function(containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');

        var connections = [];
        while (itemBlock) {
            connections.push(itemBlock.valueConnection_);
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }

        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_();
        for (var i = 0; i < this.itemCount_; i++) {
            Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
        }
    },

    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();
        }
    },

    updateShape_: function() {
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input =  this.appendValueInput("ADD"+i)
                    .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
                    .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
                    .setAlign(Blockly.ALIGN_RIGHT);
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
    onchange: function() {
        var text_content = this.getFieldValue("APIKEY");
        if (text_content.length > 50) {
          this.setFieldValue(text_content.substring(0,50),"APIKEY")
        }
    }
};
Blockly.Blocks['NBIoT_create_with_container'] = {

    init: function() {
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.BLYNK_CREATE_WITH_CONTAINER_TITLE_ADD);
        this.appendStatementInput('STACK');
        this.setTooltip('');
        this.contextMenu = false;
    }
};
Blockly.Blocks['NBIoT_create_with_item'] = {

    init: function() {
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.BLYNK_CREATE_WITH_ITEM_TITLE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
        this.contextMenu = false;
    }
};


Blockly.Blocks['NBIoT_enable'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_ENABLE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
}



Blockly.Blocks['NBIoT_http_thingspeak'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_THINGSPEAK_TITLE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_THINGSPEAK_APIKEY)
            .appendField(new Blockly.FieldTextInput("yourEventAPIKey"),"APIKEY")
        this.appendValueInput("ADD"+0)
            .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
            // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
            .setAlign(Blockly.ALIGN_RIGHT);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.itemCount_ = 1;
        this.updateShape_();
        this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    mutationToDom: function() {
        var container = document.createElement('mutation'); 
        container.setAttribute('items', this.itemCount_);
        return container;
    },

    domToMutation: function(xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },

    decompose: function(workspace) {
        var containerBlock = workspace.newBlock('NBIoT_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('NBIoT_create_with_item');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },

    compose: function(containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        var connections = [];
        while (itemBlock) {
            connections.push(itemBlock.valueConnection_);
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
        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_();
        for (var i = 0; i < this.itemCount_; i++) {
            Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
        }
    },

    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();
        }
    },

    updateShape_: function() {
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input =  this.appendValueInput("ADD"+i)
                    .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
                    // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
                    .setAlign(Blockly.ALIGN_RIGHT);
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
    onchange: function() {
        /** set limitation of AUTH input length */
        var text_content = this.getFieldValue("APIKEY");
        if (text_content.length > 50) {
          this.setFieldValue(text_content.substring(0,50),"APIKEY")
        }
    }
};



Blockly.Blocks['NBIoT_http_IFTTT'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 130, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_TITLE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_APIKEY)
            .appendField(new Blockly.FieldTextInput("yourEventAPIKey"),"APIKEY")
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_EVENT)
            .appendField(new Blockly.FieldTextInput("yourEventName"),"EVENT")
        this.appendValueInput("ADD"+0)
            .appendField(Blockly.Msg.NBIOT_HTTP_DATA+"0")
            // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
            .setAlign(Blockly.ALIGN_RIGHT);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.itemCount_ = 1;
        this.updateShape_();
        this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    mutationToDom: function() {
        var container = document.createElement('mutation'); 
        container.setAttribute('items', this.itemCount_);
        return container;
    },

    domToMutation: function(xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },

    decompose: function(workspace) {
        var containerBlock = workspace.newBlock('NBIoT_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('NBIoT_create_with_item');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },

    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);
        }
    },

    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();
        }
    },

    updateShape_: function() {

        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input =  this.appendValueInput("ADD"+i)
                    .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
                    // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
                    .setAlign(Blockly.ALIGN_RIGHT);
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
    onchange: function() {
        var text_content = this.getFieldValue("APIKEY");
        if (text_content.length > 50) {
          this.setFieldValue(text_content.substring(0,50),"APIKEY")
        }
    }
};



Blockly.Blocks['NBIoT_network_connected'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_NETWORK_CONNECTED)
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setOutput(true);
    }           
}

Blockly.Blocks['NBIoT_powered_on'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_IS_POWERON)
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setOutput(true);
    }           
}

Blockly.Blocks['NBIoT_network_IP'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_NETWORK_IP)
        this.setColour(Blockly.Blocks.nbiot.HUE);
        this.setOutput(true, Blockly.Types.TEXT.output);
    },
    getBlockType: function() {
        return Blockly.Types.TEXT;
    }      
}


Blockly.Blocks['NBIoT_http_cococloud_get'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_CLOUD_GET)
        this.appendDummyInput()
            .appendField(Blockly.Msg.NBIOT_HTTP_APIKEY)
            .appendField(new Blockly.FieldTextInput("yourCloudEventAPIKey"),"APIKEY")
        // this.setColour(Blockly.Blocks.nbiot.HUE);
        // this.setPreviousStatement(true);
        // this.setNextStatement(true);
        this.setOutput(true);
    }           
}