from datetime import timedelta

from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.recognize import Scene


class TradeTokenSolver(SceneGraphSolver):
    solver_name = "信物兑换"
    solver_max_duration = timedelta(minutes=3)

    def run(self):
        if not config.conf.trade_token:
            return True
        self.finished = False
        return super().run()

    def transition(self):
        if (scene := self.scene()) == Scene.SHOP_TRADE_TOKEN:
            self.finished = True
            self.tap("shop/trade_token_dialog", x_rate=1)
        elif scene == Scene.SHOP_TOKEN:
            if self.finished:
                return True
            elif self.find("shop/token_not_enough"):
                return True
            elif pos := self.find("shop/trade_token_button"):
                self.ctap(pos, 1)
            elif pos := self.find("shop/commendation"):
                self.tap(pos)
            else:
                self.swipe((200, 400), (0, 400))
        else:
            self.scene_graph_step(Scene.SHOP_TOKEN)
