import asyncio
from playwright.async_api import async_playwright

async def run():
    async with async_playwright() as p:
        br = await p.chromium.launch(channel='msedge', headless=False, args=['--start-maximized'])
        ctx = await br.new_context(no_viewport=True)
        pg = await ctx.new_page()
        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 "knuckleMesh 없음";
            app._knuckleSelect();
            DZW5_EDGEPATH.initBtn(app);
            app._edgeMode = true;
            const btn = document.getElementById("dzw-edge-mode-btn");
            if(btn){ btn.textContent="✏️ 엣지 선따기 ON"; btn.style.background="rgba(0,255,170,.25)"; }
            return "edgeSegs=" + app._knuckleEdgeSegs?.length + " edgeMode=" + app._edgeMode;
        }''')
        print('준비:', r)
        await pg.wait_for_timeout(500)

        # 화면 크기 확인
        size = await pg.evaluate('() => ({w: window.innerWidth, h: window.innerHeight})')
        cx = size['w'] // 2
        cy = size['h'] // 2
        print(f'화면 {size}, 클릭 중앙=({cx},{cy})')

        # 여러 지점 클릭해서 엣지 잡기
        for dx, dy in [(0,0),(50,0),(-50,0),(0,50),(0,-50),(80,80),(-80,80)]:
            await pg.mouse.move(cx+dx, cy+dy)
            await pg.wait_for_timeout(100)
            await pg.mouse.down()
            await pg.wait_for_timeout(80)
            await pg.mouse.up()
            await pg.wait_for_timeout(300)
            hit = await pg.evaluate('() => !!window.__dzw._edgeChainLine')
            if hit:
                print(f'엣지 체인 발견! ({cx+dx},{cy+dy})')
                break

        chain_ok = await pg.evaluate('() => !!window.__dzw._edgeChainLine')
        print('최종 엣지 체인 표시됨:', chain_ok)
        await pg.screenshot(path='edge_click_result.png')
        print('스크린샷: edge_click_result.png')
        await pg.wait_for_timeout(120000)
        await br.close()

asyncio.run(run())
