from mower.solvers.infra.base_mixin import BaseMixin
from mower.solvers.infra.enter_room import EnterRoomSolver
from mower.utils import config
from mower.utils import typealias as tp
from mower.utils.graph import SceneGraphSolver
from mower.utils.recognize import Scene


class GetRemainTimeSolver(SceneGraphSolver, BaseMixin):
    solver_name = "获取剩余时间"

    def run(
        self,
        room: str,
    ):
        "返回剩余时间,换算为秒"
        self.room = room
        super().run()
        return self.res

    def number(self, scope: tp.Scope, height: int, thres: int) -> str:
        "数字识别"
        rect_limits = [{"w": 7, "h": 7, "char": ":"}]
        return config.recog.num.number(
            "riic_base", scope, height, thres, rect_limits=rect_limits
        )

    def read_remain_time(self) -> int:
        h, m, s = self.number(((758, 670), (960, 705)), 30, 100).split("::")
        return int(h) * 3600 + int(m) * 60 + int(s)

    def transition(self) -> bool:
        if (
            scene := self.scene()
        ) == Scene.INFRA_DETAILS and self.detect_room() == self.room:
            self.ctap((200, 1000), 1)
        elif scene == Scene.FACTORY_ROOMS and self.detect_room_inside() == self.room:
            if pos := self.find("factory_accelerate"):
                self.tap(pos)

        elif scene == Scene.DRONE_ACCELERATE:
            if res := self.read_remain_time():
                self.res = res
                return True
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run(self.room, detail=False)
