; Click test for Mosis Designer
#Requires AutoHotkey v2.0

; Configuration
WINDOW_TITLE := "Mosis Designer"
PHONE_WIDTH := 540
PHONE_HEIGHT := 960

; Output file
outFile := A_ScriptDir . "\click_test_result.txt"
if (FileExist(outFile))
    FileDelete(outFile)

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

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

; Find window
hwnd := WinExist(WINDOW_TITLE)
if (!hwnd) {
    Log("ERROR: Window not found")
    ExitApp(1)
}

Log("Window found: " . hwnd)

; Get window info
WinGetPos(&winX, &winY, &winW, &winH, hwnd)
Log("Window position: X=" . winX . " Y=" . winY)
Log("Window size: W=" . winW . " H=" . winH)

; Calculate DPI scale factor
; Expected phone size is 540x960
; Window has some decorations, estimate ~16 pixels width, ~40 height for title+borders
expectedW := 540 + 16
expectedH := 960 + 40
scaleX := winW / expectedW
scaleY := winH / expectedH
Log("Estimated scale X: " . scaleX . " Y: " . scaleY)

; Use average scale (should be similar for both)
scale := (scaleX + scaleY) / 2
Log("Using scale factor: " . scale)

; Calculate client area offset (estimate title bar and borders scaled)
titleBarHeight := Round(31 * scale)
borderWidth := Round(8 * scale)
Log("Estimated title bar: " . titleBarHeight . "px, border: " . borderWidth . "px")

; Client area start
clientX := winX + borderWidth
clientY := winY + titleBarHeight
Log("Client area start: X=" . clientX . " Y=" . clientY)

; Function to convert phone coords to screen coords
PhoneToScreen(phoneX, phoneY) {
    global clientX, clientY, scale
    return {
        x: Round(clientX + phoneX * scale),
        y: Round(clientY + phoneY * scale)
    }
}

; Activate window
WinActivate(hwnd)
Sleep(200)

Log("")
Log("=== Performing Click Test ===")

; Click on Phone app icon (first app in grid)
; Position from home.rml layout: approximately (67, 120) in phone coords
phonePos := PhoneToScreen(67, 120)
Log("Phone app: phone(67,120) -> screen(" . phonePos.x . "," . phonePos.y . ")")

; Perform click
Click(phonePos.x, phonePos.y)
Log("Click sent!")

Sleep(1000)
Log("Test complete - check if screen changed to Dialer")

; Also try clicking messages in dock for comparison
Log("")
Log("=== Second Click Test (Messages Dock) ===")
; Dock is at bottom, approximately y=920 in phone coords
; Messages is second item, approximately x=202
msgPos := PhoneToScreen(202, 920)
Log("Messages dock: phone(202,920) -> screen(" . msgPos.x . "," . msgPos.y . ")")

Click(msgPos.x, msgPos.y)
Log("Click sent!")

Sleep(500)
Log("")
Log("=== Test Complete ===")

ExitApp(0)
