'use strict';

goog.provide('Blockly.Python.color');
goog.require('Blockly.Python');

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    var rgb = result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
    return '(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')';
};

function sharpTo0x(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? '0x' + result[1] + result[2] + result[3] : null;
};

/** add on 14:25-02/05/2018 by QR
 *   sharp color
 */
function sharpTo565(color) {
    var result, rgb;
    if (color[0] == "#") {
        result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
        rgb = result ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16)
        } : null;
    } else {
        var regexp = /([0-9]{0,3}),([0-9]{0,3}),([0-9]{0,3})/g;
        var result = regexp.exec(color);
        rgb = result ? {
            r: result[1],
            g: result[2],
            b: result[3]
        } : null;
    }
    var rest = (rgb.r >> 3) * 256 * 8 + (rgb.g >> 2) * 32 + (rgb.b >> 3);
    rest = rest.toString(16);
    if (rest.length < 4) {
        while (rest.length < 4) {
            rest = '0' + rest;
        }
    }
    return result ? '0x' + rest : null;
}
// #end

Blockly.Python['color_analyzer_setup'] = function(block) {
    Blockly.Python.addInclude('wire.h', '#include <Wire.h>\n');
    Blockly.Python.addInclude('colorAnalyzeDef', '#include "Adafruit_TCS34725.h"\n\nAdafruit_TCS34725 tcs = Adafruit_TCS34725();\n');

    Blockly.Python.addSetup('colorAnalyzeSetup', 'tcs.begin();\n');
    Blockly.Python.addDeclaration("Color_analyzer_colors", "uint16_t red, green, blue, c;\n");
    var code = 'tcs.getRawData(&red, &green, &blue, &c);\n' +
        'red = red*256/c;\n' +
        'green = green*256/c;\n' +
        'blue = blue*256/c;\n' +
        'if (red > 255) red = 255;\n' +
        'if (green > 255) green = 255;\n' +
        'if (blue > 255) blue = 255;\n';
    return code;
};

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

Blockly.Python['read_r'] = function(block) {
    var code = 'red';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['read_g'] = function(block) {
    var code = 'green';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['read_b'] = function(block) {
    var code = 'blue';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['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];
};

Blockly.Python['isColorRGB'] = function(block) {
    var color = block.getFieldValue("COLOR") || 'r';
    var code = 'int isColorRGB_' + color + '() {\n';
    switch (color) {
        case 'r':
            code += '  if (red >= 200 && green <= 50  && blue <= 50)\n ' +
                '    return 1;\n' +
                '  else\n' +
                '    return 0;\n';
            break;
        case 'g':
            code += '  if (green >= 200 && red <= 50  && blue <= 50)\n ' +
                '    return 1;\n' +
                '  else\n' +
                '    return 0;\n';
            break;
        case 'b':
            code += '  if (blue >= 200 && green <= 50  && red <= 50)\n ' +
                '    return 1;\n' +
                '  else\n' +
                '    return 0;\n';
        default:
            break;
    }
    code += '}\n';
    Blockly.Python.addFunction("isColor_" + color, code);
    code = "isColorRGB_" + color + "()";
    return [code, Blockly.Python.ORDER_ATOMIC];
}