from datetime import datetime, timedelta

from mower.utils.email import send_message
from mower.utils.graph import SceneGraphSolver
from mower.utils.recognize import Scene

from .utils.utils import find_text


class RogueSelectTeamSolver(SceneGraphSolver):
    solver_name = "肉鸽选择分队"

    def run(self, team: str) -> bool:
        self.team = team
        self.success = False
        self.start_time = datetime.now()
        super().run()
        return self.success

    def transition(self):
        if datetime.now() - self.start_time > timedelta(minutes=1):
            send_message(f"肉鸽选择分队超时,请检查{self.team}是否解锁", level="ERROR")
            return True
        if (scene := self.scene()) == Scene.ROGUE_TEAM_SELECT:
            if self.animation(interval=0.2):
                return
            if pos := self.find("rogue/team_check_to_do"):
                self.ctap(pos, 3)
                self.success = True
                return
            if pos := find_text(self.team, 39, scope=((0, 660), (1920, 725))):
                self.tap(pos)
            else:
                self.swipe_noinertia((800, 800), (-600, 0))

        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            return True
