# Audio URL
$url = "https://www.myinstants.com/media/sounds/fart-with-reverb.mp3"
$temp = "$env:TEMP\fart.mp3"

# Download the MP3 if it doesn't exist (with error handling)
if (-not (Test-Path $temp)) {
    try {
        Write-Host "Downloading audio file..."
        Invoke-WebRequest $url -OutFile $temp -ErrorAction Stop
    }
    catch {
        Write-Error "Failed to download audio: $_"
        exit 1
    }
}

# Number of times to play
$times = 5

Write-Host "Diggin' in my butt"

# Launch sounds with 1-second stagger in background thread
$soundsRunspace = [runspacefactory]::CreateRunspace()
$soundsRunspace.Open()
$soundsPowershell = [powershell]::Create().AddScript({
    param($audioPath, $count)
    for ($i = 1; $i -le $count; $i++) {
        Start-Process -WindowStyle Hidden -FilePath $audioPath
        Start-Sleep -Seconds 1
    }
}).AddArgument($temp).AddArgument($times)
$soundsPowershell.Runspace = $soundsRunspace
$soundsHandle = $soundsPowershell.BeginInvoke()

# Launch TTS concurrently
$ttsRunspace = [runspacefactory]::CreateRunspace()
$ttsRunspace.Open()
$ttsPowershell = [powershell]::Create().AddScript({
    Add-Type -AssemblyName System.Speech
    $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $tts.Speak("RATTED LOL")
})
$ttsPowershell.Runspace = $ttsRunspace
$ttsHandle = $ttsPowershell.BeginInvoke()

# Show popup immediately (runs on main thread)
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show("RATTED LOL", "Alert", "OK", "Information") | Out-Null

# Wait for background tasks
$soundsPowershell.EndInvoke($soundsHandle)
$soundsPowershell.Dispose()
$soundsRunspace.Close()

$ttsPowershell.EndInvoke($ttsHandle)
$ttsPowershell.Dispose()
$ttsRunspace.Close()

Write-Host "ur done fam"