from datetime import datetime, timedelta

from mower.utils import config
from mower.utils.email import send_message
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.recognize import Scene

from .choose_next_node import ChooseNextNodeSolver
from .choose_support_award import ChooseSupportAwardSolver
from .node_option_select import NodeOptionSelectSolver
from .rogue_abandon_explore import RogueAbandonExploreSolver
from .rogue_navi import RogueNaviSolver
from .rogue_select_team import RogueSelectTeamSolver
from .rogue_shop import RogueShopSolver
from .utils.utils import detect_layer


class RogueSolver(SceneGraphSolver):
    solver_name = "肉鸽"

    def run(self) -> bool:
        if not config.conf.RG:
            return True
        if config.conf.maa_rg_theme != "Sarkaz" or config.conf.rogue.mode != 1:
            return True
        if self.scheduler_stop_time:
            self.deadline = self.scheduler_stop_time - timedelta(minutes=1)
        else:
            self.deadline = None
        self.full = False  # 打满
        super().run()
        config.conf.maa_rg_enable = 0 if self.full else 1
        config.save_conf()
        return self.full

    def transition(self):
        if self.deadline and self.deadline < datetime.now():
            logger.info("任务打断肉鸽")
            return True
        if (scene := self.scene()) == Scene.ROGUE_INDEX:
            if self.animation(((0, 100), (450, 800)), interval=0.2, ratio=0.5):
                return
            self.mode = "full_money" if config.conf.rogue.mode == 1 else "full_level"
            if self.find(f"rogue/{self.mode}/{config.conf.maa_rg_theme}"):
                self.full = True
                text = "源石锭" if config.conf.rogue.mode == 1 else "等级"
                send_message(
                    f"肉鸽:{config.conf.maa_rg_theme}{text}已刷满",
                    level="WARNING",
                )
                return True
            self.ctap((1800, 1000), 3)
        elif scene == Scene.ROGUE_TEAM_SELECT:
            return not RogueSelectTeamSolver().run("蓝图测绘分队")

        elif scene == Scene.ROGUE_AWARD_BEFORE_EXPLORE:
            ChooseSupportAwardSolver().run()

        elif scene == Scene.ROGUE_INIT_RECRUIT:
            if pos := self.find("rogue/green_recruit"):
                self.ctap(pos, 2)
                return
            self.tap((1820, 540))
        elif scene == Scene.ROGUE_RECRUIT_AGENT_SELECT:
            self.ctap((1450, 1010), 3)

        elif scene == Scene.ROGUE_ABANDON_RECRUIT:
            self.ctap("double_confirm/main", 2, x_rate=1)

        elif scene == Scene.ROGUE_RECRUIT_SELECT:
            self.tap((330, 870))

        elif scene == Scene.ROGUE_MAIN:
            if config.conf.rogue.mode == 1 and detect_layer() != 1:
                RogueAbandonExploreSolver().run()
                return
            ChooseNextNodeSolver().run()

        elif scene in [Scene.ROGUE_NODE_OPTION_SELECT, Scene.ROGUE_NODE_LOADING]:
            NodeOptionSelectSolver().run()

        elif scene == Scene.ROGUE_LAYER_LOADING:
            self.scene_graph_step(Scene.ROGUE_MAIN)

        elif scene in [
            Scene.ROGUE_SHOP,
            Scene.ROGUE_INVESTMENT,
            Scene.ROGUE_INVESTMENT_PORTAL,
        ]:
            RogueShopSolver().run()

        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            RogueNaviSolver().run()
