"""Advance real appraisal dialogue while waiting for a specific Onyon arrival."""
import time


class AppraisalArrival:
    def delivered(self, location, baseline, timeout=300):
        deadline=time.monotonic()+timeout
        while time.monotonic()<deadline:
            state=self.game.wait_state(lambda s:True)
            if isinstance(baseline,dict) and 'physical_deliveries' in baseline:
                arrived=state.get('physical_deliveries',{}).get(str(location),0)>baseline['physical_deliveries'].get(str(location),0)
            else:
                old=baseline['delivery_count'] if isinstance(baseline,dict) else baseline
                arrived=state.get('delivery_count',0)>old and state.get('last_delivery_location')==location
            if arrived:return self.record('actual-delivery',state,location=location)
            movie=state.get('movie_state',0)
            if state.get('paused') and movie==0 and state.get('ui_scene') in (10010,10013):
                self.game.press(self.driver.START,frames=8)
            elif movie or state.get('movie_pause') or state.get('ui_scene')==10018:
                self.game.press(self.driver.A,frames=8)
                self.game.input(frames=20)
            else:self.game.input(frames=20)
        raise TimeoutError(f'actual specific Onyon arrival for {location}: {state}')
