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

collect = {"bill": "订单", "factory": "制造站产物", "trust": "信赖"}


class TodoListSolver(SceneGraphSolver):
    solver_name = "收取产物"

    def run(self):
        self.success = False
        super().run()

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.INFRA_MAIN:
            if self.success:
                return True
            if pos := self.find("infra_notification"):
                self.tap(pos)
            else:
                self.success = True
                return True

        elif scene == Scene.INFRA_TODOLIST:
            if self.success:
                self.scene_graph_step(Scene.INFRA_MAIN)
            for res, name in collect.items():
                score, pos = config.recog.match(f"todo_list/{res}")
                if score > 0.8:
                    logger.info(f"收取{name}")
                    self.ctap(pos)
                    return
            self.success = True

        else:
            self.scene_graph_step(Scene.INFRA_MAIN)
