function Set-EmmaDisplayRotation {
    param(
        [string]$ScriptDir = 'C:\Emma\current\provisioning',
        [int]$DisplayId = 0,
        [int]$Rotation = 270
    )

    $orientationScript = Join-Path $ScriptDir 'orientation-script.ps1'
    if (-not (Test-Path $orientationScript)) {
        Write-Warning "orientation-script.ps1 nicht gefunden unter $orientationScript"
        return
    }

    Write-Host "Bildschirmrotation: Display $DisplayId -> ${Rotation} Grad"
    Start-Process powershell.exe `
        -ArgumentList "-ExecutionPolicy Bypass -File `"$orientationScript`" $DisplayId $Rotation" `
        -Wait
}

function Disable-EmmaTouchHold {
    Write-Host 'Deaktiviere Rechtsklick via Touch...'

    New-Item -Path 'HKCU:\Software\Microsoft\Wisp\Touch' -Force | Out-Null
    New-ItemProperty -Path 'HKCU:\Software\Microsoft\Wisp\Touch' -Name 'TouchMode_hold' -PropertyType DWord -Value 0 -Force | Out-Null
}

function Disable-EmmaEdgeGestures {
    Write-Host 'Deaktiviere Touch-Kanten-Gesten und blende Taskleiste aus...'

    $edgeUiPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\EdgeUI'
    if (-not (Test-Path $edgeUiPath)) {
        New-Item -Path $edgeUiPath -Force | Out-Null
    }
    Set-ItemProperty -Path $edgeUiPath -Name 'AllowEdgeSwipe' -Value 0 -Type DWord -Force

    $advancedPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
    if (-not (Test-Path $advancedPath)) {
        New-Item -Path $advancedPath -Force | Out-Null
    }
    Set-ItemProperty -Path $advancedPath -Name 'TaskbarAutoHideInTabletMode' -Value 1 -Type DWord -Force
    Set-ItemProperty -Path $advancedPath -Name 'TaskbarSizeMove' -Value 0 -Type DWord -Force

    $stuckRectsPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3'
    try {
        if (-not (Test-Path $stuckRectsPath)) {
            New-Item -Path $stuckRectsPath -Force | Out-Null
        }

        $settings = (Get-ItemProperty -Path $stuckRectsPath -Name Settings -ErrorAction SilentlyContinue).Settings
        if ($settings -and $settings.Length -gt 8) {
            $bytes = [byte[]]$settings.Clone()
            $bytes[8] = 3
            Set-ItemProperty -Path $stuckRectsPath -Name Settings -Value $bytes -Type Binary -Force
        }
    }
    catch {
        Write-Warning "Taskleisten-Auto-Ausblenden konnte nicht gesetzt werden: $_"
    }
}

function Disable-EmmaScreenSaverAndSleep {
    Write-Host 'Deaktiviere Bildschirmschoner und Energiesparmodus...'

    $desktopPath = 'HKCU:\Control Panel\Desktop'
    if (-not (Test-Path $desktopPath)) {
        New-Item -Path $desktopPath -Force | Out-Null
    }
    Set-ItemProperty -Path $desktopPath -Name 'ScreenSaveActive' -Value '0' -Force
    Set-ItemProperty -Path $desktopPath -Name 'ScreenSaveTimeOut' -Value '0' -Force
    Set-ItemProperty -Path $desktopPath -Name 'ScreenSaverIsSecure' -Value '0' -Force

    $policyDesktopPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Control Panel\Desktop'
    if (-not (Test-Path $policyDesktopPath)) {
        New-Item -Path $policyDesktopPath -Force | Out-Null
    }
    Set-ItemProperty -Path $policyDesktopPath -Name 'ScreenSaveActive' -Value '0' -Force
    Set-ItemProperty -Path $policyDesktopPath -Name 'ScreenSaveTimeOut' -Value '0' -Force
    Set-ItemProperty -Path $policyDesktopPath -Name 'ScreenSaverIsSecure' -Value '0' -Force

    $personalizationPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization'
    if (-not (Test-Path $personalizationPath)) {
        New-Item -Path $personalizationPath -Force | Out-Null
    }
    Set-ItemProperty -Path $personalizationPath -Name 'NoLockScreen' -Value 1 -Type DWord -Force

    $powerCfgCommands = @(
        'powercfg /change monitor-timeout-ac 0',
        'powercfg /change monitor-timeout-dc 0',
        'powercfg /change standby-timeout-ac 0',
        'powercfg /change standby-timeout-dc 0',
        'powercfg /change hibernate-timeout-ac 0',
        'powercfg /change hibernate-timeout-dc 0'
    )

    foreach ($command in $powerCfgCommands) {
        try {
            Invoke-Expression $command | Out-Null
        }
        catch {
            Write-Warning "powercfg fehlgeschlagen ($command): $_"
        }
    }

    try {
        powercfg -setacvalueindex SCHEME_CURRENT SUB_VIDEO VIDEOIDLE 0 | Out-Null
        powercfg -setdcvalueindex SCHEME_CURRENT SUB_VIDEO VIDEOIDLE 0 | Out-Null
        powercfg -setacvalueindex SCHEME_CURRENT SUB_SLEEP STANDBYIDLE 0 | Out-Null
        powercfg -setdcvalueindex SCHEME_CURRENT SUB_SLEEP STANDBYIDLE 0 | Out-Null
        powercfg -setacvalueindex SCHEME_CURRENT SUB_SLEEP HIBERNATEIDLE 0 | Out-Null
        powercfg -setdcvalueindex SCHEME_CURRENT SUB_SLEEP HIBERNATEIDLE 0 | Out-Null
        powercfg -setactive SCHEME_CURRENT | Out-Null
    }
    catch {
        Write-Warning "powercfg-Schema konnte nicht aktualisiert werden: $_"
    }
}

function Set-EmmaKioskDisplay {
    param(
        [string]$ScriptDir = 'C:\Emma\current\provisioning',
        [int]$DisplayId = 0,
        [int]$Rotation = 270
    )

    Disable-EmmaTouchHold
    Disable-EmmaEdgeGestures
    Set-EmmaDisplayRotation -ScriptDir $ScriptDir -DisplayId $DisplayId -Rotation $Rotation
    Disable-EmmaScreenSaverAndSleep
}
