Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationCore

$outDir = "C:\Users\smithm\Desktop\browser\chrome\skin\classic"
$icoDir = "C:\Users\smithm\Desktop\browser\icons\default"

function New-Canvas([int]$w, [int]$h) {
    $bmp = New-Object System.Drawing.Bitmap($w, $h,
        [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
    $g = [System.Drawing.Graphics]::FromImage($bmp)
    $g.Clear([System.Drawing.Color]::Transparent)
    $g.SmoothingMode   = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
    $g.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
    return $bmp, $g
}

function Save-Png($bmp, [string]$name) {
    $path = Join-Path $outDir $name
    $bmp.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
    Write-Host "  Saved $name  ($([int](Get-Item $path).Length) bytes)"
}

# Build a PointF array from flat x,y pairs: Pts 5,12, 13,5, ...
function Pts([float[]]$coords) {
    $pts = New-Object System.Drawing.PointF[] ($coords.Count / 2)
    for ($i = 0; $i -lt $pts.Count; $i++) {
        $pts[$i] = [System.Drawing.PointF]::new($coords[$i*2], $coords[$i*2+1])
    }
    return $pts
}

$white       = [System.Drawing.Color]::White
$shadow      = [System.Drawing.Color]::FromArgb(80, 0, 0, 0)
$red         = [System.Drawing.Color]::FromArgb(255, 224, 50, 50)    # vivid red stop X
$lightBlue   = [System.Drawing.Color]::FromArgb(200, 200, 230, 255)
$doorColor   = [System.Drawing.Color]::FromArgb(180, 100, 150, 210)
$tbBlue      = [System.Drawing.Color]::FromArgb(255, 46, 127, 216)   # new toolbar mid-blue
# IE6-style vivid green for back/forward arrows
$arrowGreen  = [System.Drawing.Color]::FromArgb(255, 30, 172, 30)
$arrowGreenH = [System.Drawing.Color]::FromArgb(255, 80, 210, 80)    # highlight edge
$arrowShadow = [System.Drawing.Color]::FromArgb(90, 0, 70, 0)        # dark green shadow

Write-Host "Generating IE6-style toolbar icons..."

# ── Back arrow (←) — IE6 vivid green ──────────────────────────────────────────
$bmp, $g = New-Canvas 24 24
$gb  = New-Object System.Drawing.SolidBrush($arrowGreen)
$ghb = New-Object System.Drawing.SolidBrush($arrowGreenH)
$shb = New-Object System.Drawing.SolidBrush($arrowShadow)
$pts  = Pts @([float[]](5,12, 13,5, 13,9, 19,9, 19,15, 13,15, 13,19))
$ptsS = Pts @([float[]](6,13, 14,6, 14,10, 20,10, 20,16, 14,16, 14,20))
# Top-left highlight sliver (inner top edge of arrow)
$ptsH = Pts @([float[]](5,12, 13,5, 13,7, 8,12))
$g.FillPolygon($shb, $ptsS)
$g.FillPolygon($gb,  $pts)
$g.FillPolygon($ghb, $ptsH)
Save-Png $bmp "toolbar-back.png"
$g.Dispose(); $bmp.Dispose(); $gb.Dispose(); $ghb.Dispose(); $shb.Dispose()

# ── Forward arrow (→) — IE6 vivid green ───────────────────────────────────────
$bmp, $g = New-Canvas 24 24
$gb  = New-Object System.Drawing.SolidBrush($arrowGreen)
$ghb = New-Object System.Drawing.SolidBrush($arrowGreenH)
$shb = New-Object System.Drawing.SolidBrush($arrowShadow)
$pts  = Pts @([float[]](19,12, 11,5, 11,9, 5,9, 5,15, 11,15, 11,19))
$ptsS = Pts @([float[]](20,13, 12,6, 12,10, 6,10, 6,16, 12,16, 12,20))
# Top highlight on the arrowhead tip area
$ptsH = Pts @([float[]](19,12, 11,5, 11,7, 16,12))
$g.FillPolygon($shb, $ptsS)
$g.FillPolygon($gb,  $pts)
$g.FillPolygon($ghb, $ptsH)
Save-Png $bmp "toolbar-forward.png"
$g.Dispose(); $bmp.Dispose(); $gb.Dispose(); $ghb.Dispose(); $shb.Dispose()

# ── Stop (✕ red X) ────────────────────────────────────────────────────────────
$bmp, $g = New-Canvas 24 24
$pen  = New-Object System.Drawing.Pen($red,    [float]3.5)
$penS = New-Object System.Drawing.Pen($shadow, [float]3.5)
foreach ($p in @($pen, $penS)) {
    $p.StartCap = [System.Drawing.Drawing2D.LineCap]::Round
    $p.EndCap   = [System.Drawing.Drawing2D.LineCap]::Round
}
$g.DrawLine($penS, [float]6,  [float]6,  [float]19, [float]19)
$g.DrawLine($penS, [float]19, [float]6,  [float]6,  [float]19)
$g.DrawLine($pen,  [float]5,  [float]5,  [float]18, [float]18)
$g.DrawLine($pen,  [float]18, [float]5,  [float]5,  [float]18)
Save-Png $bmp "toolbar-stop.png"
$g.Dispose(); $bmp.Dispose(); $pen.Dispose(); $penS.Dispose()

# ── Reload (↻ thick green arc, upward arrowhead at 9-o'clock end) ─────────────
# Arc: from 270° (12 o'clock) clockwise 270°, ending at 180° (9 o'clock, left)
# At 9 o'clock the clockwise tangent points upward → arrowhead points up
$bmp, $g = New-Canvas 24 24
$pen  = New-Object System.Drawing.Pen($arrowGreen,  [float]3.2)
$penS = New-Object System.Drawing.Pen($arrowShadow, [float]3.2)
foreach ($p in @($pen, $penS)) {
    $p.StartCap = [System.Drawing.Drawing2D.LineCap]::Round
    $p.EndCap   = [System.Drawing.Drawing2D.LineCap]::Round
}
$gb  = New-Object System.Drawing.SolidBrush($arrowGreen)
$ghb = New-Object System.Drawing.SolidBrush($arrowGreenH)
$shb = New-Object System.Drawing.SolidBrush($arrowShadow)
# Shadow arc (+1,+1)
$g.DrawArc($penS, [float]5, [float]5, [float]15, [float]15, [float]270, [float]270)
# Main green arc: center (11.5,11.5) r=7.5, arc box (4,4,15,15)
$g.DrawArc($pen,  [float]4, [float]4, [float]15, [float]15, [float]270, [float]270)
# Arrowhead at 9-o'clock end (left side, pointing up)
# Arc end point at 180°: x=4+7.5=11.5-7.5=4, y=4+7.5=11.5 → approx (4, 11.5)
# Upward-pointing triangle centred on the arc end
$arrowS = Pts @([float[]](3,13, 7,13, 5,8))   # shadow +1
$arrow  = Pts @([float[]](2,12, 6,12, 4,7))    # upward arrowhead
$g.FillPolygon($shb, $arrowS)
$g.FillPolygon($gb,  $arrow)
$reloadHL = Pts @([float[]](2,12, 4,7, 4,10))
$g.FillPolygon($ghb, $reloadHL)  # left-face highlight
Save-Png $bmp "toolbar-reload.png"
$g.Dispose(); $bmp.Dispose()
$pen.Dispose(); $penS.Dispose(); $gb.Dispose(); $ghb.Dispose(); $shb.Dispose()

# ── Home (bold golden house, no chimney) ───────────────────────────────────────
# Classic IE6 golden/amber house colour, big readable shapes at 24×24
$gold      = [System.Drawing.Color]::FromArgb(255, 240, 165, 20)   # warm amber
$goldLight = [System.Drawing.Color]::FromArgb(255, 255, 210, 80)   # roof highlight
$goldDark  = [System.Drawing.Color]::FromArgb(255, 180, 110, 10)   # shadow side
$doorBrown = [System.Drawing.Color]::FromArgb(255, 100,  55,  5)   # dark door
$bmp, $g = New-Canvas 24 24
$goldB  = New-Object System.Drawing.SolidBrush($gold)
$goldLB = New-Object System.Drawing.SolidBrush($goldLight)
$goldDB = New-Object System.Drawing.SolidBrush($goldDark)
$doorBr = New-Object System.Drawing.SolidBrush($doorBrown)
$shb    = New-Object System.Drawing.SolidBrush($shadow)
$outPen = New-Object System.Drawing.Pen($goldDark, [float]1)

# Drop shadow
$roofS  = Pts @([float[]](13,4, 22,14, 3,14))
$g.FillPolygon($shb, $roofS)
$g.FillRectangle($shb, [float]5, [float]14, [float]15, [float]8)

# House body (walls)
$g.FillRectangle($goldB,  [float]4, [float]13, [float]15, [float]8)
# Right wall darker shading
$g.FillRectangle($goldDB, [float]14,[float]13, [float]5,  [float]8)

# Roof triangle (main gold)
$roof   = Pts @([float[]](12,2, 22,13, 2,13))
$g.FillPolygon($goldB,  $roof)
# Roof top-left highlight (lighter edge)
$roofHL = Pts @([float[]](12,2, 2,13, 5,13))
$g.FillPolygon($goldLB, $roofHL)

# Outline the whole house
$g.DrawPolygon($outPen, $roof)
$g.DrawRectangle($outPen, [float]4, [float]13, [float]14, [float]7)

# Door (centered, arched top look via rectangle)
$g.FillRectangle($doorBr, [float]9, [float]15, [float]5, [float]6)
# Door highlight (left inner edge)
$doorHL = [System.Drawing.Color]::FromArgb(120, 255, 200, 80)
$doorHLB = New-Object System.Drawing.SolidBrush($doorHL)
$g.FillRectangle($doorHLB, [float]9, [float]15, [float]1, [float]6)
$doorHLB.Dispose()

Save-Png $bmp "toolbar-home.png"
$g.Dispose(); $bmp.Dispose()
$goldB.Dispose(); $goldLB.Dispose(); $goldDB.Dispose()
$doorBr.Dispose(); $shb.Dispose(); $outPen.Dispose()

# ── Throbber GIF (8 frames, 22×22) ────────────────────────────────────────────
Write-Host "  Generating throbber.gif (8 frames)..."
$frameCount = 8
$size = 22
$bitmaps = @()
for ($f = 0; $f -lt $frameCount; $f++) {
    $bmp, $g = New-Canvas $size $size
    $g.Clear($tbBlue)
    $cx = [float]($size / 2.0)
    $cy = [float]($size / 2.0)
    for ($i = 0; $i -lt 8; $i++) {
        $angle = ($f * 45 + $i * 45) * [Math]::PI / 180.0
        $r  = 7.0
        $dx = $cx + [float]($r * [Math]::Cos($angle))
        $dy = $cy + [float]($r * [Math]::Sin($angle))
        $alpha = [int](35 + $i * 28)
        if ($alpha -gt 255) { $alpha = 255 }
        $col = [System.Drawing.Color]::FromArgb($alpha, 255, 255, 255)
        $br  = New-Object System.Drawing.SolidBrush($col)
        $dr  = [float]2.3
        $g.FillEllipse($br, ($dx - $dr), ($dy - $dr), ($dr * 2), ($dr * 2))
        $br.Dispose()
    }
    $g.Dispose()
    $bitmaps += $bmp
}

$enc = New-Object System.Windows.Media.Imaging.GifBitmapEncoder
foreach ($b in $bitmaps) {
    $ms = New-Object System.IO.MemoryStream
    $b.Save($ms, [System.Drawing.Imaging.ImageFormat]::Bmp)
    $ms.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
    $src = [System.Windows.Media.Imaging.BitmapFrame]::Create(
        $ms,
        [System.Windows.Media.Imaging.BitmapCreateOptions]::None,
        [System.Windows.Media.Imaging.BitmapCacheOption]::OnLoad)
    $enc.Frames.Add($src)
    $ms.Dispose()
}
$gifPath = Join-Path $outDir "throbber.gif"
$fs = New-Object System.IO.FileStream($gifPath, [System.IO.FileMode]::Create)
$enc.Save($fs)
$fs.Close()
foreach ($b in $bitmaps) { $b.Dispose() }
Write-Host "  Saved throbber.gif"

# ── Window icon ICO (32×32 + 16×16) ──────────────────────────────────────────
Write-Host "  Generating default.ico..."

function Draw-EpochIcon($g, [int]$w, [int]$h) {
    $bg = New-Object System.Drawing.SolidBrush(
        [System.Drawing.Color]::FromArgb(255, 30, 80, 180))
    $g.FillEllipse($bg, [float]1, [float]1, [float]($w-2), [float]($h-2))
    $bg.Dispose()
    $wb   = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
    $sz   = [float]($h * 0.58)
    $font = New-Object System.Drawing.Font("Arial", $sz,
        [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Pixel)
    $sf   = New-Object System.Drawing.StringFormat
    $sf.Alignment     = [System.Drawing.StringAlignment]::Center
    $sf.LineAlignment = [System.Drawing.StringAlignment]::Center
    $rect = New-Object System.Drawing.RectangleF([float]0, [float]0, [float]$w, [float]$h)
    $g.DrawString("E", $font, $wb, $rect, $sf)
    $font.Dispose(); $wb.Dispose(); $sf.Dispose()
}

function Get-DIBBytes($bmp) {
    $ms   = New-Object System.IO.MemoryStream
    $bmp2 = New-Object System.Drawing.Bitmap($bmp.Width, $bmp.Height,
        [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
    $g2   = [System.Drawing.Graphics]::FromImage($bmp2)
    $g2.DrawImageUnscaled($bmp, 0, 0)
    $g2.Dispose()
    $bmp2.Save($ms, [System.Drawing.Imaging.ImageFormat]::Bmp)
    $bmp2.Dispose()
    $all = $ms.ToArray()
    $ms.Dispose()
    # Strip 14-byte BITMAPFILEHEADER, return DIB (BITMAPINFOHEADER + pixels)
    return $all[14..($all.Length - 1)]
}

$icoSizes  = @(32, 16)
$icoFrames = @()
foreach ($sz in $icoSizes) {
    $bmp, $g = New-Canvas $sz $sz
    Draw-EpochIcon $g $sz $sz
    $g.Dispose()
    $icoFrames += $bmp
}
$dibs = $icoFrames | ForEach-Object { Get-DIBBytes $_ }

$ms = New-Object System.IO.MemoryStream
# ICO file header: reserved=0, type=1 (icon), count
$ms.Write([byte[]](0,0, 1,0, [byte]$icoSizes.Count, 0), 0, 6)
# Directory entries (16 bytes each)
$dataOffset = [uint32](6 + 16 * $icoSizes.Count)
for ($i = 0; $i -lt $icoSizes.Count; $i++) {
    $sz  = [byte]$icoSizes[$i]
    $len = [uint32]$dibs[$i].Length
    $ms.WriteByte($sz); $ms.WriteByte($sz)  # width, height
    $ms.WriteByte(0);   $ms.WriteByte(0)    # colorCount, reserved
    $ms.Write([byte[]](1,0),  0, 2)         # planes = 1
    $ms.Write([byte[]](32,0), 0, 2)         # bitCount = 32
    $ms.Write([System.BitConverter]::GetBytes($len),        0, 4)
    $ms.Write([System.BitConverter]::GetBytes($dataOffset), 0, 4)
    $dataOffset += $len
}
foreach ($dib in $dibs) { $ms.Write($dib, 0, $dib.Length) }

$icoPath = Join-Path $icoDir "default.ico"
$fs = New-Object System.IO.FileStream($icoPath, [System.IO.FileMode]::Create)
$ms.WriteTo($fs)
$fs.Close()
$ms.Dispose()
foreach ($b in $icoFrames) { $b.Dispose() }
Write-Host "  Saved default.ico"

Write-Host ""
Write-Host "All assets generated."
