# AutoMusic Installer Script
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$srcDir = $PSScriptRoot
$appName = "AutoMusic"
$defaultInstallPath = "$env:LOCALAPPDATA\$appName"

# 1. Main Welcome Form
$form = New-Object Windows.Forms.Form
$form.Text = "AutoMusic 安装向导"
$form.Size = New-Object Drawing.Size(520, 360)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "FixedDialog"
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.BackColor = [Drawing.Color]::FromArgb(245, 246, 248)

# Load custom icon if exists
if (Test-Path "$srcDir\app\app_icon.ico") {
    $form.Icon = [Drawing.Icon]::new("$srcDir\app\app_icon.ico")
}

# Header Label
$header = New-Object Windows.Forms.Label
$header.Text = "欢迎使用 AutoMusic 安装向导"
$header.Font = New-Object Drawing.Font("Microsoft YaHei", 14, [Drawing.FontStyle]::Bold)
$header.ForeColor = [Drawing.Color]::FromArgb(33, 37, 41)
$header.Location = New-Object Drawing.Point(30, 20)
$header.Size = New-Object Drawing.Size(460, 35)
$form.Controls.Add($header)

# Path Selection Label
$lblPath = New-Object Windows.Forms.Label
$lblPath.Text = "请选择安装目标文件夹:"
$lblPath.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$lblPath.Location = New-Object Drawing.Point(30, 70)
$lblPath.Size = New-Object Drawing.Size(400, 20)
$form.Controls.Add($lblPath)

# Path Textbox
$txtPath = New-Object Windows.Forms.TextBox
$txtPath.Text = $defaultInstallPath
$txtPath.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$txtPath.Location = New-Object Drawing.Point(30, 95)
$txtPath.Size = New-Object Drawing.Size(350, 25)
$form.Controls.Add($txtPath)

# Browse Button
$btnBrowse = New-Object Windows.Forms.Button
$btnBrowse.Text = "浏览..."
$btnBrowse.Font = New-Object Drawing.Font("Microsoft YaHei", 9)
$btnBrowse.Location = New-Object Drawing.Point(390, 93)
$btnBrowse.Size = New-Object Drawing.Size(80, 28)
$btnBrowse.BackColor = [Drawing.Color]::FromArgb(224, 224, 224)
$btnBrowse.FlatStyle = [Windows.Forms.FlatStyle]::Flat
$btnBrowse.FlatAppearance.BorderSize = 0
$form.Controls.Add($btnBrowse)

# Browse Button Action
$btnBrowse.Add_Click({
    $dialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $dialog.Description = "选择 AutoMusic 的安装目标文件夹"
    $dialog.SelectedPath = $txtPath.Text
    if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
        $txtPath.Text = $dialog.SelectedPath
    }
})

# Checkboxes
$chkDesktop = New-Object Windows.Forms.CheckBox
$chkDesktop.Text = "创建桌面快捷方式"
$chkDesktop.Checked = $true
$chkDesktop.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$chkDesktop.Location = New-Object Drawing.Point(35, 140)
$chkDesktop.Size = New-Object Drawing.Size(400, 25)
$form.Controls.Add($chkDesktop)

$chkStartMenu = New-Object Windows.Forms.CheckBox
$chkStartMenu.Text = "创建开始菜单快捷方式"
$chkStartMenu.Checked = $true
$chkStartMenu.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$chkStartMenu.Location = New-Object Drawing.Point(35, 175)
$chkStartMenu.Size = New-Object Drawing.Size(400, 25)
$form.Controls.Add($chkStartMenu)

$chkRunNow = New-Object Windows.Forms.CheckBox
$chkRunNow.Text = "安装完成后立即运行 AutoMusic 并打开管理页面"
$chkRunNow.Checked = $true
$chkRunNow.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$chkRunNow.Location = New-Object Drawing.Point(35, 210)
$chkRunNow.Size = New-Object Drawing.Size(430, 25)
$form.Controls.Add($chkRunNow)

# Install Button
$btnInstall = New-Object Windows.Forms.Button
$btnInstall.Text = "开始安装"
$btnInstall.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5, [Drawing.FontStyle]::Bold)
$btnInstall.BackColor = [Drawing.Color]::FromArgb(0, 123, 255)
$btnInstall.ForeColor = [Drawing.Color]::White
$btnInstall.FlatStyle = [Windows.Forms.FlatStyle]::Flat
$btnInstall.FlatAppearance.BorderSize = 0
$btnInstall.Location = New-Object Drawing.Point(280, 270)
$btnInstall.Size = New-Object Drawing.Size(95, 30)
$btnInstall.DialogResult = [Windows.Forms.DialogResult]::OK
$form.Controls.Add($btnInstall)

# Cancel Button
$btnCancel = New-Object Windows.Forms.Button
$btnCancel.Text = "取消"
$btnCancel.Font = New-Object Drawing.Font("Microsoft YaHei", 9.5)
$btnCancel.BackColor = [Drawing.Color]::FromArgb(224, 224, 224)
$btnCancel.FlatStyle = [Windows.Forms.FlatStyle]::Flat
$btnCancel.FlatAppearance.BorderSize = 0
$btnCancel.Location = New-Object Drawing.Point(385, 270)
$btnCancel.Size = New-Object Drawing.Size(80, 30)
$btnCancel.DialogResult = [Windows.Forms.DialogResult]::Cancel
$form.Controls.Add($btnCancel)

$result = $form.ShowDialog()
if ($result -ne [Windows.Forms.DialogResult]::OK) {
    Exit
}

$installPath = $txtPath.Text.Trim()
if ([string]::IsNullOrEmpty($installPath)) {
    [Windows.Forms.MessageBox]::Show("安装路径不能为空！", "错误", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Error)
    Exit
}

$shortcutPath = "$env:USERPROFILE\Desktop\$appName.lnk"
$startMenuPath = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\$appName.lnk"

# 2. Perform Installation
try {
    # Check if target directory exists and remove it safely
    if (Test-Path $installPath) {
        $confirm = [Windows.Forms.MessageBox]::Show("目标文件夹已存在，继续安装将覆盖其中的文件！是否继续？", "确认覆盖", [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Warning)
        if ($confirm -eq [Windows.Forms.DialogResult]::No) {
            Exit
        }
    }
    
    # Create directory if it doesn't exist
    New-Item -ItemType Directory -Path $installPath -Force | Out-Null
    
    # Copy app files
    Copy-Item -Path "$srcDir\app\*" -Destination $installPath -Recurse -Force
    
    # Create Shortcut function
    function Create-Shortcut ($path, $target, $workingDir, $icon) {
        $wshShell = New-Object -ComObject WScript.Shell
        $shortcut = $wshShell.CreateShortcut($path)
        $shortcut.TargetPath = $target
        $shortcut.WorkingDirectory = $workingDir
        $shortcut.IconLocation = $icon
        $shortcut.Save()
    }
    
    $exePath = "$installPath\AutoMusic.exe"
    
    # Create Desktop shortcut
    if ($chkDesktop.Checked) {
        Create-Shortcut $shortcutPath $exePath $installPath "$exePath,0"
    }
    
    # Create Start Menu shortcut
    if ($chkStartMenu.Checked) {
        Create-Shortcut $startMenuPath $exePath $installPath "$exePath,0"
    }
    
    # Post-Install Run
    if ($chkRunNow.Checked) {
        Start-Process -FilePath $exePath -WorkingDirectory $installPath
    }
    
    # Success Dialog
    [Windows.Forms.MessageBox]::Show("AutoMusic 安装成功！", "安装成功", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
} catch {
    [Windows.Forms.MessageBox]::Show("安装失败: $_", "错误", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Error)
}
