import sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from tools.autonomous_mapping_coordinator import AutonomousMappingCoordinator
from src.common.win32_window import find_poe2_window, get_client_rect, ensure_window_focus

def main():
    hwnd = find_poe2_window()
    if not hwnd:
        print('[ERROR] POE2 window not found!')
        sys.exit(1)

    cl, ct, cw, ch = get_client_rect(hwnd)
    print(f'[LIVE] POE2 Window: HWND={hwnd}, ClientRect=({cl}, {ct}, {cw}x{ch})')
    ensure_window_focus(hwnd)

    coordinator = AutonomousMappingCoordinator(
        screen_w=cw,
        screen_h=ch,
        is_simulated=False,
        
    )
    coordinator.client_left = cl
    coordinator.client_top = ct
    coordinator.hwnd = hwnd

    print('[LIVE] Dismissing modals & centering mouse...')
    coordinator.dismiss_modals()
    time.sleep(0.5)

    print('[LIVE] Executing Stage 2...')
    res = coordinator.execute_stage2_map_device()
    print('[LIVE] Stage 2 Result:', res)

    time.sleep(1.0)
    cap = coordinator.capture_step('STAGE2_LIVE_VERIFIED')
    print(f'[LIVE] Verification capture saved at: {cap}')

    coordinator.dismiss_modals()
    print('[LIVE] Cleaned up modals.')

if __name__ == '__main__':
    main()
