#!/usr/bin/env python3
"""Generate HevyBridge Android app icon."""

from PIL import Image, ImageDraw, ImageFilter
import math

SIZE = 512
OUTPUT = "/tmp/hevy-icon-android.png"

# Colors
BG_COLOR = (18, 18, 22)       # near-black
TEAL = (0, 191, 165)          # #00BFA5
TEAL_DIM = (0, 150, 130)      # darker teal for depth
WHITE_ISH = (220, 240, 236)   # faint glow

# Create base image with dark rounded square
img = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)

# Rounded square background (adaptive icon shape)
corner_radius = SIZE // 6
margin = SIZE // 20

# Draw rounded square background
def draw_rounded_rect(draw, xy, r, fill):
    x1, y1, x2, y2 = xy
    # Main rectangle
    draw.rectangle([x1 + r, y1, x2 - r, y2], fill=fill)
    draw.rectangle([x1, y1 + r, x2, y2 - r], fill=fill)
    # Corner circles
    draw.pieslice([x1, y1, x1 + 2*r, y1 + 2*r], 180, 270, fill=fill)
    draw.pieslice([x2 - 2*r, y1, x2, y1 + 2*r], 270, 360, fill=fill)
    draw.pieslice([x1, y2 - 2*r, x1 + 2*r, y2], 90, 180, fill=fill)
    draw.pieslice([x2 - 2*r, y2 - 2*r, x2, y2], 0, 90, fill=fill)

# Background
draw_rounded_rect(draw, [0, 0, SIZE, SIZE], corner_radius, BG_COLOR)

# Subtle inner border
border_r = corner_radius - 4
draw_rounded_rect(draw, [6, 6, SIZE-6, SIZE-6], border_r, None)
draw.rounded_rectangle([6, 6, SIZE-6, SIZE-6], radius=border_r, outline=(40, 40, 48, 100), width=2)

# --- Draw the bridge/connection symbol ---
cx = SIZE // 2
cy = SIZE // 2

# Phone icon (left side)
phone_w = SIZE // 6
phone_h = SIZE // 3
phone_x = cx - phone_w - SIZE // 10
phone_y = cy - phone_h // 2
phone_r = phone_w // 3

# Phone body
draw.rounded_rectangle(
    [phone_x, phone_y, phone_x + phone_w, phone_y + phone_h],
    radius=phone_r,
    fill=None,
    outline=TEAL,
    width=SIZE // 55
)
# Phone screen (inner)
screen_margin = phone_w // 4
draw.rounded_rectangle(
    [phone_x + screen_margin, phone_y + screen_margin,
     phone_x + phone_w - screen_margin, phone_y + phone_h - screen_margin],
    radius=phone_r - 2,
    fill=(0, 191, 165, 30),
    outline=None
)
# Phone "home button" dot
home_y = phone_y + phone_h - screen_margin - phone_w // 6
draw.ellipse(
    [phone_x + phone_w//2 - phone_w//10, home_y,
     phone_x + phone_w//2 + phone_w//10, home_y + phone_w//8],
    fill=TEAL
)

# Watch icon (right side)
watch_size = SIZE // 5  # square-ish
watch_x = cx + SIZE // 10
watch_y = cy - watch_size // 2
watch_r = watch_size // 2

# Watch body - rounded square
draw.rounded_rectangle(
    [watch_x, watch_y, watch_x + watch_size, watch_y + watch_size],
    radius=watch_r,
    fill=None,
    outline=TEAL,
    width=SIZE // 55
)
# Watch screen
watch_sm = watch_size // 5
draw.rounded_rectangle(
    [watch_x + watch_sm, watch_y + watch_sm,
     watch_x + watch_size - watch_sm, watch_y + watch_size - watch_sm],
    radius=watch_r - 3,
    fill=(0, 191, 165, 30),
    outline=None
)
# Watch crown (little bump on right)
crown_w = watch_size // 8
crown_h = watch_size // 4
crown_x = watch_x + watch_size - 2
crown_y = watch_y + watch_size // 3
draw.rounded_rectangle(
    [crown_x, crown_y, crown_x + crown_w, crown_y + crown_h],
    radius=crown_w,
    fill=TEAL
)

# Bridge connection (curved lines between phone and watch)
bridge_left = phone_x + phone_w + 1
bridge_right = watch_x - 1
bridge_mid_y = cy

# Data flow lines between devices
line_count = 3
line_spacing = SIZE // 20
for i in range(line_count):
    offset = (i - 1) * line_spacing
    y_pos = bridge_mid_y + offset
    
    # Draw a dashed or segmented line to suggest data flow
    segments = 5
    seg_len = (bridge_right - bridge_left) / segments
    for s in range(segments):
        sx = bridge_left + s * seg_len
        ex = sx + seg_len * 0.6
        alpha = 150 + int(55 * (s / segments))  # fade as it goes right
        seg_color = (*TEAL, alpha)
        draw.line([(sx, y_pos), (ex, y_pos)], fill=seg_color, width=SIZE//80)
    
    # Small dots at endpoints suggesting active connection
    dot_r = SIZE // 65
    draw.ellipse([bridge_left - dot_r, y_pos - dot_r, bridge_left + dot_r, y_pos + dot_r], fill=TEAL)
    draw.ellipse([bridge_right - dot_r, y_pos - dot_r, bridge_right + dot_r, y_pos + dot_r], fill=TEAL)

# Subtle glow behind the bridge area
glow_r = SIZE // 4
glow = Image.new("RGBA", (glow_r * 2, glow_r * 2), (0, 0, 0, 0))
glow_draw = ImageDraw.Draw(glow)
for a in range(60, 0, -2):
    glow_draw.ellipse([0, 0, glow_r*2, glow_r*2], fill=(0, 191, 165, a))
    glow_r -= 1

glow_x = cx - glow_r
glow_y = cy - glow_r
img.paste(glow, (glow_x, glow_y), glow)

# Save
img.save(OUTPUT, "PNG")
print(f"Icon saved to {OUTPUT} ({SIZE}x{SIZE})")
