; Visual click test - verifies clicks by window interaction
#Requires AutoHotkey v2.0

; Configuration
DESIGNER_EXE := A_ScriptDir . "\..\build\Debug\mosis-designer.exe"
HOME_RML := A_ScriptDir . "\..\..\src\main\assets\apps\home\home.rml"
WINDOW_TITLE := "Mosis Designer"

resultFile := A_ScriptDir . "\visual_test_result.txt"
if (FileExist(resultFile))
    FileDelete(resultFile)

Log(msg) {
    global resultFile
    FileAppend(msg . "`n", resultFile, "UTF-8")
}

Log("=== Visual Click Test ===")
Log("Time: " . FormatTime(, "yyyy-MM-dd HH:mm:ss"))

; Check if designer is running, if not start it
hwnd := WinExist(WINDOW_TITLE)
if (!hwnd) {
    Log("Starting designer...")
    Run(DESIGNER_EXE . ' "' . HOME_RML . '"', A_ScriptDir)

    ; Wait for window
    startTime := A_TickCount
    while (A_TickCount - startTime < 15000) {
        hwnd := WinExist(WINDOW_TITLE)
        if (hwnd)
            break
        Sleep(200)
    }

    if (!hwnd) {
        Log("ERROR: Window did not appear")
        ExitApp(1)
    }
    Log("Designer started")
    Sleep(3000)  ; Wait for full initialization
} else {
    Log("Using existing designer window")
}

Log("Window HWND: " . hwnd)

; Get window info
WinGetPos(&winX, &winY, &winW, &winH, hwnd)
Log("Window: " . winW . "x" . winH . " at (" . winX . "," . winY . ")")

; Calculate scale and client area
scale := (winW / 556 + winH / 1000) / 2
titleBar := Round(31 * scale)
border := Round(8 * scale)
clientX := winX + border
clientY := winY + titleBar
Log("Scale: " . scale . ", Client offset: (" . border . "," . titleBar . ")")

; Activate window
WinActivate(hwnd)
WinWaitActive(hwnd, , 3)
Sleep(500)

Log("")
Log("=== Click Sequence ===")

; Function to click
DoClick(phoneX, phoneY, name) {
    global clientX, clientY, scale, hwnd

    screenX := Round(clientX + phoneX * scale)
    screenY := Round(clientY + phoneY * scale)

    Log("Clicking " . name . " at (" . phoneX . "," . phoneY . ") -> screen(" . screenX . "," . screenY . ")")

    ; Make sure window is active
    WinActivate(hwnd)
    Sleep(100)

    ; Move mouse first, then click
    MouseMove(screenX, screenY)
    Sleep(50)
    Click()
    Sleep(100)

    Log("Click sent")
}

; Test sequence:
; 1. Click on Phone app (should navigate to dialer)
Log("")
Log("Step 1: Click Phone app icon")
DoClick(67, 120, "Phone")
Sleep(2000)

; 2. Click somewhere to go back (simulate back button or click home dock)
Log("")
Log("Step 2: Wait and observe...")
Sleep(1000)

; 3. Click on Messages dock
Log("")
Log("Step 3: Click Messages dock")
DoClick(202, 920, "Messages Dock")
Sleep(2000)

; 4. Click on Contacts dock
Log("")
Log("Step 4: Click Contacts dock")
DoClick(337, 920, "Contacts Dock")
Sleep(2000)

Log("")
Log("=== Test Sequence Complete ===")
Log("Please verify visually that navigation occurred:")
Log("- After Phone click: Should show Dialer screen")
Log("- After Messages click: Should show Messages screen")
Log("- After Contacts click: Should show Contacts screen")
Log("")
Log("Leaving designer running for manual inspection.")
Log("Close it manually when done.")

ExitApp(0)
