from datetime import datetime, timedelta

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

from .utils import notify


class Headhunting(SceneGraphSolver):
    solver_name = "每日赠送单抽"

    def run(self):
        logger.info("Start: 每日赠送单抽")
        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("每日赠送单抽签到超时", level="ERROR")
            return True
        elif (scene := self.scene()) == Scene.HEADHUNTING:
            if self.find("sign_in/headhunting/available"):
                self.tap_element("pull_once")
            else:
                self.success = True
                notify("今日赠送单抽已抽完")
                return True
        elif scene == Scene.HEADHUNTING_FREE_CONFIRM:
            self.tap_element("double_confirm/main", x_rate=1)
        elif scene == Scene.HEADHUNTING_RESULT:
            self.success = True
            notify("成功抽完赠送单抽")
            return True
        else:
            self.scene_graph_step(Scene.HEADHUNTING)
