"""
see_attack.py — double-click closest aibat, capture a sequence to SEE whether the
character walks to it and swings, or just stands. Saves s0..s4.png.
"""
import time
import cv2
import numpy as np
from playwright.sync_api import sync_playwright
from session import get_page, viewport
from find_aibat import detect
from shots import shot_path


def grab(page, name):
    open(name, "wb").write(page.screenshot())


def main():
    with sync_playwright() as p:
        browser, page = get_page(p)
        w, h = viewport(page)
        img = cv2.imdecode(np.frombuffer(page.screenshot(), np.uint8), cv2.IMREAD_COLOR)
        _, cands = detect(img)
        small = [c for c in cands if c[2] <= 600]
        if not small:
            print("no small aibat in view"); return
        small.sort(key=lambda c: -c[1])
        mx, my, a = small[0]
        print(f"double-click closest at ({mx:.0f},{my:.0f}) area={int(a)}")
        grab(page, shot_path("s0.png"))
        page.mouse.dblclick(mx, my)
        for i in range(1, 5):
            time.sleep(0.8)
            grab(page, shot_path(f"s{i}.png"))
        print("saved screenshots/s0..s4.png")


if __name__ == "__main__":
    main()
