
import * as Blockly from 'blockly';
import { pythonGenerator } from 'blockly/python';


Blockly.Python = pythonGenerator || { Msg: Object.create(null) };
const color =  "#f0983e";

Blockly.Blocks['ai_canvas_area_tuple'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_canvas_area_tuple);
        this.appendValueInput("xy")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
        this.appendValueInput("wh")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_size);
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        this.setTooltip(Blockly.Msg.ai_vision_canvas_area_tuple);
        this.setHelpUrl("");
    }
};

Blockly.Python['ai_canvas_area_tuple'] = function (block) {
    var value_coordinate = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
    var value_size = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = value_coordinate + ',' + value_size;
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};
Blockly.Blocks['image_save'] = {
    init: function () {
        this.appendValueInput("save_path")
            .setCheck(null)
            .appendField(Blockly.Msg.save_img_path_to)
        this.setOutput(false, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.save_img_path_to);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setHelpUrl("");
    }
};

Blockly.Python['image_save'] = function (block) {
    var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);

    var code = 'canvas.save(' + save_path_location + ')';
    return code;
};

Blockly.Blocks["image_open"] = {
    init: function () {
        this.appendValueInput("save_path")
            .setCheck(null)
            .appendField(Blockly.Msg.read_img_path_to);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.read_img_path_to);
        this.setHelpUrl("");
    }
};

Blockly.Python["image_open"] = function (block) {
    var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
    Blockly.Python.definitions_['import_image'] = `from maix import image`;
    var code = `image.load(${save_path_location})`;
    return [code, Blockly.Python.ORDER_ATOMIC];
};

// 翻转
{/* <block type="v831_lcd_invert"></block> */}
Blockly.Blocks['v831_lcd_invert'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_text_let_canvas)
            .appendField(Blockly.Msg.image_process_pixtovec_text_first)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.display_draw_vertical, "0"],
                [Blockly.Msg.display_draw_horizontal, "1"]
            ]), "invert")
            .appendField(Blockly.Msg.MAIXDUINO_DISPLAY_ORIENTATION_ROTATE);
        this.setOutput(true, null);
        this.setColour(color);
        let _this = this;
        // this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
        this.setTooltip(function () {
            const invert = _this.getFieldValue('invert');
            const tooltip = {
                "0": Blockly.Msg.MAIXDUINO_DISPLAY_ORIENTATION_ROTATE_title.replace('%1', Blockly.Msg.display_draw_vertical),
                "1": Blockly.Msg.MAIXDUINO_DISPLAY_ORIENTATION_ROTATE_title.replace('%1', Blockly.Msg.display_draw_horizontal),
            }
            return tooltip[invert]
        })
        this.setHelpUrl("");
    }
};

Blockly.Python['v831_lcd_invert'] = function (block) {
    let invert = block.getFieldValue('invert');

    // TODO: Assemble Python into code variable.
    var code = `canvas.flip(${invert})`
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['image_rotating'] = {
    init: function () {
        this.appendValueInput("route")
            .setCheck(null)
            .appendField(Blockly.Msg.image_process_rotate)
        this.appendDummyInput().appendField(Blockly.Msg.image_process_rotate_angle);
        this.setOutput(false, null);
        this.setTooltip(Blockly.Msg.image_process_rotate);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
}

Blockly.Python['image_rotating'] = function (block) {
    var route = Blockly.Python.valueToCode(block, 'route', Blockly.Python.ORDER_ATOMIC);
    var code = `canvas = canvas.rotate(${route})`
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks.ai_lcd_XY_vision = {
    init: function () {
        var _input = this.appendDummyInput();
        this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
        this.setColour(color);
        this.setOutput(!0, "Boolean");
        _input.appendField(Blockly.Msg.image_process_xy_x);
        this.appendValueInput("A").setCheck("Number");
        _input = this.appendDummyInput();
        _input.appendField(Blockly.Msg.image_process_xy_y)
        this.appendValueInput("B").setCheck("Number");
        this.setInputsInline(!0);
    }
};

Blockly.Python.ai_lcd_XY_vision = function (block) {
    var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
    var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
    var code = "" + _x + ", " + _y + "";
    return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Blocks['image_crop'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_imgcut_cutting);
        this.appendValueInput("xy")
            .setCheck(null)
            .appendField(Blockly.Msg.image_process_imgcut_start_coord);
        this.appendValueInput("wh")
            .setCheck(null)
            .appendField(Blockly.Msg.image_process_imgcut_size);
        this.setOutput(false, null);
        this.setTooltip(Blockly.Msg.image_process_imgcut_cutting);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
    }
}

Blockly.Python['v831_lcd_img_cut'] = function (block) {
    var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
    var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);

    // TODO: Assemble Python into code variable.
    // TODO: Change ORDER_NONE to the correct strength.
    var code = `canvas = canvas.crop(${xy},${wh})`;
    return code;
};
Blockly.Blocks.ai_lcd_WH_vision = {
    init: function () {
        var _input = this.appendDummyInput();
        this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
        this.setColour("#f0983e");
        this.setOutput(!0, "Boolean");
        _input.appendField(Blockly.Msg.image_process_wh_width);
        this.appendValueInput("A").setCheck("Number");
        _input = this.appendDummyInput();
        _input.appendField(Blockly.Msg.image_process_wh_height)
        this.appendValueInput("B").setCheck("Number");
        this.setInputsInline(!0);
    }
};

Blockly.Python.ai_lcd_WH_vision = function (block) {
    var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
    var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
    var code = "" + _x + ", " + _y + "";
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Blocks['image_resize'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_img_resize);
        this.appendValueInput("wh")
            .setCheck(null)
        this.setOutput(false, null);
        this.setTooltip(Blockly.Msg.image_process_img_resize);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setInputsInline(true);
    }
}
Blockly.Python['image_resize'] = function (block) {
    var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
    var code = `canvas = canvas.resize(${wh})`
    return code;
};


Blockly.Blocks['ai_vision_get_threshold_default'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_detection_color_threshold_set)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ai_vision_detection_color_threshold_set_black, "black"],
                [Blockly.Msg.ai_vision_detection_color_threshold_set_white, "white"],
                [Blockly.Msg.ai_vision_detection_color_threshold_set_red, "red"],
                [Blockly.Msg.ai_vision_detection_color_threshold_set_green, "green"],
                [Blockly.Msg.ai_vision_detection_color_threshold_set_blue, "blue"],
                [Blockly.Msg.ai_vision_detection_color_threshold_set_orange, "orange"]
            ]), "color");
        this.setOutput(true, null);
        this.setColour("#f0983e");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('color');
            var TOOLTIPS = {
                'black': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_black),
                'white': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_white),
                'red': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_red),
                'green': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_green),
                'blue': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_blue),
                'orange': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_orange)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Python['ai_vision_get_threshold_default'] = function (block) {
    var dropdown_color = block.getFieldValue('color');
    // TODO: Assemble Python into code variable.

    if (dropdown_color == "black") {
        var code = '[[0,40]]';
    } else if (dropdown_color == "white") {
        var code = '[[64,100]]';
    } else if (dropdown_color == "red") {
        var code = '[[0, 80, 40, 80, 10, 80]]';
    } else if (dropdown_color == "green") {
        var code = '[[0, 80, -120, -10, 0, 30]]';
    } else if (dropdown_color == "blue") {
        var code = '[0, 80, 30, 100, -120, -60]';
    } else if (dropdown_color == "orange") {
        var code = '[[77, 55, 19, 61, 14, 108]]';
    }
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['image_find_blobs'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_find_blobs);
        this.appendValueInput("color")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
        this.appendValueInput("xy")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
        this.appendValueInput("wh")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_size);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.image_process_find_blobs);
        this.setHelpUrl("");
    }
}

Blockly.Python['image_find_blobs'] = function (block) {
    var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
    var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
    var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
    let code = `img.find_blobs(color = ${color},roi=[${xy} , ${wh}],pixels_threshold=500)`;
    return [code, Blockly.Python.ORDER_NONE];
}


Blockly.Blocks['ai_vision_get_blobs'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.serial_read_data)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(Blockly.Msg.image_get_find_blobs_result)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "0"],
                [Blockly.Msg.image_start_Y, "1"],
                [Blockly.Msg.image_start_W, "2"],
                [Blockly.Msg.image_start_H, "3"],
                [Blockly.Msg.image_start_CX, "cx"],
                [Blockly.Msg.image_start_CY, "cy"]
            ]), "blob_type");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        this.setTooltip(Blockly.Msg.image_get_find_blobs_result);
        this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
    }
};

Blockly.Python['ai_vision_get_blobs'] = function (block) {
    var variable_name = block.getFieldValue('varitem');
    var type = block.getFieldValue('blob_type');
    // TODO: Assemble Python into code variable.
    var code = `${variable_name}["${type}"]`;
    if (type == "cx") {
        code = `int((${variable_name}[0] + ${variable_name}[2])/2)`
    } else if (type == "cy") {
        code = `int((${variable_name}[1] + ${variable_name}[3])/2)`
    }
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['image_find_lines'] = {
    init:function() {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_find_lines);
        this.appendValueInput("color")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
        this.appendValueInput("xy")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
        this.appendValueInput("wh")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_find_blobs_size);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.image_process_find_lines);
        this.setHelpUrl("");
    }
}

Blockly.Python['image_find_lines'] = function(block) {
    var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
    var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC) || '0,0';
    var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC) || '0,0';
    let code = `img.get_regression(color=${color},roi=[${xy} , ${wh}],area_threshold =1000)`;
    return [code, Blockly.Python.ORDER_NONE];
}

Blockly.Blocks["image_get_lines_result"] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.serial_read_data)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(Blockly.Msg.image_get_find_blobs_result)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "0"],
                [Blockly.Msg.image_start_Y, "1"],
                [Blockly.Msg.image_start_W, "2"],
                [Blockly.Msg.image_start_H, "3"],
                [Blockly.Msg.image_start_CX, "cx"],
                [Blockly.Msg.image_start_CY, "cy"]
            ]), "blob_type");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        this.setTooltip(Blockly.Msg.image_get_find_blobs_result);
        this.setHelpUrl("");
    }
}


Blockly.Blocks['ai_lane_tracking_setup'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup);
        this.appendValueInput("color")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
        this.appendValueInput("lsize")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_left);
        this.appendValueInput("rsize")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_right);
        this.appendValueInput("axis")
            .setCheck(null)
            .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_axis);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.ai_vision_lane_tracking_setup);
        this.setHelpUrl("");
    }
};

Blockly.Python['ai_lane_tracking_setup'] = function (block) {
    let code = ``
    return code
}
Blockly.Blocks["image_find_qrcodes"] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_find_qrcodes);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.image_process_find_qrcodes);
        this.setHelpUrl("");
    }   
};

Blockly.Python["image_find_qrcodes"] = function (block) {
    var code = "canvas.find_qrcodes()";
    return [code, Blockly.Python.ORDER_NONE];
}


Blockly.Blocks['image_get_qrcodes_result'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(Blockly.Msg.image_process_get_qrcodes_result)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
                [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
                [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
                [Blockly.Msg.ai_vision_pattern_detection_w, "w1"],
                [Blockly.Msg.ai_vision_pattern_detection_h, "h2"]
            ]), "get_info");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('get_info');
            var TOOLTIPS = {
                'payload': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
                'data_type': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
                'x': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
                'y': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
                'w': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_X_Destination),
                'h': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_Y_Destination),
                'w1': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
                'h1': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Python['image_get_qrcodes_result'] = function (block) {
    var variable_name = block.getFieldValue('varitem');
    var dropdown_name = block.getFieldValue('get_info');
    // TODO: Assemble Python into code variable.
    var code = `${variable_name}["${dropdown_name}"]`;
    if (dropdown_name == "w") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["x"]`
    } else if (dropdown_name == "h") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["y"]`
    } else if (dropdown_name == "w1") {
        code = `${variable_name}["w"]`
    } else if (dropdown_name == "h2") {
        code = `${variable_name}["h"]`
    }
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks["image_find_barcodes"] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_find_barcodes);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.image_process_find_barcodes);
        this.setHelpUrl("");
    }
};

Blockly.Python["image_find_barcodes"] = function (block) {
    var code = "canvas.find_barcodes()";
    return [code, Blockly.Python.ORDER_NONE];
}

Blockly.Blocks['image_get_barcodes_result'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(Blockly.Msg.image_process_get_barcodes_result)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
                [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
                [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
                [Blockly.Msg.ai_vision_pattern_detection_w, "w1"],
                [Blockly.Msg.ai_vision_pattern_detection_h, "h2"]
            ]), "get_info");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('get_info');
            var TOOLTIPS = {
                'payload': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
                'x': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
                'y': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
                'w': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_X_Destination),
                'h': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_Y_Destination),
                'w1': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
                'h1': Blockly.Msg.ai_vision_barcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Python['image_get_barcodes_result'] = function (block) {
    var variable_name = block.getFieldValue('varitem');
    var dropdown_name = block.getFieldValue('get_info');
    // TODO: Assemble Python into code variable.
    var code = `${variable_name}["${dropdown_name}"]`;
    if (dropdown_name == "w") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["x"]`
    } else if (dropdown_name == "h") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["y"]`
    } else if (dropdown_name == "w1") {
        code = `${variable_name}["w"]`
    } else if (dropdown_name == "h2") {
        code = `${variable_name}["h"]`
    }
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks["image_find_apriltag"] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.image_process_find_apriltag);
        this.setOutput(true, null);
        this.setColour(color);
        this.setTooltip(Blockly.Msg.image_process_find_apriltag);
        this.setHelpUrl("");
    }
};

Blockly.Python["image_find_apriltag"] = function (block) {
    var code = "canvas.find_barcodes()";
    return [code, Blockly.Python.ORDER_NONE];
}

Blockly.Blocks['image_get_apriltag_result'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(Blockly.Msg.ai_vision_apriltag_get_info)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
                [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
                [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
                [Blockly.Msg.ai_vision_pattern_detection_w, "w1"],
                [Blockly.Msg.ai_vision_pattern_detection_h, "h2"]
            ]), "get_info");
        this.setInputsInline(false);
        this.setOutput(true, null);
        this.setColour("#f0983e");
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('get_info');
            var TOOLTIPS = {
                'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
                'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
                'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
                'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_X_Destination),
                'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_Y_Destination),
                'w1': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
                'h1': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
            };
            return TOOLTIPS[mode];
        });
        this.setHelpUrl("");
    }
};

Blockly.Python['image_get_apriltag_result'] = function (block) {
    var variable_name = block.getFieldValue('varitem');
    var dropdown_name = block.getFieldValue('get_info');
    // TODO: Assemble Python into code variable.
    var code = `${variable_name}["${dropdown_name}"]`;
    if (dropdown_name == "w") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["x"]`
    } else if (dropdown_name == "h") {
        code = `${variable_name}["${dropdown_name}"] + ${variable_name}["y"]`
    } else if (dropdown_name == "w1") {
        code = `${variable_name}["w"]`
    } else if (dropdown_name == "h2") {
        code = `${variable_name}["h"]`
    }
    return [code, Blockly.Python.ORDER_NONE];
};

// 视频处理
const videoColor = "#4d9994"
Blockly.Blocks["record_video"] = {
    init:function () { 
        this.appendDummyInput().appendField("视频录制设置")
        this.appendValueInput("time")
            .setCheck(null)
            .appendField("录制时长")
        this.appendValueInput("save_path")
            .setCheck(null)
            .appendField("保存路径");
        this.setNextStatement(true, null);
        this.setPreviousStatement(true, null);
        this.setColour(videoColor);
        this.setTooltip("设置视频录制参数");

    }
}

Blockly.Python['record_video'] = function(block) { 
    return ""
};

Blockly.Blocks['play_video'] = { 
    init: function() { 
        this.appendDummyInput()
            .appendField('播放视频');
        this.appendValueInput("video_path")
            .setCheck(null)
            .appendField("视频路径");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(videoColor);

    }
}

Blockly.Python['play_video'] = function(block) { 
    let code = '播放视频\n';
    return code;
};


export default Blockly