param(
    [string]$AppUrl = "http://igsistemas.app.br/sgf/public/"
)

$ErrorActionPreference = "Stop"

function Write-Step {
    param([string]$Message)
    Write-Host "[SGF] $Message"
}

function Convert-PngToIco {
    param(
        [string]$PngPath,
        [string]$IcoPath
    )

    Add-Type -AssemblyName System.Drawing

    $source = [System.Drawing.Image]::FromFile($PngPath)
    try {
        $canvas = New-Object System.Drawing.Bitmap 256, 256
        $graphics = [System.Drawing.Graphics]::FromImage($canvas)
        try {
            $graphics.Clear([System.Drawing.Color]::Transparent)
            $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
            $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
            $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality

            $scale = [Math]::Min(224 / $source.Width, 224 / $source.Height)
            $width = [int]($source.Width * $scale)
            $height = [int]($source.Height * $scale)
            $x = [int]((256 - $width) / 2)
            $y = [int]((256 - $height) / 2)
            $graphics.DrawImage($source, $x, $y, $width, $height)

            $memory = New-Object IO.MemoryStream
            try {
                $canvas.Save($memory, [System.Drawing.Imaging.ImageFormat]::Png)
                $pngBytes = $memory.ToArray()
            } finally {
                $memory.Dispose()
            }
        } finally {
            $graphics.Dispose()
            $canvas.Dispose()
        }
    } finally {
        $source.Dispose()
    }

    $directory = Split-Path -Parent $IcoPath
    New-Item -ItemType Directory -Force -Path $directory | Out-Null

    $writer = New-Object IO.BinaryWriter([IO.File]::Open($IcoPath, [IO.FileMode]::Create))
    try {
        $writer.Write([UInt16]0)
        $writer.Write([UInt16]1)
        $writer.Write([UInt16]1)
        $writer.Write([Byte]0)
        $writer.Write([Byte]0)
        $writer.Write([Byte]0)
        $writer.Write([Byte]0)
        $writer.Write([UInt16]1)
        $writer.Write([UInt16]32)
        $writer.Write([UInt32]$pngBytes.Length)
        $writer.Write([UInt32]22)
        $writer.Write($pngBytes)
    } finally {
        $writer.Dispose()
    }
}

$InstallerDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ShortcutName = "SGF Custodia Florestal.url"
$DesktopDir = [Environment]::GetFolderPath("Desktop")
$ShortcutPath = Join-Path $DesktopDir $ShortcutName
$IconDir = Join-Path $env:LOCALAPPDATA "SGF"
$IconPath = Join-Path $IconDir "sgf.ico"
$LogoPath = Join-Path $InstallerDir "sgf-logo.png"
$SourceIconPath = Join-Path $InstallerDir "sgf.ico"

Write-Step "Instalador: $InstallerDir"
Write-Step "URL do aplicativo: $AppUrl"

Write-Step "Criando icone do sistema..."
if (Test-Path -LiteralPath $SourceIconPath) {
    New-Item -ItemType Directory -Force -Path $IconDir | Out-Null
    Copy-Item -LiteralPath $SourceIconPath -Destination $IconPath -Force
} elseif (Test-Path -LiteralPath $LogoPath) {
    Convert-PngToIco -PngPath $LogoPath -IcoPath $IconPath
} else {
    throw "Nao foi encontrado o icone do instalador: $SourceIconPath"
}

Write-Step "Criando atalho na Area de Trabalho..."
$shortcutContent = @"
[InternetShortcut]
URL=$AppUrl
IconFile=$IconPath
IconIndex=0
"@
Set-Content -LiteralPath $ShortcutPath -Value $shortcutContent -Encoding ASCII

Write-Host ""
Write-Host "Atalho do SGF criado."
Write-Host "Atalho criado em: $ShortcutPath"
Write-Host "URL: $AppUrl"
