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.image import cropimg
from mower.utils.log import logger
from mower.utils.rapidocr import ocr_rec
from mower.utils.recognize import Scene

from .utils import clue_cls, exit_pos


class ReceiveSolver(SceneGraphSolver, BaseMixin):
    solver_name = "接收好友线索"

    def run(self):
        super().run()

    def transition(self) -> bool:
        if (
            scene := self.scene()
        ) == Scene.INFRA_DETAILS and self.detect_room() == "meeting":
            self.tap((725, 850))
        elif scene == Scene.INFRA_CONFIDENTIAL:
            receive_scope = ((1822, 365), (1886, 400))
            if self.find("clue/badge_new", scope=receive_scope):
                self.ctap((1800, 430))
            else:
                return True
        elif scene == Scene.CLUE_RECEIVE:
            if self.find("infra_complete/信用", scope=((1440, 130), (1920, 193))):
                self.sleep()
                return
            if clue := clue_cls("receive"):
                name_scope = ((1580, 220), (1880, 255))
                name_img = cropimg(config.recog.gray, name_scope)
                name = ocr_rec(name_img) or "好友"
                logger.info(f"接收{name}的{clue}号线索")
                self.tap(name_scope)
            else:
                self.tap(exit_pos)
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run("meeting", detail=False)
