'use strict';

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

goog.require('Blockly.Python');

// "void variableTo565(string color){\n"+
// "    var result\n"+
// "    var regexp = /(\w+),(\w+),(\w+)/g\n"+
// "    var result = regexp.exec(color)\n"
// "    var rest = (result[1]>> 3) * 256 * 8 + (result[2] >> 2) * 32 + (result[3] >> 3)\n"+
// "    rest = rest.toString(16)\n"+
// "    if (rest.length < 4) {\n"+
// "        while (rest.length < 4) {\n"+
//             "rest = '0' + rest\n"+
// "        }\n"+
// "  }\n"+
// "    return result ? '0x' + rest : null\n"+
// "}\n";

function sharpTo565(a) {
    var b = "#" == a[0] ? (a = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(a)) ? { r: parseInt(a[1], 16), g: parseInt(a[2], 16), b: parseInt(a[3], 16) } : null : (a = /([0-9]{0,3}),([0-9]{0,3}),([0-9]{0,3})/g.exec(a)) ? { r: a[1], g: a[2], b: a[3] } : null;
    b = 2048 * (b.r >> 3) + 32 * (b.g >> 2) + (b.b >> 3);
    b = b.toString(16);
    if (4 > b.length)
        for (; 4 > b.length;)b = "0" + b; return a ? "0x" + b : null
}

Blockly.Python['lcd_setup'] = function (block) {
    Blockly.Python.addInclude('Aadafruit_GFX', '#include <Adafruit_GFX.h>');
    Blockly.Python.addInclude('Adafruit_SSD1351', '#include <Adafruit_SSD1351.h>');
    Blockly.Python.addInclude('SPI', '#include <SPI.h>');

    Blockly.Python.addDeclaration('SSD1351_CS', '#define CS    8');
    Blockly.Python.addDeclaration('SSD1351_DC', '#define DC    5');
    Blockly.Python.addDeclaration('SSD1351_RST', '#define RST    6');
    Blockly.Python.addDeclaration('SSD1351_decl', 'Adafruit_SSD1351 tft = Adafruit_SSD1351(CS, DC, RST);');

    Blockly.Python.addSetup('SSD1351_begin', 'tft.begin()\n  tft.fillScreen(0x0000)\n', false);
    return '';
};

Blockly.Python['lcd_set_width_height'] = function (block) {
    var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
    var height = Blockly.Python.valueToCode(block, 'HEIGHT', Blockly.Python.ORDER_ATOMIC);
    var code = width + ', ' + height;
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['lcd_set_position'] = function (block) {
    // var reg = /^?[0-9]\d*$/
    var pos_x = Blockly.Python.valueToCode(block, 'POSX', Blockly.Python.ORDER_ATOMIC);
    var pos_y = Blockly.Python.valueToCode(block, 'POSY', Blockly.Python.ORDER_ATOMIC);
    //var code = posConvert(pos_x, pos_y);
    var code = pos_x + "," + pos_y;
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['lcd_drawline'] = function (block) {
    // var drawlinecolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
    var code = Blockly.Python['lcd_color'](block);
    code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), " + code + ")\n"
    return code;
};


Blockly.Python['lcd_drawlinelen'] = function (block) {
    // var drawlinecolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));

    var _type = block.getFieldValue("rect_type");
    var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
    var code = Blockly.Python['lcd_color'](block);
    code += "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", FS_COLOR)\n"
    return code;
};


Blockly.Python['lcd_drawvhline'] = function (block) {
    // var drawlinecolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var _type = block.getFieldValue("rect_type");
    var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
    var code = Blockly.Python['lcd_color'](block);
    code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), " + code + ")\n"
    return code;
};

Blockly.Python['ai_lcd_drawvhtext'] = function (block) {
    var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
    var bcolor = Blockly.Python.valueToCode(block, 'BCOLOR', Blockly.Python.ORDER_ATOMIC);
    var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
    var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
    var _code = "lcd.draw_string(" + xy + ", " + content + ", lcd." + color + ", lcd." + bcolor + ")";//, scale=" + size + "
    return [_code, Blockly.Python.ORDER_ATOMIC]
};

Blockly.Python['lcd_set_width'] = function (block) {
    // var reg = /^?[0-9]\d*$/
    var pos_w = Blockly.Python.valueToCode(block, 'Width', Blockly.Python.ORDER_ATOMIC);
    return [pos_w, Blockly.Python.ORDER_ATOMIC];
};


Blockly.Python['lcd_set_Scale'] = function (block) {
    // var reg = /^?[0-9]\d*$/
    var pos_w = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
    return [pos_w, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['lcd_drawrect'] = function (block) {
    // var drawrectcolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
    var code = Blockly.Python['lcd_color'](block);
    code = 'tft.pixel((' + drawrectcoord + '), ' + code + ')\n';
    return code;
};

Blockly.Python['lcd_drawcircle'] = function (block) {
    // var drawcirclecolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var circletype = block.getFieldValue('circle_type');
    var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var radius = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
    var code = Blockly.Python['lcd_color'](block);
    code = 'tft.' + circletype + '((' + position + '), ' + radius + ', ' + code + ')\n';
    return code;
};


Blockly.Python['lcd_drawqrcode'] = function (block) {
    var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
    var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
    var code = 'tft.drawqrcode(' + position + ', ' + content + ', ' + width + ')\n';
    return code;
};

Blockly.Python['lcd_drawtext'] = function (block) {
    // var drawcirclecolor = sharpTo565(
    //     Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
    var tcontent = block.getFieldValue('TextContnet');
    var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
    var code = Blockly.Python['lcd_color'](block);
    code = 'tft.text((' + position + '), ' + content + ', ' + code + ', sysfont, ' + tcontent + ', nowrap=' + box + ')\n';
    return code;
};

Blockly.Python['lcd_drawtriangle'] = function (block) {
    var tri_color = sharpTo565(
        Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var triangleType = block.getFieldValue('triangle_type');
    var pos_a = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
    var pos_b = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
    var pos_c = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC);

    var code = 'tft.' + triangleType + 'Triangle(' + pos_a + ', ' + pos_b + ', ' + pos_c + ',' + tri_color + ')\n';
    return code;
};

Blockly.Python['lcd_text'] = function (block) {
    var text_input = Blockly.Python.valueToCode(block, 'INPUT', Blockly.Python.ORDER_ATOMIC);
    var text_coord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
    var text_color = sharpTo565(
        Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
    var text_size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
    var text_type = block.getFieldValue('print_type');

    var code = 'tft.setCursor(' + text_coord + ')\n' +
        'tft.setTextColor(' + text_color + ')\n' +
        'tft.setTextSize(' + text_size + ')\n' +
        'tft.' + text_type + '(' + text_input + ')\n';
    return code;
};

Blockly.Python['lcd_clear'] = function (block) {
    var code = 'tft.fillScreen(0x0000)\n';
    return code;
};

Blockly.Python['lcd_rotation'] = function (block) {
    var degree = block.getFieldValue("DEGREE");
    var code = "tft.rotation(" + degree + ")\n";
    // Blockly.Python.addSetup("setRotation", code);
    return code;
}

Blockly.Python['lcd_set_color'] = function (block) {
    var code = block.getFieldValue('COLOR');
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['lcd_set_bool'] = function (block) {
    var code = block.getFieldValue('BOOL');
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['lcd_set_colortext'] = function (block) {
    var code = block.getFieldValue('COLOR');
    //Blockly.JavaScript.valueToCode(block, 'NAME', Blockly.JavaScript.ORDER_ATOMIC);
    return [code, Blockly.Python.ORDER_ATOMIC];
};


Blockly.Python['lcd_set_colorRGB'] = function (block) {
    var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
    var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
    var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
    var code = '(' + r + ',' + g + ',' + b + ')';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

/** add on 14:25-09/01/2019 by QR
 *   convert position from origin(x,y) to (x,128-y)
 */
function posConvert(x, y) {
    var reg = /^?[0-9]\d*$/;
    // if (reg.test(y)) y = 128 - parseInt(y, 10) 
    // else y = "128 - " + y;
    return x + ", 128 - " + y;
}
// #end