
'use strict';

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

goog.require('Blockly.Blocks');



Blockly.Blocks.string.HUE = 330;

Blockly.Blocks['string_declare'] = {
  // Variable setter.
 // helpUrl: ('http://ardublock.ru/index.php?id=peremennye'),
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendValueInput('VALUE', null)
        .appendField(Blockly.Msg.Var_Declare)
        .appendField(new Blockly.FieldString(
        Blockly.LANG_VARIABLES_SET_ITEM), 'VAR')
        .appendField(Blockly.Msg.String_As)
	.appendField(Blockly.Msg.INOUT_VALUE);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip('');
	this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  },
  getString: function() {
    return [this.getFieldValue('VAR')];
  },
  renameString: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setTitleValue(newName, 'VAR');
    }
  }
};



Blockly.Blocks['string_get'] = {

  init: function() {
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.setOutput(true);
    this.setTooltip('');
    this.contextMenuMsg_ = Blockly.Msg.VARIABLES_GET_CREATE_SET;
  },

 getString: function() {
    return [this.getFieldValue('VAR')];
  },

  renameString: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setFieldValue(newName, 'VAR');
    }
  },
  contextMenuType_: 'string_set',

  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);
  }
};
/*
Blockly.Blocks['string_set'] = {
  
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.STRING_SET,
      "args0": [
        {
          "type": "field_variable",
          "name": "VAR",
          "variable": Blockly.Msg.STRING_DEFAULT_NAME
        },
        {
          "type": "input_value",
          "name": "VALUE"
        }
      ],
      "previousStatement": null,
      "nextStatement": null,
      "colour": Blockly.Blocks.string.HUE,
      "tooltip": Blockly.Msg.STRING_SET_TOOLTIP,
      "helpUrl": Blockly.Msg.STRING_SET_HELPURL
    });
    this.contextMenuMsg_ = Blockly.Msg.STRING_SET_CREATE_GET;
  },
  
  getString: function() {
    return [this.getFieldValue('VAR')];
  },
  
  renameString: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setFieldValue(newName, 'VAR');
    }
  },
  contextMenuType_: 'string_get',
  customContextMenu: Blockly.Blocks['string_get'].customContextMenu
};*/

Blockly.Blocks['string_set'] = {

  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.VARIABLES_SET,
      "args0": [
        {
         // "type": "field_variable",
		  "type": "field_string",
          "name": "VAR",
          "variable": Blockly.Msg.VARIABLES_DEFAULT_NAME
        },
        {
          "type": "input_value",
          "name": "VALUE"
        }
      ],
      "previousStatement": null,
      "nextStatement": null,
      "colour": Blockly.Blocks.string.HUE,
      "tooltip": Blockly.Msg.VARIABLES_SET_TOOLTIP,
      "helpUrl": (Blockly.Msg.array_declare_Url_1)
    });
	
	
	
    this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
  },

  getString: function() {
    return [this.getFieldValue('VAR')];
  },

  renameString: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setFieldValue(newName, 'VAR');
    }
  },
  contextMenuType_: 'string_get',
  customContextMenu: Blockly.Blocks['string_get'].customContextMenu
};


Blockly.Blocks['string_charatnnn'] = {

  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.STRING_charat,
      "args0": [
        {
          "type": "field_string",
          "name": "VAR",
          "variable": Blockly.Msg.STRING_DEFAULT_NAME
        },
        {
          "type": "input_value",
          "name": "VALUE"
        }
      ],
      "previousStatement": null,
      "nextStatement": null,
      "setOutput":null,
      "colour": Blockly.Blocks.string.HUE,
      "tooltip": Blockly.Msg.STRING_SET_TOOLTIP,
      "helpUrl": (Blockly.Msg.array_declare_Url_1)
    });
    this.contextMenuMsg_ = Blockly.Msg.STRING_SET_CREATE_GET;
  },

  getString: function() {
    return [this.getFieldValue('VAR')];
  },

  renameSting: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setFieldValue(newName, 'VAR');
    }
  },
  contextMenuType_: 'string_get',
  customContextMenu: Blockly.Blocks['string_get'].customContextMenu
};

Blockly.Blocks['string_charat'] = {

  init: function() {
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.appendValueInput("NAME")
        .setCheck("Number")
        .appendField(Blockly.Msg.STRING_charat2);
    this.setInputsInline(true);
    this.setOutput(true, "string");
    this.setTooltip('');
    this.contextMenuMsg_ = Blockly.Msg.VARIABLES_GET_CREATE_SET;
  },

  getString: function() {
    return [this.getFieldValue('VAR')];
  },

  renameString: function(oldName, newName) {
    if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
      this.setFieldValue(newName, 'VAR');
    }
  },
  contextMenuType_: 'string_set',

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

Blockly.Blocks['string_compareto'] = {
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.appendValueInput("input_to_compare")
        .setAlign(Blockly.ALIGN_CENTRE)
        .appendField(Blockly.Msg.STRING_compareTo);
    this.setInputsInline(true);
    this.setOutput(true, "Number");
    this.setTooltip('');
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  }
};

Blockly.Blocks['string_concat'] = {
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.appendValueInput("NAME")
        .appendField(Blockly.Msg.STRING_Contact);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip('');
    this.setInputsInline(true);
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  }
};

Blockly.Blocks['string_lengthof'] = {
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
   this.appendDummyInput()
        .appendField(Blockly.Msg.STRING_LengthOf);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.setInputsInline(true);
    this.setOutput(true, "Number");
    this.setTooltip('');
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  }
};

Blockly.Blocks['string_endswith'] = {
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.appendValueInput("input_to_compare")
        .setAlign(Blockly.ALIGN_CENTRE)
        .appendField(Blockly.Msg.STRING_Ends_With);
    this.setInputsInline(true);
    this.setOutput(true, "Boolean");
    this.setTooltip('');
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  }
};

Blockly.Blocks['string_equalto'] = {
  init: function() {
    this.setColour(Blockly.Blocks.string.HUE);
    this.appendDummyInput()
        .appendField(new Blockly.FieldString(
        Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
    this.appendValueInput("input_to_compare")
        .setAlign(Blockly.ALIGN_CENTRE)
        .appendField(Blockly.Msg.STRING_Equal);
    this.setInputsInline(true);
    this.setOutput(true, "Boolean");
    this.setTooltip('');
    this.setHelpUrl(Blockly.Msg.array_declare_Url_1);
  }
};
