from mower.solvers.infra.base_mixin import BaseMixin
from mower.solvers.infra.enter_room import EnterRoomSolver
from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.recognize import Scene

check_order_scope = {"lmb": ((750, 796), (803, 849)), "oru": ((1111, 796), (1164, 849))}


class SwitchOrderSolver(SceneGraphSolver, BaseMixin):
    def run(self, room: str, tar_order: str):
        """
        Args:
            room: 房间名
            tar_order: 目标订单 lmb或oru
        """
        order_type = "龙门币" if tar_order == "lmb" else "合成玉"
        logger.info(f"Start:切换为{order_type}订单 ")
        self.tar_order = tar_order
        self.scope = check_order_scope[tar_order]
        self.room = room
        if (
            self.scene() == Scene.ORDER_LIST
            and not self.detect_room_inside() == self.room
        ):
            EnterRoomSolver().run(self.room, detail=False)
        super().run()

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.INFRA_DETAILS:
            self.ctap((200, 1000), 1, config.screenshot_avg / 1000)
        elif scene == Scene.ORDER_LIST:
            if self.find(f"switch_order/{self.tar_order}"):
                return True
            else:
                self.tap((1500, 1000))
        elif scene == Scene.SWITCH_ORDER:
            if self.find("switch_order/check", scope=self.scope):
                self.scene_graph_step(Scene.ORDER_LIST)
            else:
                self.tap(self.scope)

        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run(self.room, detail=False)
