from mower.solvers.infra.enter_room import EnterRoomSolver
from mower.utils import config, rapidocr
from mower.utils.graph import SceneGraphSolver
from mower.utils.image import cropimg
from mower.utils.log import logger
from mower.utils.recognize import Scene
from mower.utils.vector import va

from .utils import clue_cls, clue_scope


class GiveAwaySolver(SceneGraphSolver):
    def run(self, clue_count) -> None:
        self.clue_count = clue_count
        self.friend_clue = []
        if not (config.conf.leifeng_mode or self.clue_count > 9):
            return
        super().run()

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.INFRA_DETAILS:
            self.tap((725, 850))
        elif scene == Scene.INFRA_CONFIDENTIAL:
            self.ctap((1799, 578))
        elif scene == Scene.CLUE_GIVE_AWAY:
            if (c := clue_cls("give_away")) and (
                config.conf.leifeng_mode or self.clue_count > 9
            ):
                if not self.friend_clue:
                    if self.find(
                        "clue/icon_notification", scope=((1400, 0), (1920, 400))
                    ):
                        self.sleep()
                        return
                    for i in range(4):
                        label_scope = ((1450, 228 + i * 222), (1580, 278 + i * 222))
                        if not self.find("clue/label_give_away", scope=label_scope):
                            break
                        name_top_left = (870, 127 + 222 * i)
                        name_scope = (name_top_left, va(name_top_left, (383, 62)))
                        name = rapidocr.engine(
                            cropimg(config.recog.gray, name_scope),
                            use_det=True,
                            use_cls=False,
                            use_rec=True,
                        )[0][0][1]
                        if name:
                            name = name.strip()
                        data = {"name": name}
                        for j in range(1, 8):
                            pos = (1230 + j * 64, 142 + i * 222)
                            data[j] = self.get_color(pos)[0] < 137
                        self.friend_clue.append(data)
                logger.debug(self.friend_clue)
                friend = None
                for idx, fc in enumerate(self.friend_clue):
                    if not fc[c]:
                        friend = idx
                        fc[c] = True
                        break
                friend = friend or 0
                logger.info(f"给{self.friend_clue[friend]['name']}送一张线索{c}")
                self.tap(clue_scope["give_away"])
                self.clue_count -= 1
                self.tap((1790, 200 + friend * 222))
            else:
                self.tap((1868, 54))
                return True
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run("meeting", detail=False)
