# YTD Dashboard
# Requires Windows PowerShell 5.1, included with Windows 10 and Windows 11.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Root = 'C:\YTD'

function Show-MissingFile {
    param([string]$Path)

    [System.Windows.Forms.MessageBox]::Show(
        "The required file was not found:`r`n`r`n$Path",
        'YTD Dashboard',
        [System.Windows.Forms.MessageBoxButtons]::OK,
        [System.Windows.Forms.MessageBoxIcon]::Error
    ) | Out-Null
}

function Start-YtdFile {
    param([string]$RelativePath)

    $Path = Join-Path $Root $RelativePath
    if (-not (Test-Path -LiteralPath $Path)) {
        Show-MissingFile -Path $Path
        return
    }

    Start-Process -FilePath $Path -WorkingDirectory $Root
}

function Open-YtdFolder {
    param([string]$RelativePath)

    $Path = Join-Path $Root $RelativePath
    if (-not (Test-Path -LiteralPath $Path)) {
        New-Item -ItemType Directory -Path $Path -Force | Out-Null
    }

    Start-Process explorer.exe -ArgumentList "`"$Path`""
}

function Open-YtdDocument {
    param([string]$RelativePath)

    $Path = Join-Path $Root $RelativePath
    if (-not (Test-Path -LiteralPath $Path)) {
        Show-MissingFile -Path $Path
        return
    }

    Start-Process -FilePath $Path
}

function Add-DashboardButton {
    param(
        [System.Windows.Forms.Control]$Parent,
        [string]$Text,
        [int]$Left,
        [int]$Top,
        [int]$Width,
        [scriptblock]$OnClick
    )

    $Button = New-Object System.Windows.Forms.Button
    $Button.Text = $Text
    $Button.Left = $Left
    $Button.Top = $Top
    $Button.Width = $Width
    $Button.Height = 46
    $Button.Font = New-Object System.Drawing.Font('Segoe UI', 11)
    $Button.FlatStyle = [System.Windows.Forms.FlatStyle]::System
    $Button.Add_Click($OnClick)
    $Parent.Controls.Add($Button)
}

$Form = New-Object System.Windows.Forms.Form
$Form.Text = 'YTD Dashboard'
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$Form.ClientSize = New-Object System.Drawing.Size(570, 520)
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$Form.MaximizeBox = $false
$Form.BackColor = [System.Drawing.Color]::White
$Form.Font = New-Object System.Drawing.Font('Segoe UI', 10)

$Title = New-Object System.Windows.Forms.Label
$Title.Text = 'YTD Downloader'
$Title.Font = New-Object System.Drawing.Font('Segoe UI', 22, [System.Drawing.FontStyle]::Bold)
$Title.AutoSize = $true
$Title.Left = 166
$Title.Top = 22
$Form.Controls.Add($Title)

$Subtitle = New-Object System.Windows.Forms.Label
$Subtitle.Text = 'Download music and videos'
$Subtitle.Font = New-Object System.Drawing.Font('Segoe UI', 10)
$Subtitle.ForeColor = [System.Drawing.Color]::DimGray
$Subtitle.AutoSize = $true
$Subtitle.Left = 199
$Subtitle.Top = 64
$Form.Controls.Add($Subtitle)

$DownloadsLabel = New-Object System.Windows.Forms.Label
$DownloadsLabel.Text = 'Downloads'
$DownloadsLabel.Font = New-Object System.Drawing.Font('Segoe UI', 12, [System.Drawing.FontStyle]::Bold)
$DownloadsLabel.AutoSize = $true
$DownloadsLabel.Left = 40
$DownloadsLabel.Top = 108
$Form.Controls.Add($DownloadsLabel)

Add-DashboardButton -Parent $Form -Text 'Download Music from URL' -Left 40 -Top 140 -Width 235 -OnClick {
    Start-YtdFile 'Scripts\DownloadSongURL.bat'
}

Add-DashboardButton -Parent $Form -Text 'Download Video from URL' -Left 295 -Top 140 -Width 235 -OnClick {
    Start-YtdFile 'Scripts\DownloadVideoURL.bat'
}

Add-DashboardButton -Parent $Form -Text 'Batch Download Songs' -Left 40 -Top 200 -Width 235 -OnClick {
    Start-YtdFile 'Scripts\DownloadBatchSongs.bat'
}

Add-DashboardButton -Parent $Form -Text 'Edit SongsToGet.txt' -Left 295 -Top 200 -Width 235 -OnClick {
    Open-YtdDocument 'SongsToGet.txt'
}

$FoldersLabel = New-Object System.Windows.Forms.Label
$FoldersLabel.Text = 'Open folders'
$FoldersLabel.Font = New-Object System.Drawing.Font('Segoe UI', 12, [System.Drawing.FontStyle]::Bold)
$FoldersLabel.AutoSize = $true
$FoldersLabel.Left = 40
$FoldersLabel.Top = 275
$Form.Controls.Add($FoldersLabel)

Add-DashboardButton -Parent $Form -Text 'Songs Folder' -Left 40 -Top 307 -Width 150 -OnClick {
    Open-YtdFolder 'Songs'
}

Add-DashboardButton -Parent $Form -Text 'Videos Folder' -Left 210 -Top 307 -Width 150 -OnClick {
    Open-YtdFolder 'Videos'
}

Add-DashboardButton -Parent $Form -Text 'YTD Folder' -Left 380 -Top 307 -Width 150 -OnClick {
    Open-YtdFolder ''
}

$MaintenanceLabel = New-Object System.Windows.Forms.Label
$MaintenanceLabel.Text = 'Maintenance'
$MaintenanceLabel.Font = New-Object System.Drawing.Font('Segoe UI', 12, [System.Drawing.FontStyle]::Bold)
$MaintenanceLabel.AutoSize = $true
$MaintenanceLabel.Left = 40
$MaintenanceLabel.Top = 382
$Form.Controls.Add($MaintenanceLabel)

Add-DashboardButton -Parent $Form -Text 'Update yt-dlp' -Left 40 -Top 414 -Width 150 -OnClick {
    Start-YtdFile 'Maintenance\UpdateYTDLP.bat'
}

Add-DashboardButton -Parent $Form -Text 'Update FFmpeg' -Left 210 -Top 414 -Width 150 -OnClick {
    Start-YtdFile 'Maintenance\UpdateFFMPEG.bat'
}

Add-DashboardButton -Parent $Form -Text 'Instructions' -Left 380 -Top 414 -Width 150 -OnClick {
    Open-YtdDocument 'Instructions.txt'
}

$Footer = New-Object System.Windows.Forms.Label
$Footer.Text = 'YTD Dashboard'
$Footer.Font = New-Object System.Drawing.Font('Segoe UI', 9)
$Footer.ForeColor = [System.Drawing.Color]::Gray
$Footer.AutoSize = $true
$Footer.Left = 236
$Footer.Top = 486
$Form.Controls.Add($Footer)

[void]$Form.ShowDialog()
