import Blockly from 'blockly';
import { def, Blocks } from './utils';

Blocks['flag_start'] = {
  init() {
    this.appendDummyInput().appendField("開始");
    this.setNextStatement(true, 'code');
    this.setTooltip('ここからプログラムを始めます');
    this.setColour(285);
    this.suppressPrefixSuffix = true;
  }
};

Blocks['say'] = {
  init() {
    this.appendDummyInput().appendField(new Blockly.FieldTextInput('Hello World'), 'text').appendField('と言う');
    this.setPreviousStatement(true, 'code');
    this.setNextStatement(true, 'code');
    this.setColour(230);
    this.setTooltip('標準出力に文字を出力します');
    this.suppressPrefixSuffix = false;
  },
};

function defLoop(name: string, inputBlock: boolean) {
  def(name, {
    init() {
      let dummy: Blockly.Input;
      if (inputBlock) {
        this.appendValueInput('arg0');
        dummy = this.appendDummyInput();
      } else {
        dummy = this.appendDummyInput();
        dummy.appendField(new Blockly.FieldNumber(5, 0, 4294967295), 'arg0');
      }
      dummy.appendField('回繰り返す');
      this.appendStatementInput('program').setCheck('code');
      this.setPreviousStatement(true, 'code');
      this.setNextStatement(true, 'code');
      this.setColour(330);
      this.setTooltip('指定した回数繰り返します');
      this.suppressPrefixSuffix = true;
    },
  });
}
defLoop('loop-field', false);
defLoop('loop-block', true);

function defBinaryOperator(name: string, op: string, colour: number) {
  def(name, {
    init() {
      this.appendValueInput('arg0');
      this.appendDummyInput().appendField(op);
      this.appendValueInput('arg1');
      this.setColour(colour);
      this.setOutput(true, 'Number');
      this.suppressPrefixSuffix = true;
    },
  });
}
defBinaryOperator('add', '+', 200);
defBinaryOperator('sub', '-', 200);
defBinaryOperator('mul', '*', 200);
defBinaryOperator('div', '/', 200);

def('number', {
  init() {
    this.appendDummyInput().appendField(new Blockly.FieldNumber(0), 'arg0');
    this.setColour(300);
    this.setOutput(true, 'Number');
    this.suppressPrefixSuffix = true;
  },
});
