# Fix OneDrive Thumbnail Override - Restore Local Thumbnails
# This script disables OneDrive's cloud thumbnail providers that override custom thumbnails

Write-Host "🎯 Fixing OneDrive Thumbnail Override Issue..." -ForegroundColor Cyan
Write-Host "This will restore priority to your locally-generated thumbnails." -ForegroundColor Yellow

# Check if running as administrator
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "❌ This script requires administrator privileges. Please run as administrator." -ForegroundColor Red
    exit 1
}

# 1. Disable OneDrive Files On-Demand thumbnail override
Write-Host "🔧 Disabling OneDrive Files On-Demand thumbnail override..." -ForegroundColor Green

try {
    # User-level OneDrive policies
    if (!(Test-Path "HKCU:\SOFTWARE\Policies\Microsoft\OneDrive")) {
        New-Item -Path "HKCU:\SOFTWARE\Policies\Microsoft\OneDrive" -Force | Out-Null
    }
    Set-ItemProperty -Path "HKCU:\SOFTWARE\Policies\Microsoft\OneDrive" -Name "DisableCloudThumbnails" -Value 1 -Type DWord

    # Machine-level OneDrive policies
    if (!(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\OneDrive")) {
        New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\OneDrive" -Force | Out-Null
    }
    Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\OneDrive" -Name "DisableCloudThumbnails" -Value 1 -Type DWord

    Write-Host "✅ OneDrive thumbnail override disabled" -ForegroundColor Green
} catch {
    Write-Host "⚠️ Warning: Could not set OneDrive policies: $($_.Exception.Message)" -ForegroundColor Yellow
}

# 2. Disable cloud files thumbnail provider
Write-Host "🔧 Disabling cloud files thumbnail provider..." -ForegroundColor Green

try {
    # This is the key provider that overrides local thumbnails
    $cloudProviderPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ThumbnailProviders\{E357FCCD-A995-4576-B01F-234630154E96}"
    if (Test-Path $cloudProviderPath) {
        Remove-Item -Path $cloudProviderPath -Force -Recurse
        Write-Host "✅ Cloud thumbnail provider removed" -ForegroundColor Green
    } else {
        Write-Host "ℹ️ Cloud thumbnail provider not found (may already be disabled)" -ForegroundColor Blue
    }
} catch {
    Write-Host "⚠️ Warning: Could not remove cloud thumbnail provider: $($_.Exception.Message)" -ForegroundColor Yellow
}

# 3. Set local thumbnail preferences
Write-Host "🔧 Configuring local thumbnail preferences..." -ForegroundColor Green

try {
    # Ensure local thumbnails are enabled
    if (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced")) {
        New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Force | Out-Null
    }
    
    Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "IconsOnly" -Value 0 -Type DWord
    Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "DisableThumbnailsOnNetworkFolders" -Value 0 -Type DWord
    
    # Add custom registry value to prefer local thumbnails
    Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "UseLocalThumbnailProvider" -Value 1 -Type DWord

    Write-Host "✅ Local thumbnail preferences configured" -ForegroundColor Green
} catch {
    Write-Host "⚠️ Warning: Could not set local thumbnail preferences: $($_.Exception.Message)" -ForegroundColor Yellow
}

# 4. Disable OneDrive shell icon overlays (these can interfere with thumbnails)
Write-Host "🔧 Disabling OneDrive shell overlays..." -ForegroundColor Green

$overlayPaths = @(
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive1",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive2",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive3",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive4",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive5",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive6",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\OneDrive7"
)

$overlaysRemoved = 0
foreach ($overlayPath in $overlayPaths) {
    try {
        if (Test-Path $overlayPath) {
            Remove-Item -Path $overlayPath -Force -Recurse
            $overlaysRemoved++
        }
    } catch {
        Write-Host "⚠️ Warning: Could not remove overlay $overlayPath" -ForegroundColor Yellow
    }
}

Write-Host "✅ Removed $overlaysRemoved OneDrive shell overlays" -ForegroundColor Green

# 5. Clear thumbnail cache to force regeneration
Write-Host "🔧 Clearing thumbnail cache to force regeneration..." -ForegroundColor Green

try {
    # Stop Explorer process
    Stop-Process -Name "explorer" -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2

    # Clear thumbnail cache
    $thumbCachePath = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer"
    $thumbCacheFiles = Get-ChildItem -Path $thumbCachePath -Filter "thumbcache_*.db" -ErrorAction SilentlyContinue
    
    foreach ($file in $thumbCacheFiles) {
        try {
            Remove-Item -Path $file.FullName -Force
        } catch {
            Write-Host "⚠️ Could not remove $($file.Name)" -ForegroundColor Yellow
        }
    }

    # Restart Explorer
    Start-Process -FilePath "explorer.exe"
    Start-Sleep -Seconds 3

    Write-Host "✅ Thumbnail cache cleared and Explorer restarted" -ForegroundColor Green
} catch {
    Write-Host "⚠️ Warning: Issues clearing thumbnail cache: $($_.Exception.Message)" -ForegroundColor Yellow
    Write-Host "🔄 Please restart Windows Explorer manually" -ForegroundColor Blue
}

# 6. Prevent automatic thumbnail cache deletion
Write-Host "🔧 Preventing Windows from auto-deleting thumbnail cache..." -ForegroundColor Green

try {
    # Disable thumbnail cache auto-cleanup in Disk Cleanup
    $autorunKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache"
    if (Test-Path $autorunKey) {
        Set-ItemProperty -Path $autorunKey -Name "Autorun" -Value 0 -Type DWord
    }

    # Also disable for WOW64 on 64-bit systems
    $autorunKeyWow64 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache"
    if (Test-Path $autorunKeyWow64) {
        Set-ItemProperty -Path $autorunKeyWow64 -Name "Autorun" -Value 0 -Type DWord
    }

    Write-Host "✅ Thumbnail cache auto-deletion disabled" -ForegroundColor Green
} catch {
    Write-Host "⚠️ Warning: Could not disable thumbnail cache auto-deletion: $($_.Exception.Message)" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "🎉 OneDrive thumbnail override fix complete!" -ForegroundColor Green
Write-Host ""
Write-Host "📋 What was changed:" -ForegroundColor Cyan
Write-Host "   ✓ Disabled OneDrive cloud thumbnail providers" -ForegroundColor White
Write-Host "   ✓ Removed cloud files thumbnail provider" -ForegroundColor White
Write-Host "   ✓ Set local thumbnail preferences" -ForegroundColor White
Write-Host "   ✓ Disabled OneDrive shell overlays" -ForegroundColor White
Write-Host "   ✓ Cleared and regenerated thumbnail cache" -ForegroundColor White
Write-Host "   ✓ Prevented automatic thumbnail cache deletion" -ForegroundColor White
Write-Host ""
Write-Host "🔄 Please restart your computer for all changes to take effect." -ForegroundColor Yellow
Write-Host "📁 Your custom thumbnails should now have priority over OneDrive's generic icons!" -ForegroundColor Green 