; Full click test with output verification
#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"
PHONE_WIDTH := 540
PHONE_HEIGHT := 960

; Output files
logFile := A_ScriptDir . "\designer_output.log"
resultFile := A_ScriptDir . "\full_test_result.txt"

if (FileExist(logFile))
    FileDelete(logFile)
if (FileExist(resultFile))
    FileDelete(resultFile)

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

Log("=== Full Click Test with Output Capture ===")
Log("Time: " . FormatTime(, "yyyy-MM-dd HH:mm:ss"))
Log("")

; Start designer with output redirection
Log("Starting designer...")
cmd := '"' . DESIGNER_EXE . '" "' . HOME_RML . '" > "' . logFile . '" 2>&1'
Log("Command: " . cmd)

; Run via cmd to handle redirection
Run(A_ComSpec . " /c " . cmd, A_ScriptDir, , &pid)
Log("Started with PID: " . pid)

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

if (!hwnd) {
    Log("ERROR: Window did not appear within 15 seconds")
    ExitApp(1)
}
Log("Window appeared: " . hwnd)

; Wait for document to load
Sleep(2000)

; Get window dimensions and calculate scale
WinGetPos(&winX, &winY, &winW, &winH, hwnd)
Log("Window: X=" . winX . " Y=" . winY . " W=" . winW . " H=" . winH)

; Calculate scale
expectedW := 540 + 16
expectedH := 960 + 40
scale := (winW / expectedW + winH / expectedH) / 2
Log("DPI Scale: " . scale)

; Calculate client offset
titleBar := Round(31 * scale)
border := Round(8 * scale)
clientX := winX + border
clientY := winY + titleBar
Log("Client: X=" . clientX . " Y=" . clientY)

; Activate window
WinActivate(hwnd)
Sleep(300)

; Function to click at phone coordinates
ClickAt(phoneX, phoneY, desc) {
    global clientX, clientY, scale, hwnd
    screenX := Round(clientX + phoneX * scale)
    screenY := Round(clientY + phoneY * scale)
    Log("Click " . desc . ": phone(" . phoneX . "," . phoneY . ") -> screen(" . screenX . "," . screenY . ")")

    ; Activate and click
    WinActivate(hwnd)
    Sleep(50)
    Click(screenX, screenY)
    Sleep(500)
}

Log("")
Log("=== Test 1: Click Phone App ===")
ClickAt(67, 120, "Phone app")
Sleep(1500)

; Check log for navigation
Log("Checking log for navigation...")
if (FileExist(logFile)) {
    content := FileRead(logFile)
    if (InStr(content, "Navigated to: dialer") || InStr(content, "Loaded screen: apps/dialer")) {
        Log("SUCCESS: Navigation to dialer detected!")
    } else if (InStr(content, "navigateTo called")) {
        Log("PARTIAL: navigateTo was called but navigation may have failed")
    } else {
        Log("FAIL: No navigation detected")
    }
} else {
    Log("WARNING: Log file not found")
}

Log("")
Log("=== Test 2: Click Messages Dock ===")
ClickAt(202, 920, "Messages dock")
Sleep(1500)

if (FileExist(logFile)) {
    content := FileRead(logFile)
    if (InStr(content, "messages")) {
        Log("SUCCESS: Messages-related activity detected")
    } else {
        Log("FAIL: No messages navigation detected")
    }
}

Log("")
Log("=== Cleaning Up ===")
; Close designer
WinClose(hwnd)
Sleep(500)

; Dump the full log
Log("")
Log("=== Designer Log Contents ===")
if (FileExist(logFile)) {
    content := FileRead(logFile)
    ; Only show last part of log (navigation related)
    lines := StrSplit(content, "`n")
    Log("(Last 30 lines)")
    startIdx := lines.Length > 30 ? lines.Length - 30 : 1
    loop lines.Length - startIdx + 1 {
        Log(lines[startIdx + A_Index - 1])
    }
}

Log("")
Log("=== Test Complete ===")
ExitApp(0)
