from datetime import timedelta

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

activity_name = "狂欢游乐好运墙"


class Orundum(SceneGraphSolver):
    solver_name = "幸运墙"
    solver_max_duration = timedelta(minutes=1)

    def run(self):
        self.in_progress = False
        return super().run()

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.INDEX:
            score, scope = config.recog.match("sign_in/orundum/entry")
            if score >= 0.8:
                self.tap(scope)
            else:
                notify(f"未找到{activity_name}签到入口")
                return True
        elif scene == Scene.SIGN_IN_ORUNDUM:
            if self.find("sign_in/orundum/complete"):
                if self.in_progress:
                    logger.info("等待物资领取界面弹出")
                    self.sleep()
                else:
                    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:
                self.sleep()
                notify(f"成功领取{activity_name}奖励")
                return True
            self.tap((960, 960))
        else:
            self.scene_graph_step(Scene.INDEX)
