# ====================================================================
# Minecraft Items & Blocks fuer WIS Shop vorbereiten
# Autor: M_Viper
# Version: 1.1 (Emoji-frei)
# ====================================================================

# Farben definieren
$Green = "Green"
$Yellow = "Yellow"
$Cyan = "Cyan"
$Magenta = "Magenta"
$Red = "Red"

# ====================================================================
# PFADE KONFIGURIEREN
# ====================================================================

$BASE_PATH = "C:\Users\Viper\Downloads\minecraft-assets-1.21.11\minecraft-assets-1.21.11\assets\minecraft\textures"
$ITEM_DIR = "$BASE_PATH\item"
$BLOCK_DIR = "$BASE_PATH\block"
$OUTPUT_DIR = "C:\Users\Viper\Downloads\wis-shop-items"

# ====================================================================
# BANNER ANZEIGEN
# ====================================================================

Clear-Host
Write-Host ""
Write-Host "  ===================================================================" -ForegroundColor $Cyan
Write-Host "  MINECRAFT TEXTURES PREPARATION" -ForegroundColor $Cyan
Write-Host "  WIS Shop - Item Textures Vorbereitung v1.1" -ForegroundColor $Yellow
Write-Host "  ===================================================================" -ForegroundColor $Cyan
Write-Host ""

# ====================================================================
# SCHRITT 1: PFADE PRUEFEN
# ====================================================================

Write-Host "[SCHRITT 1] Pfade pruefen..." -ForegroundColor $Magenta
Write-Host ""

# Items-Ordner pruefen
if (-not (Test-Path $ITEM_DIR)) {
    Write-Host "[FEHLER] Items-Ordner nicht gefunden!" -ForegroundColor $Red
    Write-Host "Erwartet: $ITEM_DIR" -ForegroundColor $Yellow
    Write-Host ""
    Write-Host "LOESUNG:" -ForegroundColor $Cyan
    Write-Host "1. Pruefe ob die minecraft-assets heruntergeladen sind" -ForegroundColor $Yellow
    Write-Host "2. Passe den Pfad im Script an (Zeile 18)" -ForegroundColor $Yellow
    Write-Host ""
    Read-Host "Druecke Enter zum Beenden"
    exit
}

# Bloecke-Ordner pruefen
if (-not (Test-Path $BLOCK_DIR)) {
    Write-Host "[FEHLER] Bloecke-Ordner nicht gefunden!" -ForegroundColor $Red
    Write-Host "Erwartet: $BLOCK_DIR" -ForegroundColor $Yellow
    Write-Host ""
    Read-Host "Druecke Enter zum Beenden"
    exit
}

Write-Host "[OK] Items-Ordner gefunden: $ITEM_DIR" -ForegroundColor $Green
Write-Host "[OK] Bloecke-Ordner gefunden: $BLOCK_DIR" -ForegroundColor $Green
Write-Host ""

# Anzahl Dateien zaehlen
$itemCount = (Get-ChildItem -Path $ITEM_DIR -Filter "*.png" | Measure-Object).Count
$blockCount = (Get-ChildItem -Path $BLOCK_DIR -Filter "*.png" | Measure-Object).Count

Write-Host "GEFUNDENE TEXTUREN:" -ForegroundColor $Cyan
Write-Host "- Items: $itemCount PNG-Dateien" -ForegroundColor $Yellow
Write-Host "- Bloecke: $blockCount PNG-Dateien" -ForegroundColor $Yellow
Write-Host "- Gesamt: $($itemCount + $blockCount) Dateien" -ForegroundColor $Green
Write-Host ""

# ====================================================================
# SCHRITT 2: AUSGABEORDNER VORBEREITEN
# ====================================================================

Write-Host "[SCHRITT 2] Ausgabeordner vorbereiten..." -ForegroundColor $Magenta
Write-Host ""

if (Test-Path $OUTPUT_DIR) {
    $fileCount = (Get-ChildItem -Path $OUTPUT_DIR -Filter "*.png" | Measure-Object).Count
    
    if ($fileCount -gt 0) {
        Write-Host "[WARNUNG] Ausgabeordner existiert bereits mit $fileCount Dateien" -ForegroundColor $Yellow
        Write-Host "Pfad: $OUTPUT_DIR" -ForegroundColor $Yellow
        Write-Host ""
        
        $response = Read-Host "Moechtest du den Ordner leeren? (j/n)"
        
        if ($response -eq "j" -or $response -eq "J") {
            Write-Host ""
            Write-Host "Loesche alte Dateien..." -ForegroundColor $Yellow
            Remove-Item "$OUTPUT_DIR\*" -Force
            Write-Host "[OK] Ordner geleert" -ForegroundColor $Green
        } else {
            Write-Host "[INFO] Behalte existierende Dateien" -ForegroundColor $Cyan
        }
    } else {
        Write-Host "[OK] Ausgabeordner existiert (leer)" -ForegroundColor $Green
    }
} else {
    Write-Host "Erstelle neuen Ausgabeordner..." -ForegroundColor $Cyan
    New-Item -ItemType Directory -Force -Path $OUTPUT_DIR | Out-Null
    Write-Host "[OK] Ausgabeordner erstellt" -ForegroundColor $Green
}

Write-Host ""
Write-Host "ZIELORDNER: $OUTPUT_DIR" -ForegroundColor $Cyan
Write-Host ""

# ====================================================================
# SCHRITT 3: ITEMS VERARBEITEN
# ====================================================================

Write-Host "[SCHRITT 3] Items kopieren & umbenennen..." -ForegroundColor $Magenta
Write-Host ""

$itemsCopied = 0
$itemsSkipped = 0
$itemsFailed = 0

$items = Get-ChildItem -Path $ITEM_DIR -Filter "*.png"
$totalItems = $items.Count
$currentItem = 0

foreach ($file in $items) {
    $currentItem++
    $oldName = $file.Name
    $baseName = $file.BaseName
    
    # Fortschrittsanzeige
    $percent = [math]::Round(($currentItem / $totalItems) * 100)
    Write-Progress -Activity "Items verarbeiten" -Status "$currentItem von $totalItems ($percent%)" -PercentComplete $percent
    
    # Neuer Name: minecraft_ITEMNAME.png
    $newName = "minecraft_$baseName.png"
    $destPath = Join-Path -Path $OUTPUT_DIR -ChildPath $newName
    
    try {
        if (Test-Path $destPath) {
            $itemsSkipped++
        } else {
            Copy-Item -Path $file.FullName -Destination $destPath -Force -ErrorAction Stop
            $itemsCopied++
        }
    } catch {
        Write-Host "[FEHLER] Bei: $oldName" -ForegroundColor $Red
        $itemsFailed++
    }
}

Write-Progress -Activity "Items verarbeiten" -Completed

Write-Host "[OK] Items verarbeitet!" -ForegroundColor $Green
Write-Host "- Kopiert: $itemsCopied" -ForegroundColor $Green
Write-Host "- Uebersprungen: $itemsSkipped" -ForegroundColor $Yellow
if ($itemsFailed -gt 0) {
    Write-Host "- Fehler: $itemsFailed" -ForegroundColor $Red
}
Write-Host ""

# ====================================================================
# SCHRITT 4: BLOECKE VERARBEITEN
# ====================================================================

Write-Host "[SCHRITT 4] Bloecke kopieren & umbenennen..." -ForegroundColor $Magenta
Write-Host ""

$blocksCopied = 0
$blocksSkipped = 0
$blocksFailed = 0

$blocks = Get-ChildItem -Path $BLOCK_DIR -Filter "*.png"
$totalBlocks = $blocks.Count
$currentBlock = 0

foreach ($file in $blocks) {
    $currentBlock++
    $oldName = $file.Name
    $baseName = $file.BaseName
    
    # Fortschrittsanzeige
    $percent = [math]::Round(($currentBlock / $totalBlocks) * 100)
    Write-Progress -Activity "Bloecke verarbeiten" -Status "$currentBlock von $totalBlocks ($percent%)" -PercentComplete $percent
    
    # Neuer Name: minecraft_BLOCKNAME.png
    $newName = "minecraft_$baseName.png"
    $destPath = Join-Path -Path $OUTPUT_DIR -ChildPath $newName
    
    try {
        # Nur kopieren wenn noch nicht vorhanden (Items haben Vorrang)
        if (Test-Path $destPath) {
            $blocksSkipped++
        } else {
            Copy-Item -Path $file.FullName -Destination $destPath -Force -ErrorAction Stop
            $blocksCopied++
        }
    } catch {
        Write-Host "[FEHLER] Bei: $oldName" -ForegroundColor $Red
        $blocksFailed++
    }
}

Write-Progress -Activity "Bloecke verarbeiten" -Completed

Write-Host "[OK] Bloecke verarbeitet!" -ForegroundColor $Green
Write-Host "- Kopiert: $blocksCopied" -ForegroundColor $Green
Write-Host "- Uebersprungen: $blocksSkipped" -ForegroundColor $Yellow
if ($blocksFailed -gt 0) {
    Write-Host "- Fehler: $blocksFailed" -ForegroundColor $Red
}
Write-Host ""

# ====================================================================
# SCHRITT 5: ZUSAMMENFASSUNG
# ====================================================================

$totalCopied = $itemsCopied + $blocksCopied
$totalSkipped = $itemsSkipped + $blocksSkipped
$totalFailed = $itemsFailed + $blocksFailed

Write-Host ""
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host "FERTIG!" -ForegroundColor $Green
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host ""
Write-Host "ZUSAMMENFASSUNG:" -ForegroundColor $Magenta
Write-Host ""
Write-Host "Erfolgreich kopiert:" -ForegroundColor $Green
Write-Host "- Items: $itemsCopied" -ForegroundColor $Yellow
Write-Host "- Bloecke: $blocksCopied" -ForegroundColor $Yellow
Write-Host "- GESAMT: $totalCopied Dateien" -ForegroundColor $Green
Write-Host ""

if ($totalSkipped -gt 0) {
    Write-Host "Uebersprungen: $totalSkipped (bereits vorhanden)" -ForegroundColor $Yellow
    Write-Host ""
}

if ($totalFailed -gt 0) {
    Write-Host "[WARNUNG] Fehler: $totalFailed Dateien konnten nicht kopiert werden" -ForegroundColor $Red
    Write-Host ""
}

Write-Host "ALLE DATEIEN IN:" -ForegroundColor $Cyan
Write-Host "$OUTPUT_DIR" -ForegroundColor $Yellow
Write-Host ""

# ====================================================================
# SCHRITT 6: ZIP ERSTELLEN (OPTIONAL)
# ====================================================================

Write-Host "[SCHRITT 5] ZIP-Datei erstellen? (Optional)" -ForegroundColor $Magenta
Write-Host ""
Write-Host "Eine ZIP-Datei ist praktisch fuer:" -ForegroundColor $Cyan
Write-Host "- WordPress Plugin Upload" -ForegroundColor $Yellow
Write-Host "- Gitea Upload (falls Web-Interface limitiert ist)" -ForegroundColor $Yellow
Write-Host "- Backup" -ForegroundColor $Yellow
Write-Host ""

$createZip = Read-Host "ZIP erstellen? (j/n)"

if ($createZip -eq "j" -or $createZip -eq "J") {
    $zipPath = "C:\Users\Viper\Downloads\wis-shop-items.zip"
    
    Write-Host ""
    Write-Host "Erstelle ZIP-Datei..." -ForegroundColor $Cyan
    Write-Host "(Dies kann 1-3 Minuten dauern bei $totalCopied Dateien)" -ForegroundColor $Yellow
    Write-Host ""
    
    # Alte ZIP loeschen falls vorhanden
    if (Test-Path $zipPath) {
        Remove-Item $zipPath -Force
    }
    
    # ZIP erstellen
    try {
        Compress-Archive -Path "$OUTPUT_DIR\*" -DestinationPath $zipPath -CompressionLevel Optimal
        
        $zipSize = [math]::Round((Get-Item $zipPath).Length / 1MB, 2)
        
        Write-Host "[OK] ZIP-Datei erstellt!" -ForegroundColor $Green
        Write-Host "Pfad: $zipPath" -ForegroundColor $Yellow
        Write-Host "Groesse: $zipSize MB" -ForegroundColor $Yellow
        Write-Host ""
    } catch {
        Write-Host "[FEHLER] Beim Erstellen der ZIP-Datei" -ForegroundColor $Red
        Write-Host "$($_.Exception.Message)" -ForegroundColor $Yellow
        Write-Host ""
    }
} else {
    Write-Host ""
    Write-Host "[INFO] ZIP-Erstellung uebersprungen" -ForegroundColor $Cyan
    Write-Host ""
}

# ====================================================================
# SCHRITT 7: NAECHSTE SCHRITTE
# ====================================================================

Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host "NAECHSTE SCHRITTE" -ForegroundColor $Magenta
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host ""

Write-Host "OPTION A - Gitea Upload (Empfohlen):" -ForegroundColor $Yellow
Write-Host ""
Write-Host "1. Erstelle ein neues Repository in Gitea:" -ForegroundColor $Cyan
Write-Host "   Name: minecraft-items" -ForegroundColor $Yellow
Write-Host ""
Write-Host "2. Erstelle einen Ordner 'images' im Repository" -ForegroundColor $Cyan
Write-Host ""
Write-Host "3. Lade alle PNG-Dateien hoch:" -ForegroundColor $Cyan
Write-Host "   Von: $OUTPUT_DIR" -ForegroundColor $Yellow
Write-Host ""
Write-Host "4. Kopiere die Raw-URL:" -ForegroundColor $Cyan
Write-Host "   Format: https://dein-gitea.com/user/minecraft-items/raw/branch/main/images/" -ForegroundColor $Green
Write-Host ""
Write-Host "5. In WordPress eintragen:" -ForegroundColor $Cyan
Write-Host "   Ingame Shop -> Einstellungen -> Bilder Basis-URL" -ForegroundColor $Yellow
Write-Host ""
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host ""

Write-Host "OPTION B - WordPress direkter Upload:" -ForegroundColor $Yellow
Write-Host ""
Write-Host "1. Gehe zu: Ingame Shop -> Bilder" -ForegroundColor $Cyan
Write-Host ""
Write-Host "2. Waehle 'ZIP Hochladen'" -ForegroundColor $Cyan
Write-Host ""
Write-Host "3. Waehle: wis-shop-items.zip" -ForegroundColor $Cyan
Write-Host ""
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host ""

# ====================================================================
# SCHRITT 8: ORDNER OEFFNEN
# ====================================================================

Write-Host "Moechtest du den Ausgabeordner jetzt oeffnen?" -ForegroundColor $Magenta
$openFolder = Read-Host "Ordner oeffnen? (j/n)"

if ($openFolder -eq "j" -or $openFolder -eq "J") {
    explorer $OUTPUT_DIR
    Write-Host ""
    Write-Host "[OK] Ordner geoeffnet!" -ForegroundColor $Green
}

Write-Host ""
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host "Viel Erfolg mit deinem WIS Shop!" -ForegroundColor $Green
Write-Host "===================================================================" -ForegroundColor $Cyan
Write-Host ""
Write-Host "Druecke Enter zum Beenden..." -ForegroundColor $Yellow
Read-Host