'use strict';

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

Blockly.Blocks.basic101.hue = 15;

Blockly.Blocks['read_button'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_READBUTTON, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList,
        }]);
        this.setInputsInline(true);
        this.setOutput(true, Blockly.Types.BOOLEAN.output);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.BOOLEAN;
    }
};
Blockly.Blocks['basic101_dropdown_button'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["8", "8"],
                ["9", "9"],
                ["12", "12"],
                ["13", "13"]
            ]), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['read_knob'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_READKNOB, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList,
        }]);
        this.setInputsInline(true);
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};
Blockly.Blocks['basic101_dropdown_knob'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["A3", "A3"]
            ]), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['analog_light'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_SETANALOGLED, [{
                "type": "input_value",
                "name": "PINSNUM",
                "check": Blockly.Types.NUMBER.checkList
            },
            {
                "type": "input_value",
                "name": "BRIGHTNESS",
                "check": Blockly.Types.NUMBER.checkList
            }
        ]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip('');
        this.setHelpUrl('');
    },

    onchange: function() {
        var bright = Blockly.Python.valueToCode(this, "BRIGHTNESS", Blockly.Python.ORDER_ATOMIC)
        if (bright > 255 || bright < 0) {
            this.setWarningText(Blockly.Msg.BASIC_LIGHT_WARNING, 'light');
        } else {
            this.setWarningText(null, 'light');
        }
    },

    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};
Blockly.Blocks['basic101_dropdown_analog_light'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["3", "3"],
                ["5", "5"],
                ["6", "6"],
            ]), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['digital_light'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_SETDIGITALLED, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList
        }]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};
Blockly.Blocks['basic101_dropdown_digital_light'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["0", "0"],
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
            ]), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};


Blockly.Blocks['nolight'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_NOLCD, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList
        }]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip();
        this.setHelpUrl('');
    }
};
Blockly.Blocks['basic101_dropdown_nolight'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                    ["0", "0"],
                    ["1", "1"],
                    ["2", "2"],
                    ["3", "3"],
                    ["4", "4"],
                    ["5", "5"],
                    ["6", "6"],
                ]),
                'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['tone'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_SETTONE, [{
                "type": "input_value",
                "name": "PINSNUM",
                "check": Blockly.Types.NUMBER.checkList
            },
            {
                "type": "input_value",
                "name": "FREQUENCY",
                "check": Blockly.Types.NUMBER.checkList
            }
        ]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip(Blockly.Msg.BASIC_TONE_TIP);
        this.setHelpUrl('https://www.arduino.cc/en/Reference/tone');
    },

    onchange: function() {
        var freq = Blockly.Python.valueToCode(this, "FREQUENCY", Blockly.Python.ORDER_ATOMIC)
        if (freq < 31 || freq > 65535) {
            this.setWarningText(Blockly.Msg.BASIC_TONE_WARNING, 'io_tone');
        } else {
            this.setWarningText(null, 'io_tone');
        }
    },

    /** @return {!string} The type of input value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};
Blockly.Blocks['basic101_dropdown_tone'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                    ["11", "11"]
                ]),
                'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['notone'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_NOTONE, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList
        }]);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip(Blockly.Msg.BASIC_NOTONE_TIP);
        this.setHelpUrl('https://www.arduino.cc/en/Reference/noTone');
    }
};
Blockly.Blocks['basic101_dropdown_notone'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                    ["11", "11"]
                ]),
                'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['basic101_dropdown_digital'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.digitalPins), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['basic101_dropdown_analog'] = {
    /**
     * Block for creating a analog pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.analogPins), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['basic101_dropdown_pwm'] = {
    /**
     * Block for creating a analog pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(
                Blockly.Python.Boards.selected.pwmPins), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['counter_button_setup'] = {
    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-sensor101-counter.png", 50, 40, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.BASIC_COUNTER_SETUP)
        this.appendDummyInput()
            .appendField(Blockly.Msg.BASIC_COUNTER_SETTING)
            .appendField(new Blockly.FieldDropdown(Blockly.Python.Boards.selected.digitalPins), 'PINSNUM');
        // this.interpolate_(Blockly.Msg.BASIC_COUNTER_SETUP, [
        //     {
        //         "type": "input_value",
        //         "name": "PINSNUM",
        //         "check": Blockly.Types.NUMBER.checkList,
        //     }
        // ]);        
        //this.setInputsInline(true);
        //this.setOutput(true, Blockly.Types.BOOLEAN.output);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};
Blockly.Blocks['read_counter'] = {
    init: function() {
        this.interpolate_(Blockly.Msg.BASIC_READCOUNTER, [{
            "type": "input_value",
            "name": "PINSNUM",
            "check": Blockly.Types.NUMBER.checkList,
        }]);
        this.setInputsInline(true);
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.basic101.hue);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};
Blockly.Blocks['basic101_counter_dropdown_button'] = {
    /**
     * Block for creating a digtial pin selector.
     * @this Blockly.Block
     */
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["8", "8"],
                ["9", "9"],
            ]), 'SELECTPIN');
        this.setOutput(true, Blockly.Types.NUMBER.output);
        this.setColour(Blockly.Blocks.io.HUE);
    },
    /** @return {!string} The type of return value for the block, an integer. */
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};