
# It's tradition to document stuff like this poorly...
# Also, a random comment nobody will understand:
#     Class started 2 hours ago, oh, am I late?
#     No, I already graduated
#     And you can live through anything if Magic made it
#     They say I talk with so much emphasis
#     Ooh, they so sensitive
#     Don't ever fix your lips like collagen
#     And say something when you gon' end up apolog'ing
#     Let me know if it's a problem then, aight, man, holla, then

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

$form = New-Object System.Windows.Forms.Form
$form.Text = "adesso App2Cloud Microsoft 365 Forms Auto"
$form.Width = 620
$form.Height = 300
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D

# Create the header label
$header = New-Object System.Windows.Forms.Label
$header.Text = "App2Cloud Microsoft 365 Forms Auto"
$header.Font = New-Object System.Drawing.Font("Impact", 18, [System.Drawing.FontStyle]::Bold)
$header.ForeColor = [System.Drawing.Color]::DarkBlue
$header.Width = 620
$header.Height = 40
$header.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$header.Location = New-Object System.Drawing.Point(0, 100)
$form.Controls.Add($header)


# Create Button 1
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Load Spec"
$button1.Font = New-Object System.Drawing.Font("Arial", 10)
$button1.Width = 100
$button1.Height = 40
$button1.Location = New-Object System.Drawing.Point(160, 170)
$form.Controls.Add($button1)

# Create Button 2
$button2 = New-Object System.Windows.Forms.Button
$button2.Text = "Get Template"
$button2.Font = New-Object System.Drawing.Font("Arial", 10)
$button2.Width = 100
$button2.Height = 40
$button2.Location = New-Object System.Drawing.Point(360, 170)
$form.Controls.Add($button2)

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 30 # Controls animation speed

$font = New-Object System.Drawing.Font("Jokerman", 20, [System.Drawing.FontStyle]::Bold)
$brush = [System.Drawing.Brushes]::Blue
$sineOffset = 20
$tileSize = 20       # Size of each square in the checkered pattern

Set-Variable -Name angle -Scope Global -Value 0
Set-Variable -Name text -Scope Global -Value ""
Set-Variable -Name hueShift -Scope Global -Value 0
Set-Variable -Name textHueShift -Scope Global -Value 0

Function GetColorFromHue ($hue) {
    $hue %= 360 # Keep hue within 0-359
    $c = 255
    $x = 255 * (1 - [Math]::Abs(($hue / 60) % 2 - 1))
    $m = 0

    if ($hue -lt 60) { return [System.Drawing.Color]::FromArgb($c, $x, $m) }
    elseif ($hue -lt 120) { return [System.Drawing.Color]::FromArgb($x, $c, $m) }
    elseif ($hue -lt 180) { return [System.Drawing.Color]::FromArgb($m, $c, $x) }
    elseif ($hue -lt 240) { return [System.Drawing.Color]::FromArgb($m, $x, $c) }
    elseif ($hue -lt 300) { return [System.Drawing.Color]::FromArgb($x, $m, $c) }
    else { return [System.Drawing.Color]::FromArgb($c, $m, $x) }
}

$graphicsArea = New-Object System.Drawing.Rectangle(0, 0, $form.Width, 100)

# TODO: fix buffering issue to get rid of that annoying flickering...
$form.add_Paint({
    $graphicsContext = [System.Drawing.BufferedGraphicsManager]::Current
    $buffer = $graphicsContext.Allocate($form.CreateGraphics(), $graphicsArea)

    $graphics = $buffer.Graphics
    $graphics.Clear([System.Drawing.Color]::White) # Clear background color (white base)

    # Checkered background
    # TODO: Limit to bounding box of wavy text
    for ($x = 0; $x -lt $form.Width; $x += $tileSize) {
        for ($y = 0; $y -lt 100; $y += $tileSize) {
            if ((($x / $tileSize) + ($y / $tileSize)) % 2 -eq 0) {
                $graphics.FillRectangle(
                    (New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(240, 245, 245))),
                    $x,
                    $y,
                    $tileSize,
                    $tileSize
                )
            }
        }
    }

    # Draw the wobbly text
    if (![string]::IsNullOrWhiteSpace($Global:text)) {
        $x = 0
        for ($i = 0; $i -lt $Global:text.Length; $i++) {
            $y = 20 + [Math]::Sin($Global:angle + $i * 0.5) * $sineOffset

            $textColor = GetColorFromHue($Global:textHueShift)

            $graphics.DrawString(
                $Global:text[$i],
                $font,
                (New-Object System.Drawing.SolidBrush $textColor),
                $x,
                $y
            )
            $x += 30
        }
    }

    # Render the buffered graphics to the screen
    $buffer.Render()
    $buffer.Dispose()
})

$timer.add_Tick({
    $Global:angle += 0.1
    $Global:hueShift += 0 # Slowly shift the hue
    $Global:textHueShift += 1 # Slowly shift the hue
    $Global:text = "@Tiaradessi $(Get-Date -UFormat "%T")"
    $form.Invalidate()
})

$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "untitled.wav"
$Song.PlayLooping()

# Start the animation and show the form
$timer.Start()
$form.ShowDialog()
