"use strict";

goog.provide("Blockly.Blocks.bluetooth");

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

Blockly.Blocks.bluetooth.HUE = 230;

Blockly.Blocks["bluetooth_setup"] = {
  init: function() {
    this.appendDummyInput()
      // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_bluetooth.png", 140, 40, "0"));
      .appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth.png",50,40,"0"));
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_SETUP)
      .appendField(
        new Blockly.FieldDropdown([
          [Blockly.Msg.BLUETOOTH_SETUP_OPT1, "string"],
          [Blockly.Msg.BLUETOOTH_SETUP_OPT2, "number"],
          [Blockly.Msg.BLUETOOTH_SETUP_OPT3, "array"]
        ]),
        "OPTIONS"
      );
    this.appendDummyInput("VAR")
      .appendField(Blockly.Msg.BLUETOOTH_SETUP_DATA_SET)
      .appendField(new Blockly.FieldVariable("command"), "CMDVAR")
      .appendField(Blockly.Msg.BLUETOOTH_SETUP_TO);
    this.appendDummyInput("BAUDSETTING")
      .appendField(Blockly.Msg.BLUETOOTH_SETUP_SET)
      .appendField(
        new Blockly.FieldDropdown([["38400", "38400"], ["9600", "9600"]]),
        "BAUD"
      );
    this.appendStatementInput("RECEIVED_DO")
      .setCheck(null)
      .appendField(Blockly.Msg.BLUETOOTH_SETUP_DO);
    this.getVarType();
    this.setColour(Blockly.Blocks.bluetooth.HUE);
    this.setTooltip("");
    this.setHelpUrl("");
  },

  getVarType: function(varName) {
    let vartype = Blockly.Types.UNDEF;
    if (this && this.getFieldValue("OPTIONS") === "string") {
      vartype = Blockly.Types.TEXT;
    } else if (this && this.getFieldValue("OPTIONS") === "number") {
      vartype = Blockly.Types.NUMBER;
    } else if (this && this.getFieldValue("OPTIONS") === "array") {
      vartype = Blockly.Types.ARRAY;
    }
    return vartype;
  },

  onchange: function() {
    if (this && this.getFieldValue("OPTIONS") === "array") {
      if (!this.getInput("VARLENGTH")) {
        this.appendDummyInput("VARLENGTH")
          .appendField(Blockly.Msg.BLUETOOTH_SETUP_LIST_SETTING)
          .appendField(
            new Blockly.FieldDropdown([
              ["1", "1"],
              ["2", "2"],
              ["3", "3"],
              ["4", "4"],
              ["5", "5"],
              ["6", "6"],
              ["7", "7"],
              ["8", "8"],
              ["9", "9"],
              ["10", "10"]
            ]),
            "LENGTH"
          );
        this.moveInputBefore("VARLENGTH", "BAUDSETTING");
      }
    } else {
      if (this.getInput("VARLENGTH")) this.removeInput("VARLENGTH");
    }
  }
};

Blockly.Blocks["bluetooth_text_getCommand"] = {
  init: function() {
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_TEXT_RECEIVED)
      .appendField(new Blockly.FieldVariable("command"), "VAR");
    this.setColour(Blockly.Blocks.bluetooth.HUE);
    this.setOutput(true);
    this.setTooltip("");
    this.setHelpUrl("");
  },

  customContextMenu: function(options) {
    var option = { enabled: true };
    var name = this.getFieldValue("VAR");
    option.text = this.contextMenuMsg_.replace("%1", name);
    var xmlField = goog.dom.createDom("field", null, name);
    xmlField.setAttribute("name", "VAR");
    var xmlBlock = goog.dom.createDom("block", null, xmlField);
    xmlBlock.setAttribute("type", this.contextMenuType_);
    option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
    options.push(option);
  },

  getBlockType: function() {
    return Blockly.Types.TEXT;
  },

  getVarType: function(varName) {
    return Blockly.Types.TEXT;
  }
};

Blockly.Blocks["bluetooth_number_getCommand"] = {
  init: function() {
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_NUMBER_RECEIVED)
      .appendField(new Blockly.FieldVariable("command"), "VAR");
    this.setColour(Blockly.Blocks.bluetooth.HUE);
    this.setOutput(true);
    this.setTooltip("");
    this.setHelpUrl("");
  },

  customContextMenu: function(options) {
    var option = { enabled: true };
    var name = this.getFieldValue("VAR");
    option.text = this.contextMenuMsg_.replace("%1", name);
    var xmlField = goog.dom.createDom("field", null, name);
    xmlField.setAttribute("name", "VAR");
    var xmlBlock = goog.dom.createDom("block", null, xmlField);
    xmlBlock.setAttribute("type", this.contextMenuType_);
    option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
    options.push(option);
  },

  getBlockType: function() {
    return Blockly.Types.NUMBER;
  },

  getVarType: function(varName) {
    return Blockly.Types.NUMBER;
  }
};

Blockly.Blocks["bluetooth_list_getCommand"] = {
  init: function() {
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED)
      .appendField(new Blockly.FieldVariable("command"), "VAR")
      .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED1)
      .appendField(
        new Blockly.FieldDropdown([
          ["0", "0"],
          ["1", "1"],
          ["2", "2"],
          ["3", "3"],
          ["4", "4"],
          ["5", "5"],
          ["6", "6"],
          ["7", "7"],
          ["8", "8"],
          ["9", "9"],
        ]),
        "INDEX"
      )
      .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED2);
    this.setColour(Blockly.Blocks.bluetooth.HUE);
    this.setOutput(true);
    this.setTooltip("");
    this.setHelpUrl("");
  },
  customContextMenu: function(options) {
    var option = { enabled: true };
    var name = this.getFieldValue("VAR");
    option.text = this.contextMenuMsg_.replace("%1", name);
    var xmlField = goog.dom.createDom("field", null, name);
    xmlField.setAttribute("name", "VAR");
    var xmlBlock = goog.dom.createDom("block", null, xmlField);
    xmlBlock.setAttribute("type", this.contextMenuType_);
    option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
    options.push(option);
  },

  getBlockType: function() {
    return Blockly.Types.NUMBER;
  },

  getVarType: function(varName) {
    return Blockly.Types.NUMBER;
  }
};


/*bluetooth intercommunication*/
Blockly.Blocks["bluetooth_intercomms_send"] = {
  init: function() {
    this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
    // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/main-bluetooth_comm.png", 50, 40, "0"));
    this.appendDummyInput()
      .appendField("New "+Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
      .appendField(new Blockly.FieldDropdown([
          ["9600","9600"],
          ["38400","38400"]]),"BAUD")
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SENDMSG)
    this.appendValueInput("ADD0")
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA)
      .setAlign(Blockly.ALIGN_RIGHT)
    // this.setColour(215);
    this.setColour(145);
    this.setTooltip("");
    this.setHelpUrl("");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.itemCount_ = 1;
    this.updateShape_();
    this.setMutator(new Blockly.Mutator(['bluetooth_intercomms_create_with_item']));
  },
  mutationToDom: function() {
        var container = document.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
  },
    /**
     * Parse XML to restore the 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('bluetooth_intercomms_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('bluetooth_intercomms_create_with_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() {
        // Add new inputs.
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input = this.appendValueInput('ADD' + i)
                     .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA + i)
                    .setAlign(Blockly.ALIGN_RIGHT)
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
};
Blockly.Blocks['bluetooth_intercomms_create_with_container'] = {
    /**
     * Mutator block for list container.
     * @this Blockly.Block
     */
    init: function() {
        this.setColour(215);
        this.appendDummyInput()
            .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
        this.appendStatementInput('STACK');
        this.setTooltip('');
        this.contextMenu = false;
    }
};
Blockly.Blocks['bluetooth_intercomms_create_with_item'] = {
    /**
     * Mutator bolck for adding items.
     * @this Blockly.Block
     */
    init: function() {
        this.setColour(215);
        this.appendDummyInput()
            .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
        this.contextMenu = false;
    }
};


/*receive message from  bluetooth to bluetooth */
Blockly.Blocks["bluetooth_intercomms_receive"] = {
  init: function() {
    this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
    this.appendDummyInput()
      .appendField("New "+Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
      .appendField(new Blockly.FieldDropdown([
          ["9600","9600"],
          ["38400","38400"]]),"BAUD")
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_TIMEOUT)
      .appendField(new Blockly.FieldTextInput("50"),"TIMEOUT")
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVEMSG)
    this.appendDummyInput()
      .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_LINE3)
      .appendField(new Blockly.FieldDropdown([
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
                ["7", "7"],
                ["8", "8"],
                ["9", "9"],
                ["10", "10"],
                ["11", "11"],
                ["12", "12"],
            ]), "LENGTH");
    // this.setColour("215");
    this.setColour(145);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    // this.setOutput(true,Blockly.Types.BOOLEAN.output);
    this.setTooltip("");
    this.setHelpUrl("");
  },
  getVarType: function(varName) {
    return Blockly.Types.ARRAY;
  },
  getBlockType: function() {
        var type = this.getFieldValue("TYPE");
        switch (type) {
            case "" :
                return Blockly.Types.TEXT;
            case ".toInt()":
                return Blockly.Types.NUMBER;
            case ".toFloat()":
                return Blockly.Types.LARGE_NUMBER;
        }
        return Blockly.Types.NUMBER;
    },
};

/*check if sucessfully receive message from bluetooth to bluetooth */
Blockly.Blocks["bluetooth_intercomms_receive_getData"] = {
  init: function() {
      this.appendDummyInput()
         // .appendField(" " + Blockly.Msg.MAINEASYMODE_TRANSFER_RECEIVE_GET)
         // .appendField(new Blockly.FieldDropdown([
         //     ["rxData", "rxData"]
         // ]), "DATAMAIN")
         .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2)
         .appendField(new Blockly.FieldDropdown([
             ["0", "0"],
             ["1", "1"],
             ["2", "2"],
             ["3", "3"],
             ["4", "4"],
             ["5", "5"],
             ["6", "6"],
             ["7", "7"],
             ["8", "8"],
             ["9", "9"],
             ["10", "10"],
             ["11", "11"],
         ]), "INDEX")
         .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE3)
         .appendField(new Blockly.FieldDropdown([
             ["String", ""],
             ["Number", ".toInt()"],
             ["Float", ".toFloat()"],
         ]), "TYPE")
         .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE4)
      // this.setColour("215");
      this.setColour(145);
      this.setOutput(true)
        // this.setInputsInline(true);
      this.setTooltip("");
      this.setHelpUrl("");
    },
    getBlockType: function() {
        var type = this.getFieldValue("TYPE");
        switch (type) {
            case "" :
                return Blockly.Types.TEXT;
            case ".toInt()":
                return Blockly.Types.NUMBER;
            case ".toFloat()":
                return Blockly.Types.LARGE_NUMBER;
        }
        return Blockly.Types.NUMBER;
    },
    getOutputType: function() {
        var type = this.getFieldValue("TYPE");
        switch (type) {
            case "" :
                return Blockly.Types.TEXT.output
            case ".toInt()":
                return Blockly.Types.NUMBER.output;
            case ".toFloat()":
                return Blockly.Types.LARGE_NUMBER.output;
        }
        return Blockly.Types.NUMBER.output;
    }
};


/************************************************* */
/*bluetooth intercommunication*/
Blockly.Blocks["bluetooth_intercomms_send_old"] = {
  init: function() {
    this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
    // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/main-bluetooth_comm.png", 50, 40, "0"));
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
      .appendField(new Blockly.FieldDropdown([
          ["38400","38400"],
          ["9600","9600"]]),"BAUD")
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SENDMSG)
    this.appendValueInput("ADD0")
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA)
      .setAlign(Blockly.ALIGN_RIGHT)
    this.setColour(215);
    this.setTooltip("");
    this.setHelpUrl("");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.itemCount_ = 1;
    this.updateShape_();
    this.setMutator(new Blockly.Mutator(['bluetooth_intercomms_create_with_item']));
  },
  mutationToDom: function() {
        var container = document.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
  },
    /**
     * Parse XML to restore the 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('bluetooth_intercomms_create_with_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('bluetooth_intercomms_create_with_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() {
        // Add new inputs.
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('ADD' + i)) {
                var input = this.appendValueInput('ADD' + i)
                     .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA + i)
                    .setAlign(Blockly.ALIGN_RIGHT)
            }
        }
        while (this.getInput('ADD' + i)) {
            this.removeInput('ADD' + i);
            i++;
        }
    },
};
Blockly.Blocks['bluetooth_intercomms_create_with_container'] = {
    /**
     * Mutator block for list container.
     * @this Blockly.Block
     */
    init: function() {
        this.setColour(215);
        this.appendDummyInput()
            .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
        this.appendStatementInput('STACK');
        this.setTooltip('');
        this.contextMenu = false;
    }
};
Blockly.Blocks['bluetooth_intercomms_create_with_item'] = {
    /**
     * Mutator bolck for adding items.
     * @this Blockly.Block
     */
    init: function() {
        this.setColour(215);
        this.appendDummyInput()
            .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
        this.contextMenu = false;
    }
};


/*receive message from  bluetooth to bluetooth */
Blockly.Blocks["bluetooth_intercomms_receive_old"] = {
  init: function() {
    this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
      .appendField(new Blockly.FieldDropdown([
          ["38400","38400"],
          ["9600","9600"]]),"BAUD")
    // this.appendDummyInput()
    //   .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_TIMEOUT)
    //   .appendField(new Blockly.FieldTextInput("50"),"TIMEOUT")
    this.appendDummyInput()
      .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVEMSG)
    this.appendDummyInput()
      .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_LINE3)
      .appendField(new Blockly.FieldDropdown([
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
                ["7", "7"],
                ["8", "8"],
                ["9", "9"],
                ["10", "10"],
                ["11", "11"],
                ["12", "12"],
            ]), "LENGTH");
    this.setColour("215");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    // this.setOutput(true,Blockly.Types.BOOLEAN.output);
    this.setTooltip("");
    this.setHelpUrl("");
  },
  getVarType: function(varName) {
    return Blockly.Types.ARRAY;
  }
};

/*check if sucessfully receive message from bluetooth to bluetooth */
Blockly.Blocks["bluetooth_intercomms_receive_getData_old"] = {
  init: function() {
      this.appendDummyInput()
         // .appendField(" " + Blockly.Msg.MAINEASYMODE_TRANSFER_RECEIVE_GET)
         // .appendField(new Blockly.FieldDropdown([
         //     ["rxData", "rxData"]
         // ]), "DATAMAIN")
         .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2)
         .appendField(new Blockly.FieldDropdown([
             ["0", "0"],
             ["1", "1"],
             ["2", "2"],
             ["3", "3"],
             ["4", "4"],
             ["5", "5"],
             ["6", "6"],
             ["7", "7"],
             ["8", "8"],
             ["9", "9"],
             ["10", "10"],
             ["11", "11"],
         ]), "INDEX")
         .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2_2)
        //  .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE3)
        //  .appendField(new Blockly.FieldDropdown([
        //      ["String", ""],
        //      ["Number", ".toInt()"],
        //      ["Float", ".toFloat()"],
        //  ]), "TYPE")
        //  .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE4)
      this.setColour("215");
      this.setOutput(true,Blockly.Types.NUMBER.output)
        // this.setInputsInline(true);
      this.setTooltip("");
      this.setHelpUrl("");
    },
    getBlockType: function() {
        // var type = this.getFieldValue("TYPE");
        // switch (type) {
        //     case "" :
        //         return Blockly.Types.TEXT;
        //     case ".toInt()":
        //         return Blockly.Types.NUMBER;
        //     case ".toFloat()":
        //         return Blockly.Types.LARGE_NUMBER;
        // }
        return Blockly.Types.NUMBER;
    },
    // getOutputType: function() {
    //     var type = this.getFieldValue("TYPE");
    //     switch (type) {
    //         case "" :
    //             return Blockly.Types.TEXT.output
    //         case ".toInt()":
    //             return Blockly.Types.NUMBER.output;
    //         case ".toFloat()":
    //             return Blockly.Types.LARGE_NUMBER.output;
    //     }
    //     return Blockly.Types.NUMBER.output;
    // }
};

/************************************************* */


/*
 *AT Mode Block
 *Slave setting
 */
 Blockly.Blocks["bluetooth_at_slave"] = {
   init:function() {
      this.appendDummyInput()
        .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
      this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SLAVE)
      this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
        .appendField(new Blockly.FieldDropdown([
              ["38400","38400"],
              ["9600","9600"]]),"BAUD")
      this.setColour("#5d67a0")
      this.setTooltip("");
   }
 }

/*
 *AT Mode Block
 *master setting
 */
 Blockly.Blocks["bluetooth_at_master"] = {
   init:function() {
      this.appendDummyInput()
        .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
      this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_PAIRING_MASTER)
      this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_PAIRING_BIND_ADDR)
        .appendField(new Blockly.FieldTextInput("98d3:81:fd6a96"),"ADDR")
      this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
        .appendField(new Blockly.FieldDropdown([
              ["38400","38400"],
              ["9600","9600"]]),"BAUD")
      this.setColour("#5d67a0")
      this.setTooltip("");
   }
 }


/*
AT Mode code
*/

Blockly.Blocks['bluetooth_at_interaction'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
    this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_INTERACTION_TITLE);
    this.appendDummyInput()
        .appendField(Blockly.Msg.BLUETOOTH_INTERACTION_SET_CONTENT)
        .appendField(new Blockly.FieldDropdown([["9600","9600"], ["38400","38400"], ["115200","115200"]]), "bluetooth_at_interaction_baud");
    this.setColour("#5d67a0");
 this.setTooltip("");
 this.setHelpUrl("");
  }
};