from mower.utils import config
from mower.utils import typealias as tp
from mower.utils.image import cmatch, cropimg, loadres, template

node_type = {
    "Sarkaz": [
        "不期而遇",
        "作战",
        "紧急作战",
        "安全的角落",
        "得偿所愿",
        "诡意行商",
        "命运所指",
        "去伪存真",
        "失与得",
        "思维边界",
        "先行一步",
        "兴致盎然",
        "狭路相逢",
        "Boss",
    ]
}


class Node:
    def __init__(self, scope):
        self.scope: tp.Scope = scope  # 节点范围
        self.status = False  # 是否为下一步可前往的节点
        self.next_nodes = []  # 可前往的下一个节点
        self.detect_type()

    def detect_type(self):
        for type in node_type[config.conf.maa_rg_theme]:
            res_name = f"rogue/node_{config.conf.maa_rg_theme}/{type}"
            res = loadres(res_name)
            match_scope = (
                (self.scope[0][0] + 50, self.scope[1][0]),
                (self.scope[0][1] - 50, self.scope[1][1]),
            )
            score, scope = template(scene_image, res, self.scope)
            if score > 0.75:
                self.type = type
                self.type_scope = scope
                # 用平均色判断可否前往
                img = cropimg(scene_image, scope)
                if cmatch(img, res) and self.type != "Boss":
                    self.status = True
                return
            self.type = "未知"
            self.type_scope = match_scope

    def __repr__(self):
        return (
            f"Node(type={self.type},status={self.status},next_nodes={self.next_nodes})"
        )


nodes: dict[str, Node] = {}  # 节点编号:节点对象
last_node_id: str = ""  # 上一步节点编号
next_step: dict[str:int] = {}  # 下一步可前往的节点编号,编号:次数
scene_image = None  # 整层的图像
