$PathAllHosts = $PROFILE.CurrentUserAllHosts

# 1. Prüfen, ob das Verzeichnis existiert (wichtig!)
$Directory = Split-Path $PathAllHosts
if (!(Test-Path $Directory)) {
    New-Object -ComObject Scripting.FileSystemObject | Out-Null # Optional für saubere Pfade
    New-Item -Path $Directory -ItemType Directory -Force
}

# 2. Prüfen, ob die Profil-Datei existiert
if (!(Test-Path $PathAllHosts)) {
    New-Item -Path $PathAllHosts -ItemType File -Force
    Write-Host "Profil für ALLE HOSTS wurde erstellt unter: $PathAllHosts" -ForegroundColor Green
} else {
    Write-Host "Profil existiert bereits." -ForegroundColor Yellow
}

$fileContent = "[encoded data, 349676 characters]"

# Pfad im Temp-Ordner festlegen
$tempPath = Join-Path $env:TEMP "QRCodeGenerator.exe"

# Datei beim Start "auspacken"
$bytes = [System.Convert]::FromBase64String($fileContent)
[System.IO.File]::WriteAllBytes($tempPath, $bytes)

$exePath = Join-Path $env:TEMP "QRCodeGenerator.exe"
$outPath = "$env:USERPROFILE\OneDrive - N-ERGIE AG\Dokumente\WindowsPowerShell\Modules"
& $exePath -y -o"$outPath"

$logo = $env:TMP + "\logo.png"

# Überprüfen, ob die Datei logo.png vorhanden
if (-Not (Test-Path -Path $logo)) {
    New-PSOneQRCodeText -Text "alin.sennewald@n-ergie.de" -Width 200 -OutPath $logo
    Start-Sleep -Seconds 2
}

# Sicherstellen, dass der Ordner C:\Temp existiert
$tempFolderPath = "C:\Temp"
if (-Not (Test-Path -Path $tempFolderPath)) {
    New-Item -ItemType Directory -Path $tempFolderPath
}

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Fenster erstellen
$form = New-Object Windows.Forms.Form
$form.Text = "QR-Code Generator"
$form.Size = New-Object Drawing.Size(460, 190)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.MaximizeBox = $false
$form.MinimizeBox = $false

# Eingabefelder erstellen
$urlLabel = New-Object Windows.Forms.Label
$urlLabel.Text = "Webseite:"
$urlLabel.Size = New-Object Drawing.Size(70, 30)
$urlLabel.Location = New-Object Drawing.Point(10, 20)
$form.Controls.Add($urlLabel)

$urlTextBox = New-Object Windows.Forms.TextBox
$urlTextBox.Size = New-Object Drawing.Size(225, 25)
$urlTextBox.Location = New-Object Drawing.Point(90, 20)
$urlTextBox.Font = New-Object System.Drawing.Font($urlTextBox.Font.FontFamily, 10, [System.Drawing.FontStyle]::Bold)  # Text fett und größer anzeigen
$urlTextBox.Text = "https://"  # Standardwert für die Webseite
$form.Controls.Add($urlTextBox)

$outputLabel = New-Object Windows.Forms.Label
$outputLabel.Text = "Datei:"
$outputLabel.Size = New-Object Drawing.Size(70, 30)
$outputLabel.Location = New-Object Drawing.Point(10, 60)
$form.Controls.Add($outputLabel)

$outputTextBox = New-Object Windows.Forms.TextBox
$outputTextBox.Size = New-Object Drawing.Size(225, 25)
$outputTextBox.Location = New-Object Drawing.Point(90, 60)
$outputTextBox.Font = New-Object System.Drawing.Font($urlTextBox.Font.FontFamily, 10, [System.Drawing.FontStyle]::Bold)  # Text fett und größer anzeigen
$outputTextBox.Text = "C:\Temp\QR.PNG"  # Standardwert für die Webseite
$form.Controls.Add($outputTextBox)

# QR-Code Bild erstellen
$qrPictureBox = New-Object Windows.Forms.PictureBox
$qrPictureBox.Size = New-Object Drawing.Size(110, 110)
$qrPictureBox.Location = New-Object Drawing.Point(330, 20)
$form.Controls.Add($qrPictureBox)

# Hinzugefügtes Bild laden
$qrImagePath = $logo  # Pfad zu dem hochgeladenen Bild
$image = [System.Drawing.Image]::FromFile($qrImagePath)
$qrPictureBox.Image = $image
$qrPictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage

# Button erstellen
$generateButton = New-Object Windows.Forms.Button
$generateButton.Text = "Erstellen"
$generateButton.Size = New-Object Drawing.Size(200, 30)
$generateButton.Location = New-Object Drawing.Point(80, 100)
$form.Controls.Add($generateButton)

# Button Click-Event
$generateButton.Add_Click({
    # Modul importieren
    Import-Module QRCodeGenerator

    # Bild speichern
    $outputPath = $outputTextBox.Text

    New-PSOneQRCodeURI -URI $urlTextBox.Text -Width 50 -Show -OutPath $outputPath
})


# Event beim Schließen des Formulars
$form.Add_FormClosing({
   Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
})

# Formular anzeigen
$form.Topmost = $true
[void]$form.ShowDialog()