from datetime import datetime, timedelta

from mower.solvers.infra.base_mixin import BaseMixin
from mower.solvers.infra.enter_room import EnterRoomSolver
from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.recognize import Scene


class WaitForProductSolver(SceneGraphSolver, BaseMixin):
    def run(self, room, wait_time) -> None:
        self.room = room
        self.wait_start()
        self.sleep(wait_time)
        super().run()

    def timeout(self) -> bool:
        return datetime.now() > self.start_time + timedelta(seconds=180)

    def wait_start(self):
        self.start_time = datetime.now()

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.INFRA_DETAILS:
            self.ctap((200, 1000), 1, config.screenshot_avg / 1000)
        elif scene == Scene.FACTORY_ROOMS:
            if self.detect_product_complete():
                return True
            elif self.timeout():
                return True
            else:
                self.tap((1750, 950))
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            EnterRoomSolver().run(self.room, detail=False)
