# Milonga DJ Pro — デスクトップショートカット作成
$ErrorActionPreference = 'Stop'
try {
    $p = Get-AppxPackage *MilongaDJPro* | Select-Object -First 1
    if (-not $p) {
        Write-Host ''
        Write-Host '  [エラー] Milonga DJ Pro がインストールされていません。' -ForegroundColor Red
        Write-Host '          先に MilongaDJPro_*.msix をインストールしてください。' -ForegroundColor Red
        Write-Host ''
        Read-Host '  Enter キーで閉じます'
        exit 1
    }
    $appid = (Get-AppxPackageManifest $p).Package.Applications.Application.Id
    $pfn   = $p.PackageFamilyName

    # アプリアイコンから .ico を生成（任意・失敗してもショートカットは作る）
    $ico = Join-Path $env:LOCALAPPDATA 'MilongaDJPro\app.ico'
    try {
        New-Item -ItemType Directory -Force -Path (Split-Path $ico) | Out-Null
        $srcPng = Join-Path $p.InstallLocation 'Assets\Square310x310Logo.png'
        if (Test-Path $srcPng) {
            Add-Type -AssemblyName System.Drawing
            $img = [System.Drawing.Image]::FromFile($srcPng)
            $bmp = New-Object System.Drawing.Bitmap 256, 256
            $g = [System.Drawing.Graphics]::FromImage($bmp)
            $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
            $g.DrawImage($img, 0, 0, 256, 256); $g.Dispose(); $img.Dispose()
            $ms = New-Object System.IO.MemoryStream
            $bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png); $bmp.Dispose()
            $png = $ms.ToArray(); $ms.Dispose()
            $fs = [System.IO.File]::Create($ico); $bw = New-Object System.IO.BinaryWriter($fs)
            $bw.Write([uint16]0); $bw.Write([uint16]1); $bw.Write([uint16]1)
            $bw.Write([byte]0); $bw.Write([byte]0); $bw.Write([byte]0); $bw.Write([byte]0)
            $bw.Write([uint16]1); $bw.Write([uint16]32)
            $bw.Write([uint32]$png.Length); $bw.Write([uint32]22)
            $bw.Write($png); $bw.Flush(); $bw.Close(); $fs.Close()
        }
    } catch { $ico = $null }

    $lnk = Join-Path ([Environment]::GetFolderPath('Desktop')) 'Milonga DJ Pro.lnk'
    $ws = New-Object -ComObject WScript.Shell
    $sc = $ws.CreateShortcut($lnk)
    $sc.TargetPath = "$env:WINDIR\explorer.exe"
    $sc.Arguments  = "shell:AppsFolder\$pfn!$appid"
    if ($ico -and (Test-Path $ico)) { $sc.IconLocation = "$ico,0" }
    $sc.Description = 'Milonga DJ Pro'
    $sc.Save()

    Write-Host ''
    Write-Host '  [完了] デスクトップに「Milonga DJ Pro」を作成しました。' -ForegroundColor Green
    Write-Host ''
} catch {
    Write-Host ''
    Write-Host ('  [エラー] ' + $_.Exception.Message) -ForegroundColor Red
    Write-Host ''
}
Read-Host '  Enter キーで閉じます'
