import asyncio
from playwright.async_api import async_playwright

async def run():
    async with async_playwright() as p:
        br = await p.chromium.launch(headless=True)
        pg = await br.new_page(viewport={'width':1400,'height':800})
        await pg.goto('http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html')
        print('로딩 50초...')
        await pg.wait_for_timeout(50000)

        # 기본 위치 유지, 엣지모드만 ON
        r = await pg.evaluate('''() => {
            const app = window.__dzw;
            if(!app._knuckleMesh) return "없음";
            app._knuckleSelect();
            DZW5_EDGEPATH.initBtn(app);
            app._edgeMode = true;
            return "edgeSegs=" + app._knuckleEdgeSegs?.length + " / edgeMode=" + app._edgeMode;
        }''')
        print('준비:', r)
        await pg.screenshot(path='E:\\도진팩토리\\3D스캔및티칭시스템\\edge_before.png')

        # 화면 전체를 grid로 클릭해서 제품 맞추기
        found = False
        for cy in range(200, 700, 50):
            for cx in range(300, 1100, 50):
                await pg.mouse.move(cx, cy)
                await pg.mouse.down(); await pg.wait_for_timeout(50); await pg.mouse.up()
                await pg.wait_for_timeout(200)
                hit = await pg.evaluate('() => !!window.__dzw._edgeChainLine')
                if hit:
                    print(f'엣지 잡힘! ({cx},{cy})')
                    found = True
                    break
            if found:
                break

        if not found:
            print('엣지 잡기 실패 — hit 자체가 없거나 코드 문제')

        await pg.screenshot(path='E:\\도진팩토리\\3D스캔및티칭시스템\\edge_shot.png')
        print('스크린샷 저장')
        await br.close()

asyncio.run(run())
