#SingleInstance Force
#Persistent
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
    ExitApp
}

; Define constants
DEVICE_NAME := "SteelSeries Sonar - Media"
VOLUME_STEP := 1311

; Create GUI for OSD
Gui, +AlwaysOnTop +ToolWindow -Caption +LastFound
Gui, Color, 000000
Gui, Font, s20 cFFFFFF, Segoe UI
Gui, Add, Text, vVolumeText w180 x10, Volume: --`%  ; Reduced width to 180, left-aligned with 10px margin
WinSet, Transparent, 200
Gui, +E0x20 ; Click through

; Global variables to track volume
global currentVolume := 0
global isMuted := 0

; Function to show volume OSD
ShowVolumeOSD() {
    ; Update OSD text
    if (isMuted) {
        GuiControl,, VolumeText, Volume: Muted
    } else {
        GuiControl,, VolumeText, Volume: %currentVolume%`%
    }

    ; Position the OSD in the center of the screen
    SysGet, MonitorWorkArea, MonitorWorkArea
    OSDWidth := 300
    OSDHeight := 60
    xPos := MonitorWorkAreaRight - OSDWidth / 2
    yPos := MonitorWorkAreaBottom - OSDHeight / 2

    ; Show the OSD
    Gui, Show, NoActivate w%OSDWidth% h%OSDHeight% x%xPos% y%yPos%

    ; Auto-hide after 1.5 seconds
    SetTimer, HideOSD, -1500
}

; Auto-hide function
HideOSD:
    Gui, Hide
return

; Initialize volume (try to get initial value)
InitializeVolume:
    tempFile := A_Temp . "\volume_init.txt"
    RunWait, %comspec% /c C:\Nircmd\nircmd.exe setsysvolume 0 "%DEVICE_NAME%" > "%tempFile%" 2>&1,, Hide
    FileRead, volumeOutput, %tempFile%
    RegExMatch(volumeOutput, "Before change.* (\d+)", volumeMatch)
    volume := volumeMatch1
    if (volume != "") {
        currentVolume := Round((volume / 65535) * 100)
    }
    FileDelete, %tempFile%

    ; Check mute state
    tempFile := A_Temp . "\mute_init.txt"
    RunWait, %comspec% /c C:\Nircmd\nircmd.exe mutesysvolume 3 "%DEVICE_NAME%" > "%tempFile%" 2>&1,, Hide
    FileRead, muteOutput, %tempFile%
    isMuted := InStr(muteOutput, "muted")
    FileDelete, %tempFile%
return

; Run initialization on startup
GoSub, InitializeVolume

Volume_Up::
    ; Calculate new volume
    newVolume := currentVolume + 2  ; Approximately 2% per step
    if (newVolume > 100)
        newVolume := 100

    ; Update our tracker
    currentVolume := newVolume
    isMuted := 0

    ; Execute volume change
    Run, C:\Nircmd\nircmd.exe changesysvolume %VOLUME_STEP% "%DEVICE_NAME%",, Hide

    ; Show the OSD
    ShowVolumeOSD()
Return

Volume_Down::
    ; Calculate new volume
    newVolume := currentVolume - 2  ; Approximately 2% per step
    if (newVolume < 0)
        newVolume := 0

    ; Update our tracker
    currentVolume := newVolume

    ; Execute volume change
    Run, C:\Nircmd\nircmd.exe changesysvolume -%VOLUME_STEP% "%DEVICE_NAME%",, Hide

    ; Show the OSD
    ShowVolumeOSD()
Return

Volume_Mute::
    ; Toggle mute state
    isMuted := !isMuted

    ; Execute mute toggle
    Run, C:\Nircmd\nircmd.exe mutesysvolume 2 "%DEVICE_NAME%",, Hide

    ; Show the OSD
    ShowVolumeOSD()
Return
