#Requires -Version 5.1
<#
.SYNOPSIS
    ImajViewer Windows kaldirma scripti

.DESCRIPTION
    ImajViewer'i Windows sistemden tamamen kaldirir:
      - Kurulum dizinini siler
      - Registry'den uninstall girdisini siler
      - Start Menu kisayolu silinir
      - Dosya association'lar temizlenir

.PARAMETER InstallDir
    Kurulum dizini. Belirtilmezse registry'den okunur.

.PARAMETER SkipConfirm
    Onay sorusunu atlar (CI/otomasyon icin)

.EXAMPLE
    .\uninstall.ps1
    .\uninstall.ps1 --install-dir "C:\Program Files\ImajViewer"
    .\uninstall.ps1 -SkipConfirm
#>

param(
    [string]$InstallDir = "",
    [switch]$SkipConfirm
)

# Renkli cikti
function Write-Step { param([string]$Msg) Write-Host "`n> $Msg" -ForegroundColor Cyan }
function Write-Ok   { param([string]$Msg) Write-Host "  [OK] $Msg" -ForegroundColor Green }
function Write-Err  { param([string]$Msg) Write-Host "  [X]  $Msg" -ForegroundColor Red; exit 1 }
function Write-Warn { param([string]$Msg) Write-Host "  [!]  $Msg" -ForegroundColor Yellow }

$ErrorActionPreference = 'Stop'
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$AppName = "ImajViewer"
$AppExe = "imajviewer.exe"
$UninstallGuid = "B8F4A3D2-1C5E-4A7B-9D0F-6E2C8A1B3D5F"

# ── Kurulum dizinini bul ─────────────────────────────────
if (-not $InstallDir) {
    # Registry'den oku
    $regPaths = @(
        "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$UninstallGuid",
        "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$UninstallGuid"
    )
    foreach ($rp in $regPaths) {
        if (Test-Path $rp) {
            $InstallDir = (Get-ItemProperty -Path $rp).InstallLocation
            break
        }
    }
}

if (-not $InstallDir -or -not (Test-Path $InstallDir)) {
    # Varsayilan dizinleri kontrol et
    $defaultPaths = @(
        Join-Path ${env:ProgramFiles(x86)} $AppName,
        Join-Path $env:ProgramFiles $AppName,
        Join-Path $env:LOCALAPPDATA $AppName
    )
    foreach ($dp in $defaultPaths) {
        if (Test-Path (Join-Path $dp $AppExe)) {
            $InstallDir = $dp
            break
        }
    }
}

if (-not $InstallDir) {
    Write-Err "$AppName kurulumu bulunamadi. Manuel olarak kaldirmak icin Ayarlar > Uygulamalar kullanin."
}

Write-Host "=" * 60 -ForegroundColor White
Write-Host "  ImajViewer Kaldirma" -ForegroundColor White
Write-Host "=" * 60 -ForegroundColor White
Write-Host "  Install Dir  : $InstallDir" -ForegroundColor Gray
Write-Host ""

# ── Onay ─────────────────────────────────────────────────
if (-not $SkipConfirm) {
    $answer = Read-Host "  Tum $AppName dosyalari ve ayarlari silinecek. Devam etmek istiyor musunuz? (E/H)"
    if ($answer -ne 'E' -and $answer -ne 'e') {
        Write-Host "  Kaldirma iptal edildi." -ForegroundColor Yellow
        exit 0
    }
}

# ── Surucu olan process'leri kapat ───────────────────────
Write-Step "$AppExe process'leri kontrol ediliyor..."
$processes = Get-Process -Name (Split-Path $AppExe -Leaf) -ErrorAction SilentlyContinue
if ($processes) {
    foreach ($p in $processes) {
        Write-Host "  Kapaniyor: $($p.Id) ($($p.Path))" -ForegroundColor Yellow
        $p | Stop-Process -Force -ErrorAction SilentlyContinue
    }
    Start-Sleep -Seconds 1
}
Write-Ok "Process kontrolu tamam"

# ── Kurulum dizinini sil ─────────────────────────────────
Write-Step "Kurulum dizini siliniyor..."
if (Test-Path $InstallDir) {
    Remove-Item -Path $InstallDir -Recurse -Force
    Write-Ok "$InstallDir silindi"
}

# ── Registry: Uninstall girdisi ─────────────────────────
Write-Step "Registry girdileri temizleniyor..."

$regPaths = @(
    "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$UninstallGuid",
    "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$UninstallGuid"
)
foreach ($rp in $regPaths) {
    if (Test-Path $rp) {
        Remove-Item -Path $rp -Recurse -Force
        Write-Ok "Registry silindi: $rp"
    }
}

# Applications registry girdisi
$appRegKey = "HKCU:\Software\Classes\Applications\$AppExe"
if (Test-Path $appRegKey) {
    Remove-Item -Path $appRegKey -Recurse -Force
    Write-Ok "Applications registry girdisi silindi"
}

# ── Kisayollari sil ─────────────────────────────────────
Write-Step "Kisayollar siliniyor..."

$startMenuPaths = @(
    Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs",
    [Environment]::GetFolderPath("CommonPrograms")
)
foreach ($sm in $startMenuPaths) {
    $lnk = Join-Path $sm "$AppName.lnk"
    if (Test-Path $lnk) {
        Remove-Item $lnk -Force
        Write-Ok "Start Menu kisayolu silindi: $lnk"
    }
}

$desktop = [Environment]::GetFolderPath("Desktop")
$desktopLnk = Join-Path $desktop "$AppName.lnk"
if (Test-Path $desktopLnk) {
    Remove-Item $desktopLnk -Force
    Write-Ok "Masaustu kisayolu silindi: $desktopLnk"
}

# ── Tamamlandı ──────────────────────────────────────────
Write-Host ""
Write-Host ("=" * 60) -ForegroundColor Green
Write-Host "  $AppName basariyla kaldirildi!" -ForegroundColor Green
Write-Host ("=" * 60) -ForegroundColor Green
Write-Host ""
