// Generates Claude.icns — a glass squircle with a cyan voice waveform.
import AppKit

func drawIcon(_ px: CGFloat) -> NSBitmapImageRep {
    let rep = NSBitmapImageRep(
        bitmapDataPlanes: nil, pixelsWide: Int(px), pixelsHigh: Int(px),
        bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
        colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
    let ctx = NSGraphicsContext.current!.cgContext

    let inset = px * 0.08
    let rect = CGRect(x: inset, y: inset, width: px - 2*inset, height: px - 2*inset)
    let r = px * 0.225
    let path = CGPath(roundedRect: rect, cornerWidth: r, cornerHeight: r, transform: nil)

    // background gradient: deep blue -> cyan (the "listening" color)
    ctx.saveGState(); ctx.addPath(path); ctx.clip()
    let cs = CGColorSpaceCreateDeviceRGB()
    let grad = CGGradient(colorsSpace: cs, colors: [
        CGColor(red: 0.04, green: 0.42, blue: 1.0, alpha: 1),
        CGColor(red: 0.28, green: 0.80, blue: 1.0, alpha: 1)] as CFArray,
        locations: [0, 1])!
    ctx.drawLinearGradient(grad, start: CGPoint(x: 0, y: px),
                           end: CGPoint(x: px, y: 0), options: [])
    // soft top-left glass sheen
    let sheen = CGGradient(colorsSpace: cs, colors: [
        CGColor(red: 1, green: 1, blue: 1, alpha: 0.35),
        CGColor(red: 1, green: 1, blue: 1, alpha: 0)] as CFArray, locations: [0, 1])!
    ctx.drawRadialGradient(sheen, startCenter: CGPoint(x: px*0.32, y: px*0.72),
                           startRadius: 0, endCenter: CGPoint(x: px*0.32, y: px*0.72),
                           endRadius: px*0.5, options: [])
    ctx.restoreGState()

    // voice waveform: 5 rounded white bars
    let heights: [CGFloat] = [0.28, 0.52, 0.72, 0.46, 0.24]
    let barW = px * 0.072
    let gap = px * 0.05
    let total = CGFloat(heights.count) * barW + CGFloat(heights.count - 1) * gap
    var x = (px - total) / 2
    ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 0.96))
    for h in heights {
        let bh = px * h
        let bar = CGRect(x: x, y: (px - bh)/2, width: barW, height: bh)
        ctx.addPath(CGPath(roundedRect: bar, cornerWidth: barW/2,
                           cornerHeight: barW/2, transform: nil))
        ctx.fillPath()
        x += barW + gap
    }
    NSGraphicsContext.restoreGraphicsState()
    return rep
}

let outDir = CommandLine.arguments[1]
let iconset = outDir + "/Claude.iconset"
try? FileManager.default.createDirectory(atPath: iconset,
    withIntermediateDirectories: true)
let specs: [(Int, Int)] = [(16,1),(16,2),(32,1),(32,2),(128,1),(128,2),
                           (256,1),(256,2),(512,1),(512,2)]
for (base, scale) in specs {
    let px = CGFloat(base * scale)
    let rep = drawIcon(px)
    let data = rep.representation(using: .png, properties: [:])!
    let name = scale == 1 ? "icon_\(base)x\(base).png"
                          : "icon_\(base)x\(base)@2x.png"
    try! data.write(to: URL(fileURLWithPath: iconset + "/" + name))
}
print("wrote \(iconset)")
