'use strict';

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

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

Blockly.Blocks['lcd_setup'] = {
    init: function() {
        this.appendDummyInput()
            // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_oledScreen.png", 140, 40, "0"));
            .appendField(new Blockly.FieldImage("./../blockly/media/main-screen.png", 50, 40, "0"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_SETUP);
        this.setColour(245);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_set_width_height'] = {
    init: function() {
        this.appendValueInput("WIDTH")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_WIDTH);
        this.appendValueInput("HEIGHT")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_HEIGHT);
        this.setOutput(true, "String");
        this.setInputsInline(true);
        this.setColour("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['lcd_set_bool'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ARD_TYPE_BOOL)
            .appendField(new Blockly.FieldCheckbox("TRUE"), "BOOL");
        this.setOutput(true, "Boolean");
        this.setColour("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};



Blockly.Blocks['lcd_get_WH'] = {
    init: function() {
        this.jsonInit({
            colour: CategoryColors.Show,
            args0: [{
                name: "display_fill",
                options: [
                    [Blockly.Msg.LED_WIDTH, "0"],
                    [Blockly.Msg.LED_HEIGHT, "1"]
                ],
                type: "field_dropdown"
            }],
            nextStatement: null,
            previousStatement: null,
            helpUrl: "",
            tooltip: "",
            message0: Blockly.Msg.GLCDD
        })
    }
};


Blockly.Python['lcd_get_WH'] = function(a) {
    var _text = a.getFieldValue("display_fill");
    return "tft.size()[" + _text + "]";
};

Blockly.Blocks['lcd_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("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['lcd_set_width'] = {
    init: function() {
        this.appendValueInput("Width")
            .setCheck("Number")
            .appendField("Width:");
        this.setOutput(true, "String");
        this.setInputsInline(true);
        this.setColour("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['lcd_set_Scale'] = {
    init: function() {
        this.appendValueInput("Scale")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_SCALE);
        this.setOutput(true, "String");
        this.setInputsInline(true);
        this.setColour("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};


Blockly.Blocks['lcd_screen'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_SCREEN)
            .appendField(Blockly.Msg.PROCEDURES_BEFORE_PARAMS)
            .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
        // this.appendValueInput("COLOR")
        //     .setCheck("String")
        //     .appendField(Blockly.Msg.OLCD_COLOR);
        // this.appendValueInput("POSA")
        //     .setCheck("String")
        //     .appendField(Blockly.Msg.OLCD_LINE_START);
        // this.appendValueInput("POSB")
        //     .setCheck("String")
        //     .appendField(Blockly.Msg.OLCD_LINE_END);
        //this.setInputsInline(false);

        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Python['lcd_screen'] = function(a) {
    a = a.getFieldValue("COLOR") || Blockly.Python.valueToCode(a, 'COLOR', Blockly.Python.ORDER_ATOMIC);
    var d = 0,
        e = 0,
        f = 0;
    try {
        7 == a.length && (d = parseInt(a.substring(1, 3), 16),
            e = parseInt(a.substring(3, 5), 16),
            f = parseInt(a.substring(5, 7), 16))
    } catch (g) {}

    // var _code = "FS_RGB = (" + d + "," + e + "," + f + ")\n";
    // _code += "FS_COLOR = ((FS_RGB[0] & 0xF8) << 8) | ((FS_RGB[1] & 0xFC) << 3) | (FS_RGB[2] >> 3)\n";
    // _code += "tft.fill(FS_COLOR)\n";

    //var _code = "tft.fill(TFT.TFTColor(0x" + d + ", 0x" + e + ", 0x" + f + "))\n";
    var _code = "(" + d + "," + e + "," + f + ")";
    return _code;
};

Blockly.Blocks['lcd_drawline'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_LINE);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_LINE_START);
        this.appendValueInput("POSB")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_LINE_END);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawvhline'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_FULL, "fillrect"],
                [Blockly.Msg.OLCD_CIRCLE, "rect"]
            ]), "rect_type")
            .appendField(Blockly.Msg.OLCD_LINE)
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COORDINATE);
        this.appendValueInput("POSB")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_SIZE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['ai_lcd_drawvhtext'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LCDDT);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.LED_COORDINATE);
        this.appendValueInput("CONTENT")
            .setCheck("String")
            .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_CONTENT);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LCDDT_C);
        this.appendValueInput("BCOLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.LCDDT_BC);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawlinelen'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_VLINE, "vline"],
                [Blockly.Msg.OLCD_HEIGHT, "hline"]
            ]), "rect_type")
            .appendField(Blockly.Msg.OLCD_LINE)
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_LINE_START);
        this.appendValueInput("POSB")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_LENGTHIS);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};


Blockly.Blocks['lcd_color'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.COLOR)
            .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
        this.setOutput(true, null);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Python['lcd_color'] = function(a) {
    // a = a.getFieldValue("COLOR");
    // var d = 0
    //     , e = 0
    //     , f = 0;
    // try {
    //     7 == a.length && (d = a.substring(1, 3),
    //         e = a.substring(3, 5),
    //         f = a.substring(5, 7));
    // }
    // catch (g) { }
    var _code = Blockly.Python['lcd_screen'](a);
    _code = "_TFTColor" + _code + "";
    //_code += "tft.fill(TFT.TFTColor(FS_COLOR))\n";
    return _code;
};

Blockly.Blocks['lcd_drawrect'] = {
    init: function() {
        //this.appendDummyInput()
        // .appendField(
        //     new Blockly.FieldImage("./../blockly/media/rect.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            // .appendField(new Blockly.FieldDropdown([
            //     [Blockly.Msg.OLCD_FULL, "fill"],
            //     [Blockly.Msg.OLCD_STROKED, "draw"]
            // ]), "rect_type")
            .appendField(Blockly.Msg.LED_PIXEL);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        // this.appendValueInput("SIZE")
        //     .setCheck("String")
        //     .appendField(Blockly.Msg.OLCD_SIZE);
        this.appendValueInput("POS")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COORDINATE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawcircle'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_FULL, "fillcircle"],
                [Blockly.Msg.OLCD_STROKED, "circle"]
            ]), "circle_type")
            .appendField(Blockly.Msg.OLCD_CIRCLE);

        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);

        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_CENTER_POS);

        this.appendValueInput("RADIUS")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_RADIUS);

        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawqrcode'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.LCDRQ);

        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_SET_POS);

        this.appendValueInput("CONTENT")
            .setCheck("String")
            .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_CONTENT);

        this.appendValueInput("WIDTH")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_WT);

        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawtext'] = {
    init: function() {
        // this.appendDummyInput()
        //     .appendField(
        //         new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.ARD_TYPE_TEXT);

        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);

        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_CENTER_POS);

        this.appendValueInput("CONTENT")
            .setCheck("String")
            .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_CONTENT);

        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_SCALE)
            .appendField(new Blockly.FieldNumber(1), 'TextContnet');

        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_WT)
            .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');

        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_drawtriangle'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/triangle.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_FULL, "fill"],
                [Blockly.Msg.OLCD_STROKED, "draw"]
            ]), "triangle_type")
            .appendField(Blockly.Msg.OLCD_TRIANGLE);
        this.appendValueInput("COLOR")
            .setCheck(null)
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POSA")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_POINT + ' A');
        this.appendValueInput("POSB")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_POINT + ' B');
        this.appendValueInput("POSC")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_POINT + ' C');
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(245);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_text'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(
                new Blockly.FieldImage("./../blockly/media/text.png", 25, 25, "15"));
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_TEXTNEWLINE)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.OLCD_TRUE, "println"],
                [Blockly.Msg.OLCD_FALSE, "print"]
            ]), "print_type");
        this.appendValueInput("INPUT")
            .setCheck(null)
            .appendField(Blockly.Msg.OLCD_TEXT);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POS")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COORDINATE);
        this.appendValueInput("SIZE")
            .setCheck("Number")
            .appendField(Blockly.Msg.OLCD_SIZE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(245);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_clear'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_CLEAR);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(245);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['lcd_rotation'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_SETROTATION)
            .appendField(new Blockly.FieldDropdown([
                ["0°", "1"],
                ["90°", "2"],
                ["180°", "3"],
                ["270°", "0"]
            ]), "DEGREE")
            .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
}

Blockly.Blocks['ai_lcd_invertcanvas'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LCDSCIC)
            .appendField(new Blockly.FieldVariable("canvas"), "varitem")
            .appendField(Blockly.Msg.LCDSCIC_I)
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
}


Blockly.Python.ai_lcd_invertcanvas = function(block) {
    var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
    var _code = variable_name + " = canvas.invert()\n";
    return _code;;
}

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

Blockly.Blocks['lcd_set_colortext'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.COLOR)
            .appendField(new Blockly.FieldDropdown([
                ["黑色", "BLACK"],
                ["紅色", "RED"],
                ["綠色", "GREEN"],
                ["藍色", "BLUE"],
                ["紫色", "PURPLE"],
                ["青色", "CYAN"],
                ["橙色", "ORANGE"],
                ["黃色", "YELLOW"]
            ]), "COLOR");
        this.setOutput(true, "String");
        this.setColour("#5bb2d6");
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['lcd_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(245);
        this.setTooltip("");
        this.setHelpUrl("http://help.cocorobo.cn/#/cocomod/color-analyzer?id=rgb-%E5%8F%96%E8%89%B2%E5%99%A8");
    }
}


Blockly.Blocks['ai_lcd_drawrect'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.OLCD_DRAW)
            .appendField(Blockly.Msg.LED_PIXEL);
        this.appendValueInput("COLOR")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COLOR);
        this.appendValueInput("POS")
            .setCheck("String")
            .appendField(Blockly.Msg.OLCD_COORDINATE);
        this.setInputsInline(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['ai_camera_filp'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.LCDMF)
            .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour("#5bb2d6");
        this.setTooltip('');
        this.setHelpUrl('');
    }
}

Blockly.Python.ai_camera_filp = function(block) {
    var _tf = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
    var _code = "lcd.mirror(" + _tf + ")\n";
    return _code;
}