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

/**
 * @fileoverview Ardublockly JavaScript for the Blockly resources and bindings.
 */
'use strict';

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

Blockly.Blocks.led.HUE = 198;

Blockly.Blocks['led_neopixel_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_ledLight.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-light.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_SETUP);
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_PIN)
            .appendField(new Blockly.FieldNumber(6, 0, 13), "neopixel_initpin")
            .appendField(Blockly.Msg.LED_BRTN)
            .appendField(new Blockly.FieldNumber(20, 0, 255), "neopixel_initbright");
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_PANELWIDTH)
            .appendField(new Blockly.FieldDropdown([
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
            ]), "neopixel_initwidth");
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_PANELHEIGHT)
            .appendField(new Blockly.FieldDropdown([
                ["1", "1"],
                ["2", "2"],
                ["3", "3"],
                ["4", "4"],
                ["5", "5"],
                ["6", "6"],
            ]), "neopixel_initheight");
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};


Blockly.Blocks['led_setrotation'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(Blockly.Msg.LED_SET_ROTATION)
        // .appendField(new Blockly.FieldDropdown([["0","led_setrotation_option_0"], ["90","led_setrotation_option_90"], ["180","led_setrotation_option_180"], ["270","led_setrotation_option_270"]]), "led_setrotation_option")
        .appendField(new Blockly.FieldDropdown([
                ["0", "0"],
                ["90", "1"],
                ["180", "2"],
                ["270", "3"]
            ]), "led_setrotation_option")
        .appendField(Blockly.Msg.LED_SET_ROTATION_DEGREE);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(Blockly.Blocks.led.HUE);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Blockly.Blocks['led_setbright'] = {
    init: function() {
        this.appendValueInput("BRIGHTNESS")
            .setCheck(Blockly.Types.NUMBER.checkList)
            .appendField(Blockly.Msg.LED_SET_BRIGHTNESS);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    },
    onchange: function() {
        var bright = Blockly.Python.valueToCode(this, "BRIGHTNESS", Blockly.Python.ORDER_ATOMIC)
        if (bright < 0 || bright > 255) {
            this.setWarningText(Blockly.Msg.LED_BRIGHTNESS_WARNING, 'fastled_setbright');
        } else {
            this.setWarningText(null, 'fastled_setbright');
        }
    },
    getBlockType: function() {
        return Blockly.Types.NUMBER;
    }
};

Blockly.Blocks['led_set_width_height'] = {
    init: function() {
        this.appendValueInput("WIDTH")
            .setCheck("Number")
            .appendField(Blockly.Msg.LED_WIDTH);
        this.appendValueInput("HEIGHT")
            .setCheck("Number")
            .appendField(Blockly.Msg.LED_HEIGHT);
        this.setOutput(true, "String");
        this.setInputsInline(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['led_set_position'] = {
    init: function() {
        this.appendValueInput("POSX")
            .setCheck("Number")
            .appendField("X:");
        this.appendValueInput("POSY")
            .setCheck("Number")
            .appendField("Y:");
        this.setOutput(true, "String");
        this.setInputsInline(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['led_drawpixel'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/pixel.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PIXEL);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COLOR);
        this.appendValueInput("POS")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COORDINATE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['led_drawrect'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/rect.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_FULL, "fill"],
                [Blockly.Msg.OLCD_STROKED, "draw"]
            ]), "rect_type")
            .appendField(Blockly.Msg.LED_RECTANGLE);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COLOR);
        this.appendValueInput("SIZE")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_SIZE);
        this.appendValueInput("POS")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COORDINATE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['led_drawline'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_LINE);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COLOR);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_LINE_START);
        this.appendValueInput("POSB")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_LINE_END);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['led_showall'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_SHOW_ALL);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['led_clear'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_CLEAR);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['led_text'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_COORDINATE)
            .appendField("x")
            .appendField(new Blockly.FieldTextInput("1"), "x_val")
            .appendField("y")
            .appendField(new Blockly.FieldTextInput("1"), "y_val");
        this.appendValueInput("text_color")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_TEXT)
        this.appendValueInput("back_color")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_BACKCOLOR)
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_SIZE)
            .appendField(new Blockly.FieldNumber(1, 0, 4, 1), "text_size");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setInputsInline(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};


Blockly.Blocks['led_scrolling_text'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("Scrolling");
        this.appendDummyInput()
            .appendField("speed")
            .appendField(new Blockly.FieldTextInput("100"), "led_scrolling_speed");
        this.appendValueInput("led_scrolling_color")
            .setCheck("String")
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setInputsInline(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['led_pattern_5x5'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
            .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
        this.appendDummyInput()
            .appendField("  ")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "0_0")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "0_1")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "0_2")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "0_3")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "0_4")
            .appendField("  ");
        this.appendDummyInput()
            .appendField("  ")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "1_0")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "1_1")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "1_2")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "1_3")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "1_4")
            .appendField("  ");
        this.appendDummyInput()
            .appendField("  ")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "2_0")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "2_1")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "2_2")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "2_3")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "2_4")
            .appendField("  ");
        this.appendDummyInput()
            .appendField("  ")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "3_0")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "3_1")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "3_2")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "3_3")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "3_4")
            .appendField("  ");
        this.appendDummyInput()
            .appendField("  ")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "4_0")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "4_1")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "4_2")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "4_3")
            .appendField((new Blockly.FieldColour("#3B5587")).setColours(["#3B5587", "#FFFFFF", "#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFC0CB"]), "4_4")
            .appendField("  ");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

/*
draw a num or vocabulary in 5x5
*/
Blockly.Blocks['led_drawWord_5x5'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/text.png", 25, 25, "15"));
        this.appendValueInput("WORD")
            .appendField(Blockly.Msg.LED_WORD);
        this.appendValueInput("COLOR")
            .appendField(Blockly.Msg.LED_COLOR);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

/*
 *draw a text with option:
 *    scroll
 *    setFont
 */
Blockly.Blocks['led_text_input'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./../blockly/media/text.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_SCROLL)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.LED_SCROLL_NO, '0'],
                [Blockly.Msg.LED_SCROLL_YES, '1']
            ]), "scroll")
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_FONT)
            .appendField(new Blockly.FieldDropdown([
                ["5x5", '1'],
                ["5x7", '2'],
                ["10x12", "3"]
            ]), "font")
            .appendField("px")
        this.appendValueInput("TEXT")
            .appendField(Blockly.Msg.LED_TEXT);
        this.appendValueInput("COLOR")
            .appendField(Blockly.Msg.LED_COLOR);
        this.appendValueInput("POS")
            .appendField(Blockly.Msg.LED_COORDINATE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

/*
 *draw emotion { :) , :| , :( } 
 */
Blockly.Blocks['led_draw_emotion'] = {
    init: function() {
        this.appendDummyInput("image")
            .appendField(new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"), "face_image");
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [":) SMILE", '1'],
                [":| NORMAL", '2'],
                [":( SAD", '3']
            ]), "face")
        this.appendValueInput("COLOR")
            .appendField(Blockly.Msg.LED_COLOR);
        // this.appendValueInput("POS")
        //     .appendField(Blockly.Msg.LED_COORDINATE);
        this.updateImage_();
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    },
    updateImage_: function() {
        var input = this.getInput("image");
        var type = this.getFieldValue("face");
        switch (type) {
            case "1":
                input.removeField("face_image");
                input.appendField(new Blockly.FieldImage("./../blockly/media/emotion.png", 40, 40, "15"), "face_image");
                break;
            case "2":
                input.removeField("face_image");
                input.appendField(new Blockly.FieldImage("./../blockly/media/normal-face.png", 40, 40, "15"), "face_image");
                break;
            case "3":
                input.removeField("face_image");
                input.appendField(new Blockly.FieldImage("./../blockly/media/sad-face.png", 40, 40, "15"), "face_image");
                break;
        }

    }
};

/**
 * draw pixel in led strip 
 */
Blockly.Blocks['led_draw_strip'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/strip_pixel.png", 25, 25, "15"));
        // new Blockly.FieldImage("./../blockly/media/strip_pixel.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.LED_STRIP_PIXEL);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COLOR);
        this.appendValueInput("LOC")
            .setCheck("Number")
            .appendField(Blockly.Msg.LED_STRIP_LOCATION);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};


Blockly.Blocks['led_set_color'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.COLOR)
            .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
        this.setOutput(true, "String");
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['led_set_colorRGB'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.COLOR);
        this.appendValueInput("RED_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.RED);
        this.appendValueInput("GREEN_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.GREEN);
        this.appendValueInput("BLUE_VAL")
            .setCheck(null)
            .appendField(Blockly.Msg.BLUE);
        this.setInputsInline(true);
        this.setOutput(true, "String");
        this.setColour(Blockly.Blocks.led.HUE);
        this.setTooltip("");
        this.setHelpUrl("http://help.cocorobo.cn/#/cocomod/color-analyzer?id=rgb-%E5%8F%96%E8%89%B2%E5%99%A8");
    }
}