from functools import cached_property

from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.recognize import Scene

from .utils.utils import find_text


class NodeOptionSelectSolver(SceneGraphSolver):
    solver_name = "节点选项选择"
    _option = {
        "Sarkaz": {
            "相遇": ["它邀请对方共享猎物"],
            "在故事结束之后": ["时光的永恒在此定格", "战士的长角生根发芽"],
            "虫卡兹！": ["这怎么可能嘛？"],
            "阴魂不散": ["问他为什么哭"],
            "戴冠式": ["兄长为妹妹戴上冠冕"],
            "高空坠物": ["找些帮手来把这里搬空", "从中寻找有用的物件"],
            "前瞻": ["留意当下"],
            "原初异途": ["一个方盒", "那是一只角，还是一把铳？"],
            "待诉说的真相": ["收集散落的思绪"],
            "解惑": ["他们的头顶都生出了光环", "萨科塔杀死了萨卡兹"],
        }
    }

    @cached_property
    def options(self):
        for stage_name in self._option[config.conf.maa_rg_theme]:
            if find_text(
                stage_name, 36, scope=([165, 700], [165 + len(stage_name) * 50, 800])
            ):
                return self._option[config.conf.maa_rg_theme][stage_name]
        logger.error("未登记节点选项")
        return []

    def transition(self):
        if (scene := self.scene()) == Scene.ROGUE_MAIN:
            return True
        elif scene == Scene.ROGUE_NODE_OPTION_SELECT:
            if pos := self.find("rogue/node_check_to_do"):
                self.ctap(pos, 3)
                return
            for option in self.options:
                if pos := find_text(option, 36, scope=((1270, 70), (1920, 940))):
                    self.ctap(pos, 3)
                    return

        else:
            self.scene_graph_step(Scene.ROGUE_MAIN)
