# Helpers for driving the tiletopia window from PowerShell.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type @'
using System;
using System.Runtime.InteropServices;
public class W {
  [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
  [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
  [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);
  [DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
  [DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint dx, uint dy, uint dwData, int extraInfo);
  [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
}
'@

function Get-TileWindow {
  $p = Get-Process tiletopia -ErrorAction Stop | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1
  if (-not $p) { throw "no tiletopia window" }
  return $p
}

function Bring-ToFront {
  $p = Get-TileWindow
  [W]::ShowWindow($p.MainWindowHandle, 9) | Out-Null  # SW_RESTORE
  [W]::SetForegroundWindow($p.MainWindowHandle) | Out-Null
  Start-Sleep -Milliseconds 300
  $r = New-Object W+RECT
  [W]::GetWindowRect($p.MainWindowHandle, [ref] $r) | Out-Null
  return $r
}

function Save-Shot([string]$path) {
  $r = Bring-ToFront
  $w = $r.Right - $r.Left
  $h = $r.Bottom - $r.Top
  $bmp = New-Object System.Drawing.Bitmap $w, $h
  $g = [System.Drawing.Graphics]::FromImage($bmp)
  $g.CopyFromScreen($r.Left, $r.Top, 0, 0, (New-Object System.Drawing.Size $w, $h))
  $bmp.Save($path)
  $g.Dispose()
  $bmp.Dispose()
  Write-Host ("saved " + $path + " (" + $w + "x" + $h + " at " + $r.Left + "," + $r.Top + ")")
  return @{ x = $r.Left; y = $r.Top; w = $w; h = $h }
}

# Click at *window-relative* coordinates (so we don't depend on the window's
# screen position from one run to the next).
function Click-Win([int]$relX, [int]$relY) {
  $r = Bring-ToFront
  $absX = $r.Left + $relX
  $absY = $r.Top + $relY
  [W]::SetCursorPos($absX, $absY) | Out-Null
  Start-Sleep -Milliseconds 80
  [W]::mouse_event(0x0002, 0, 0, 0, 0)  # LEFTDOWN
  Start-Sleep -Milliseconds 30
  [W]::mouse_event(0x0004, 0, 0, 0, 0)  # LEFTUP
  Start-Sleep -Milliseconds 120
  Write-Host ("clicked window-rel " + $relX + "," + $relY + " (abs " + $absX + "," + $absY + ")")
}
