import * as Blockly from "blockly"
import { pythonGenerator } from "blockly/python"
import { getAllGobalVariables } from "../../utils/blocklyFunction"

Blockly.Python = pythonGenerator || { Msg: Object.create(null) }

const aiColor = "#ee783a"

Blockly.Blocks['ai_set_object_classification'] = {
    init: function () {
        this.appendDummyInput()
            .appendField("开始物体分类识别");
        this.setPreviousStatement(true, null)
        this.setNextStatement(true, null)
        this.setColour(aiColor);
        this.setTooltip("开始物体分类识别");
    }
}

Blockly.Python['ai_set_object_classification'] = function (block) {
    var code = 'ai.set_object_classification()\n';
    return code;
};

Blockly.Blocks['ai_get_object_classification_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取物体分类的")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取物体分类结果");
    }
}

Blockly.Python['ai_get_object_classification_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_classification_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_object_detection_is_result'] = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.ai_model_digital_new_text)
        this.appendStatementInput("input")
            .setCheck(null)
            .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
        this.itemCount_ = 0;
        this.updateShape_();
        this.setMutator(new Blockly.icons.MutatorIcon(['ai_model_false_create_with_item'], this));
        this.setColour(aiColor);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    mutationToDom: function () {
        if (!this.itemCount_)
            return null;
        var container = document.createElement('mutation');
        this.itemCount_ && container.setAttribute('aielse', 1);
        return container;
    },
    domToMutation: function (a) {
        this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
        this.updateShape_();
    },
    decompose: function (a) {
        var b = a.newBlock('ai_model_false_create_with_container');
        b.initSvg();
        var c = b.nextConnection
        this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
            a.initSvg(),
            c.connect(a.previousConnection));
        console.log("this.elseCount_", a)
        return b
    },
    compose: function (a) {
        var b = a.nextConnection.targetBlock();
        this.itemCount_ = 0;
        var e = null;
        if (b && b.type === "ai_model_false_create_with_item") {
            this.itemCount_++;
            e = b.statementConnection_;
            b = b.nextConnection && b.nextConnection.targetBlock()
        }
        this.updateShape_()
        Blockly.icons.MutatorIcon.reconnect(e, this, "AIELSE")
    },
    saveConnections: function (a) {
        a = a.nextConnection.targetBlock();
        var AIELSE = ""
        if (a && a.type === "ai_model_false_create_with_item") {
            AIELSE = this.getInput("AIELSE");
            a.statementConnection_ = AIELSE && AIELSE.connection.targetConnection;

            a = a.nextConnection && a.nextConnection.targetBlock()
        }
    },
    updateShape_: function () {
        this.getInput("AIELSE") && this.removeInput("AIELSE");
        this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
    },
};

Blockly.Blocks['ai_model_false_create_with_container'] = {
    init: function () {
        this.setColour(aiColor);
        this.appendDummyInput()
            .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
        this.setNextStatement(true)
        this.setTooltip('');
        this.contextMenu = false;
    }
};

Blockly.Blocks['ai_model_false_create_with_item'] = {
    init: function () {
        this.setColour(aiColor);
        this.appendDummyInput().appendField(Blockly.Msg.CONTROLS_IF_ELSE_TITLE_ELSE);
        this.setPreviousStatement(!0);
        this.setTooltip(Blockly.Msg.CONTROLS_IF_ELSE_TOOLTIP);
        this.contextMenu = !1
    }
};

Blockly.Python['ai_get_object_detection_is_result'] = function (block) {
    var statements_input = Blockly.Python.statementToCode(block, 'input');
    var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
    let global = getAllGobalVariables(block)
    Blockly.Python.definitions_['objectStatements'] = `def objectStatements(BOXESI):
  ${global != '' ? `global ${global}` : ''}
${statements_input}
`
    var code = `if (len(BOXES)):
  for BOXESI in BOXES:
    digitalStatements(BOXESI)
else:
${statements_input1 != "" ? statements_input1 : "    pass"}
`
    return code;
};

Blockly.Blocks['ai_set_object_detection'] = {
    init: function () {
        this.appendDummyInput()
            .appendField("开始物体检测");
        this.setPreviousStatement(true, null)
        this.setNextStatement(true, null)
        this.setColour(aiColor);
        this.setTooltip("开始物体检测");
    }
}
Blockly.Python['ai_set_object_detection'] = function (block) {
    var code = 'ai.set_object_detection()\n';
    return code;
};

Blockly.Blocks['ai_get_object_detection_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取物体检测结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取物体检测结果");
    }
}

Blockly.Python['ai_get_object_detection_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};


Blockly.Blocks['ai_set_face_and_keypoint_detection'] = {
    init: function () {
        this.appendDummyInput().appendField("开始人脸关键点检测");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}

Blockly.Python['ai_set_face_and_keypoint_detection'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_face_and_keypoint_detection_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取人脸检测结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取人脸检测结果");
    }
}

Blockly.Python['ai_get_face_and_keypoint_detection_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_facial_Keypoint_detection'] = {
    init: function () {
        this.appendDummyInput().appendField("开始人脸多关键点检测");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}

Blockly.Python['ai_set_facial_Keypoint_detection'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_facial_Keypoint_detection_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取人脸多关键点检测结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取人脸多关键点检测结果");
    }
}

Blockly.Python['ai_get_facial_Keypoint_detection_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_face_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始人脸识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_face_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_face_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取人脸识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取人脸识别结果");
    }
}
Blockly.Python['ai_get_face_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_facial_expression_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始情绪识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}

Blockly.Python['ai_set_facial_expression_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_facial_expression_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取情绪识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取情绪识别结果");
    }
}

Blockly.Python['ai_get_facial_expression_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_human_keypoint_detection'] = {
    init: function () {
        this.appendDummyInput().appendField("开始人体检测");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}

Blockly.Python['ai_set_human_keypoint_detection'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_human_keypoint_detection_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取人体检测结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取人体检测结果");
    }
}

Blockly.Python['ai_get_human_keypoint_detection_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_self_learning_classifier_init'] = {
    init: function () {
        this.appendDummyInput()
            .appendField("自学分类器初始化");
        this.appendValueInput("class_input")
            .setCheck(null)
            .appendField("设置识别的名字");
        this.setInputsInline(false);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip(Blockly.Msg.ai_model_self_learning_load);
        this.setHelpUrl("");
    }
}

Blockly.Python['ai_set_self_learning_classifier_init'] = function (block) {
    var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
    var code = value_class_input + '.init()\n';
    return code;
};

Blockly.Blocks['ai_set_self_learning_classifier_add_train_data'] = {
    init: function () {
        this.appendDummyInput().appendField('添加训练数据')
        this.setColour(aiColor);
        this.setTooltip('添加训练数据');
        this.setHelpUrl("");
        this.setNextStatement(true);
        this.setPreviousStatement(true);
    }
};

Blockly.Python['ai_set_self_learning_classifier_add_train_data'] = function (block) {
    var code = '.train()\n';
    return code;
};

Blockly.Blocks['ai_set_self_learning_classifier_save_model'] = {
    init: function () {
        this.appendDummyInput().appendField('保存模型')
        this.setNextStatement(true);
        this.setColour(aiColor);
        this.setTooltip('保存模型');
        this.setPreviousStatement(true);
    }
}

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

Blockly.Blocks['ai_set_self_learning_classifier_load_model'] = {
    init: function () {
        this.appendDummyInput().appendField('加载保存模型')
        this.setNextStatement(true);
        this.setColour(aiColor);
        this.setTooltip('加载保存模型');
        this.setPreviousStatement(true);
    }
}
Blockly.Python['ai_set_self_learning_classifier_load_model'] = function (block) {
    let code = ""
    return code;
};

Blockly.Blocks['ai_set_self_learning_classifier_result'] = {
    init: function () {
        this.appendDummyInput().appendField('自学习识别结果')
            .appendField(new Blockly.FieldDropdown([
                ["名称", "name"],
                ["置信度", "confidence"]
            ]), "TYPE");
        this.setNextStatement(true);
        this.setColour(aiColor);
        this.setTooltip('加载保存模型');
        this.setPreviousStatement(true);
    }
}
Blockly.Python['ai_set_self_learning_classifier_result'] = function (block) {
    let code = ""
    return code;
};

Blockly.Blocks['ai_set_object_trajectory_tracking_and_counting'] = {
    init: function () {
        this.appendDummyInput().appendField("开始物体轨迹识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_object_trajectory_tracking_and_counting'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_object_trajectory_tracking_and_counting_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取物体轨迹结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取物体轨迹结果");
    }
}
Blockly.Python['ai_set_object_trajectory_tracking_and_counting_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_ocr_text_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始文字识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_ocr_text_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_ocr_text_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取文字识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取文字识别结果");
    }
}
Blockly.Python['ai_get_ocr_text_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_gesture_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始手势识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_gesture_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_gesture_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取手势识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取手势识别结果");
    }
}
Blockly.Python['ai_set_gesture_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_pose_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始姿态识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_pose_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_pose_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取姿态识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取姿态识别结果");
    }
}
Blockly.Python['ai_set_pose_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

// 语音识别
Blockly.Blocks['ai_set_real_time_speech_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始语音识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_real_time_speech_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_real_time_speech_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取语音识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取语音识别结果");
    }
}
Blockly.Python['ai_get_real_time_speech_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_keyword_recognition'] = {
    init: function () {
        this.appendDummyInput().appendField("开始关键词识别");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(aiColor);
        this.setTooltip("");

    }
}
Blockly.Python['ai_set_keyword_recognition'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_get_keyword_recognition_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取关键词识别结果")
            .appendField(Blockly.Msg.ai_vision_from)
            .appendField(new Blockly.FieldVariable("i"), "varitem")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.image_start_X, "x"],
                [Blockly.Msg.image_start_Y, "y"],
                [Blockly.Msg.image_start_W, "w"],
                [Blockly.Msg.image_start_H, "h"],
                [Blockly.Msg.image_start_result, "result"],
                [Blockly.Msg.image_start_confidence, "confidence"]
            ]), "from");
        this.setColour(aiColor);
        this.setOutput(true, null);
        this.setTooltip("获取关键词识别结果");
    }
}
Blockly.Python['ai_get_keyword_recognition_result'] = function (block) {
    var from = block.getFieldValue('from');
    var code = 'ai.get_object_detection_result("' + from + '")';
    return [code, Blockly.Python.ORDER_NONE];
};



// AIGC
const AIGCColor = "#0000FF"
Blockly.Blocks['Initialize_the_AIGC_large_model'] = {
    init: function () {
        this.appendDummyInput().appendField("初始化AIGC大模型")
            .appendField(new Blockly.FieldDropdown([
                ["Qwen", "Qwen"],
                ["DeepSeek", "DeepSeek"],
                ["InternVl", "InternVl"]
            ]),"selectModel");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(AIGCColor);
        this.setTooltip("");
    }
}
Blockly.Python['Initialize_the_AIGC_large_model'] = function (block) {
    var selectModel = block.getFieldValue('selectModel');
    var code = 'ai.initialize_the_AIGC_large_model()';
    return code;
}

Blockly.Blocks['ai_set_aigc_image_generation'] = {
    init: function () {
        this.appendDummyInput().appendField("设置提问的问题")
        this.appendValueInput("generation").setCheck(null);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(AIGCColor);
        this.setTooltip("");
        this.setInputsInline(true);
    }
}

Blockly.Python['ai_set_aigc_image_generation'] = function (block) {
    var generation = Blockly.Python.valueToCode(block, 'generation', Blockly.Python.ORDER_ATOMIC);
    var code = 'ai.set_aigc_image_generation(' + generation + ')\n';
    return code;
}

Blockly.Blocks['ai_get_aigc_image_generation_result'] = {
    init: function () {
        this.appendDummyInput().appendField("获取AIGC大模型生成结果");
        this.setColour(AIGCColor);
        this.setOutput(true, null);
        this.setTooltip("获取AIGC大模型生成结果");
    }
}
Blockly.Python['ai_get_aigc_image_generation_result'] = function (block) {
    var code = 'ai.get_aigc_image_generation_result()';
    return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Blocks['ai_set_ai_agent'] = {
    init: function () {
        this.appendValueInput("NAME")
            .setCheck(null)
            .appendField(Blockly.Msg.ChatGpt_ai_agent_chat_set_appID);
        this.appendValueInput("NAME1")
            .setCheck(null)
            .appendField(Blockly.Msg.ChatGpt_set_problem_content);
        this.appendDummyInput().appendField(Blockly.Msg.ChatGpt_ai_agent_chat_result)
        this.setOutput(true, null);
        this.setInputsInline(false);
        this.setColour(AIGCColor);
        this.setTooltip(Blockly.Msg.ai_set_ai_agent_title);
        this.setHelpUrl("");
    }
}
Blockly.Python['ai_set_ai_agent'] = function (block) {
    var appID = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
    var problem = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
    var code = 'ai.set_ai_agent(' + appID + ',' + problem + ')';
    return code;
}


export default Blockly;