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


class TradeTokenSolver(SceneGraphSolver):
    def run(self):
        logger.info("Start: 兑换溢出信物")
        self.finished = False
        super().run()

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