# Requires -RunAsAdministrator

function Write-Status($Message, $Type = "Info") {
    switch ($Type) {
        "Info" { $Color = "White" }
        "Success" { $Color = "Green" }
        "Error" { $Color = "Red" }
        "Warning" { $Color = "Yellow" }
    }
    Write-Host ">>> $Message" -ForegroundColor $Color
}

# Check if running as Administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Status "This script must be run as Administrator" "Error"
    Exit 1
}

try {
    # Stop Docker containers
    Write-Status "Stopping Docker containers..." "Info"
    $containers = docker ps -aq
    if ($containers) {
        docker stop $containers
        Write-Status "All containers stopped" "Success"
    } else {
        Write-Status "No running containers found" "Info"
    }

    # Remove Docker containers
    Write-Status "Removing Docker containers..." "Info"
    $containers = docker ps -aq
    if ($containers) {
        docker rm $containers
        Write-Status "All containers removed" "Success"
    } else {
        Write-Status "No containers to remove" "Info"
    }

    # Remove Docker images
    Write-Status "Removing Docker images..." "Info"
    $images = docker images -aq
    if ($images) {
        docker rmi -f $images
        Write-Status "All images removed" "Success"
    } else {
        Write-Status "No images to remove" "Info"
    }

    # Stop Docker service
    Write-Status "Stopping Docker service..." "Info"
    $dockerService = Get-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
    if ($dockerService) {
        Stop-Service "com.docker.service" -Force
        Write-Status "Docker service stopped" "Success"
    } else {
        Write-Status "Docker service not found" "Info"
    }

    # Remove Docker Desktop
    Write-Status "Uninstalling Docker Desktop..." "Info"
    $dockerDesktop = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like "*Docker Desktop*" }
    if ($dockerDesktop) {
        $dockerDesktop.Uninstall()
        Write-Status "Docker Desktop uninstalled" "Success"
    } else {
        Write-Status "Docker Desktop not found in installed programs" "Info"
    }

    # Clean up Docker data directories
    $directories = @(
        "$env:ProgramData\Docker",
        "$env:APPDATA\Docker",
        "$env:LOCALAPPDATA\Docker"
    )

    foreach ($dir in $directories) {
        if (Test-Path $dir) {
            Write-Status "Removing $dir..." "Info"
            Remove-Item -Path $dir -Recurse -Force -ErrorAction SilentlyContinue
            Write-Status "$dir removed" "Success"
        }
    }

    # Remove environment variables
    Write-Status "Cleaning up environment variables..." "Info"
    $dockerEnvVars = @(
        'DOCKER_HOST',
        'DOCKER_TLS_VERIFY',
        'DOCKER_CERT_PATH',
        'DOCKER_CONFIG',
        'DOCKER_CONTEXT'
    )

    foreach ($var in $dockerEnvVars) {
        if (Test-Path env:$var) {
            Remove-Item env:$var -ErrorAction SilentlyContinue
            [System.Environment]::SetEnvironmentVariable($var, $null, 'User')
            [System.Environment]::SetEnvironmentVariable($var, $null, 'Machine')
            Write-Status "Removed environment variable: $var" "Success"
        }
    }

    # Clean up registry entries
    Write-Status "Cleaning up registry entries..." "Info"
    $registryPaths = @(
        'HKCU:\Software\Docker Inc.',
        'HKLM:\Software\Docker Inc.'
    )

    foreach ($path in $registryPaths) {
        if (Test-Path $path) {
            Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
            Write-Status "Removed registry path: $path" "Success"
        }
    }

    Write-Status "Docker uninstallation completed successfully!" "Success"
    Write-Status "Please restart your computer to complete the cleanup." "Warning"

} catch {
    Write-Status "An error occurred during uninstallation: $_" "Error"
    Write-Status "Please try to finish the cleanup manually" "Warning"
    Exit 1
}