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.digit_reader import get_drone
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.recognize import Scene


class DroneSolver(SceneGraphSolver, BaseMixin):
    solver_name = "无人机加速"

    def run(
        self,
        room: str,
        count: int | None = None,
        all_in: bool = False,
        cur_count: int = None,
    ) -> bool:
        """
        Args:
            room: 房间名
            count:消耗无人机数量
            all_in:贸易站-加速完成一笔订单，制造站-消耗全部无人机消耗数量
            cur_count:当前无人机数量
        """
        self.room = room
        while not all_in and cur_count is None:
            try:
                cur_count = get_drone(config.recog.gray)
            except Exception:
                logger.exception("当前场景无法获取无人机数量")
                EnterRoomSolver().run(self.room, detail=False)

        if not all_in:
            if count is None:
                count = cur_count - config.conf.drone_count_limit
            if cur_count < count or count <= 0:
                logger.info("无人机数量不足")
                return False

        self.count = count
        self.all_in = all_in
        self.success = False
        super().run()
        return True

    def number(self, scope: tp.Scope, height: int, thres: int) -> int:
        "无人机加速数量"
        return config.recog.num.number_int("riic_base", scope, height, thres)

    def transition(self) -> bool:
        if (
            scene := self.scene()
        ) == Scene.INFRA_DETAILS and self.detect_room() == self.room:
            self.ctap((200, 1000), 1)
        elif (
            scene in [Scene.ORDER_LIST, Scene.FACTORY_ROOMS]
            and self.detect_room_inside() == self.room
        ):
            if self.success:
                return True
            elif pos := self.find("factory_accelerate"):
                self.tap(pos)
            elif pos := self.find("bill_accelerate"):
                self.tap(pos)

        elif scene == Scene.DRONE_ACCELERATE:
            if self.all_in:
                if self.find("all_in"):
                    self.tap((1450, 500))
                    self.success = True
                elif self.success:
                    self.ctap((1450, 900), 3)
            elif (
                tap_count := self.count - self.number(((240, 650), (350, 720)), 40, 150)
            ) == 0:
                self.tap((1450, 900))
                self.success = True
            elif tap_count > 0:
                for _ in range(tap_count):
                    self.tap((1300, 500))
                    self.sleep(0.07)
                self.sleep(0.2)
            elif tap_count < 0:
                for _ in range(-tap_count):
                    self.tap((900, 500))
                    self.sleep(0.07)
                self.sleep(0.2)
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run(self.room, detail=False)
