/**
 * @license Licensed under the Apache License, Version 2.0 (the "License"):
 *          http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @fileoverview Arduino blocks for the Servo library.
 *     The Arduino Servo functions can be found in
 *     http://arduino.cc/en/reference/servo
 *
 * TODO: Add angle selector instead of block input.
 */
'use strict';

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

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


/** Common HSV hue for all blocks in this category. */
Blockly.Blocks.servo.HUE = 200;


Blockly.Blocks['servo_setup'] = {
    /**
     * Block for setup a servo .
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            //   .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_motorDriver.png", 140, 40, "15"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-servo.png", 50, 40, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_SERVO_SETUP);
        //.appendField('SERVO_SETUP');
        this.setColour(Blockly.Blocks.servo.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};


Blockly.Blocks['servo_write'] = {
    /**
     * Block for writing an angle value into a servo pin.
     * @this Blockly.Block
     */
    init: function() {
        this.setHelpUrl('http://arduino.cc/en/Reference/ServoWrite');
        this.setColour(Blockly.Blocks.servo.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_SERVO_WRITE)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), 'SERVO_PIN');
        this.setInputsInline(false);
        this.appendValueInput('SERVO_ANGLE')
            .setCheck(Blockly.Types.NUMBER.checkList)
            .appendField(Blockly.Msg.ARD_SERVO_WRITE_TO);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_SERVO_WRITE_DEG_180);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip(Blockly.Msg.ARD_SERVO_WRITE_TIP);
    },
    /**
     * Updates the content of the the pin related fields.
     * @this Blockly.Block
     */
    updateFields: function() {
        Blockly.Python.Boards.refreshBlockFieldDropdown(
            this, 'SERVO_PIN', 'pwmPins');
    }
};

Blockly.Blocks['servo_read'] = {
    /**
     * Block for reading an angle value of a servo pin.
     * @this Blockly.Block
     */
    init: function() {
        this.setHelpUrl('http://arduino.cc/en/Reference/ServoRead');
        this.setColour(Blockly.Blocks.servo.HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_SERVO_READ)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), 'SERVO_PIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setTooltip(Blockly.Msg.ARD_SERVO_READ_TIP);
    },
    /** @return {string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    },
    /**
     * Updates the content of the the pin related fields.
     * @this Blockly.Block
     */
    updateFields: function() {
        Blockly.Python.Boards.refreshBlockFieldDropdown(
            this, 'SERVO_PIN', 'pwmPins');
    }
};

Blockly.Blocks['robot_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_bipedalRobot.png", 140, 40, "15"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-biped_robot.png", 50, 40, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.ROBOT_SETUP);
        this.appendDummyInput()
            .appendField(Blockly.Msg.ROBOT_SETUP_SETYLPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), "YL_PIN")
            .appendField(Blockly.Msg.ROBOT_SETUP_SETYRPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), "YR_PIN");
        this.appendDummyInput()
            .appendField(Blockly.Msg.ROBOT_SETUP_SETRLPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), "RL_PIN")
            .appendField(Blockly.Msg.ROBOT_SETUP_SETRRPIN)
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), "RR_PIN");
        this.setNextStatement(false, null);
        this.setColour(Blockly.Blocks.servo.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['robot_set_movement'] = {
    init: function() {
        this.appendValueInput("MOVEMENT")
            .setCheck("String")
            .appendField(Blockly.Msg.ROBOT_SETMOVE_MOVEMENT);
        this.appendValueInput("TEMPO")
            .setCheck("Number")
            .appendField(Blockly.Msg.ROBOT_SETMOVE_TEMPO);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Blocks.servo.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['robot_movement'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_GOINGUP, "goingUp"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_KICKLEFT, "kickLeft"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_KICKRIGHT, "kickRight"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_RUN, "run"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_WALK, "walk"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_BACKYARD, "backyard"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_TURNLEFT, "turnLeft"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_TURNRIGHT, "turnRight"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_MOONWALKLEFT, "moonWalkLeft"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_MOONWALKRIGHT, "moonWalkRight"],
                [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_SWING, "swing"]
            ]), "MOVEMENT");
        this.setOutput(true, null);
        this.setColour(Blockly.Blocks.servo.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['robot_tempo'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldNumber(400), "TEMPO");
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.servo.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};