; Mosis Designer Interactive Test Helper
; AutoHotkey v2
; Launches designer and shows coordinates as you click

#Requires AutoHotkey v2.0
#Include "lib\utils.ahk"

; Global state
global isRunning := true
global hwnd := 0

; Create a simple GUI to show status
CreateStatusGui() {
    global statusGui, statusText, coordText

    statusGui := Gui("+AlwaysOnTop", "Mosis Test Helper")
    statusGui.SetFont("s10")
    statusGui.Add("Text", "w300", "Designer Status:")
    statusText := statusGui.Add("Text", "w300 h20", "Not running")
    statusGui.Add("Text", "w300", "Mouse Position (Phone Coords):")
    coordText := statusGui.Add("Text", "w300 h20", "N/A")
    statusGui.Add("Text", "w300", "")
    statusGui.Add("Button", "w100", "Refresh").OnEvent("Click", RefreshStatus)
    statusGui.Add("Button", "x+10 w100", "Click Test").OnEvent("Click", DoClickTest)
    statusGui.Add("Button", "x+10 w80", "Exit").OnEvent("Click", (*) => ExitApp())

    statusGui.OnEvent("Close", (*) => ExitApp())
    statusGui.Show("x10 y10")
}

; Refresh status display
RefreshStatus(*) {
    global hwnd, statusText

    hwnd := FindDesignerWindow()
    if (hwnd) {
        statusText.Value := "Running (hwnd: " . hwnd . ")"
    } else {
        statusText.Value := "Not running"
    }
}

; Perform a test click at center of phone
DoClickTest(*) {
    global hwnd

    hwnd := FindDesignerWindow()
    if (!hwnd) {
        MsgBox("Designer window not found!")
        return
    }

    ; Click center of phone
    ClickPhone(270, 480, hwnd)
    MsgBox("Clicked at phone center (270, 480)")
}

; Update coordinate display based on mouse position
UpdateCoordinates() {
    global hwnd, coordText

    if (!hwnd) {
        hwnd := FindDesignerWindow()
        if (!hwnd) {
            coordText.Value := "Window not found"
            return
        }
    }

    ; Get mouse position
    CoordMode("Mouse", "Screen")
    MouseGetPos(&mouseX, &mouseY)

    ; Get client area
    client := GetClientArea(hwnd)
    if (!client) {
        coordText.Value := "Could not get client area"
        return
    }

    ; Calculate phone coordinates
    phoneX := mouseX - client.x
    phoneY := mouseY - client.y

    ; Check if within phone bounds
    if (phoneX >= 0 && phoneX < PHONE_WIDTH && phoneY >= 0 && phoneY < PHONE_HEIGHT) {
        coordText.Value := "X: " . phoneX . "  Y: " . phoneY
    } else {
        coordText.Value := "Outside phone area"
    }
}

; Timer to update coordinates
SetTimer(UpdateCoordinates, 100)

; Main
LogMessage("Starting interactive test helper")

; Start designer if not running
if (!FindDesignerWindow()) {
    LogMessage("Starting designer...")
    StartDesigner()
    Sleep(3000)
}

CreateStatusGui()
RefreshStatus()

; Keep running
return
