from datetime import datetime, timedelta

from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.scene import Scene

from .utils import notify

activity_name = "狂欢游乐好运墙"


class Orundum(SceneGraphSolver):
    solver_name = "幸运墙"

    def run(self):
        logger.info(f"Start: {activity_name}")
        self.in_progress = False
        self.start_time = datetime.now()
        self.success = False
        super().run()
        return self.success

    def transition(self) -> bool:
        if datetime.now() - self.start_time > timedelta(minutes=2):
            notify(f"{activity_name}签到超时", level="ERROR")
            return True
        elif (scene := self.scene()) == Scene.INDEX:
            score, scope = config.recog.match("sign_in/orundum/entry")
            if score >= 0.8:
                self.tap(scope)
                self.in_progress = True
            else:
                notify(f"未找到{activity_name}签到入口")
                self.success = True
                return True
        elif scene == Scene.SIGN_IN_ORUNDUM:
            if self.find("sign_in/orundum/complete"):
                if self.in_progress:
                    self.sleep()
                else:
                    self.success = True
                    notify(f"今日{activity_name}奖励已领完")
                    return True
            for x in range(445, 1520, 213):
                if self.find("sign_in/orundum/choose"):
                    self.tap((x, 415))
                elif pos := self.find("sign_in/orundum/confirm"):
                    self.in_progress = True
                    self.ctap(pos)
                else:
                    self.sleep()
        elif scene == Scene.MATERIEL:
            if self.in_progress:
                notify(f"成功领取{activity_name}奖励")
                self.success = True
                return True
            self.tap((960, 960))
        else:
            self.scene_graph_step(Scene.INDEX)
