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

from .utils import is_full


class SSSOpeSolver(SceneGraphSolver):
    solver_name = "保全代理"

    def run(self) -> bool:
        self.success = False  # 是否刷满
        self.check = False  # 是否可代理层数小于设置要求
        super().run()
        return self.success

    def transition(self):
        if (scene := self.scene()) == Scene.SSS_START:
            if self.find("sss/ope/prts") and not self.check:
                self.tap("sss/ope/prts")
                return
            elif self.find("sss/ope/elimi_agency_on") and self.check:
                self.tap("sss/ope/elimi_agency_on")
                return
            self.tap((1555, 966))

        elif scene in [Scene.SSS_EC, Scene.SSS_EC_EX] and self.check:
            return True
        elif scene == Scene.SSS_ELIMI_AGENCY:
            if is_full():
                self.success = True
                return True
            if (
                config.recog.num.number_int(
                    "riic_base",
                    ((1750, 50), (1850, 150)),
                    50,
                    120,
                    target_range=range(1, 7),
                )
                < config.conf.sss.ope_limit_stage
            ):
                self.scene_graph_step(Scene.SSS_START)
                self.check = True
                return
            self.tap("sss/ope/use")
        else:
            self.scene_graph_step(Scene.SSS_START)
