"""
attack_probe.py — Ctrl+Click at a viewport fraction on the live game to test the
attack/targeting mechanic. Aim at a monster (its nameplate is a good target).

Run:  python attack_probe.py [xfrac yfrac]
e.g.  python attack_probe.py 0.42 0.30   (42% across, 30% down)
Default: 0.5 0.38
"""
import sys
import time
from playwright.sync_api import sync_playwright
from session import get_page, viewport
from shots import shot_path


def main():
    xf = float(sys.argv[1]) if len(sys.argv) > 1 else 0.5
    yf = float(sys.argv[2]) if len(sys.argv) > 2 else 0.38
    with sync_playwright() as p:
        browser, page = get_page(p)
        w, h = viewport(page)
        x, y = w * xf, h * yf
        page.screenshot(path=shot_path("atk_before.png"))
        page.keyboard.down("Control")
        page.mouse.click(x, y)
        page.keyboard.up("Control")
        time.sleep(2.5)
        page.screenshot(path=shot_path("atk_after.png"))
        print(f"Ctrl+Click at ({x:.0f},{y:.0f}) = ({xf},{yf}) of {w}x{h}. "
              f"Saved screenshots/atk_before.png / atk_after.png.")


if __name__ == "__main__":
    main()
