import pygame, time, math

# Initialize Pygame
pygame.init()
s = pygame.display.set_mode((700, 700))
pygame.display.set_caption("Sun + Sky animation")
clock = pygame.time.Clock()

# Colors
yellow, gold, orange = (255, 255, 0), (255, 215, 0), (255, 165, 0)
dark_blue, light_blue = (0, 0, 80), (135, 206, 250)
pink, red, purple = (255, 182, 193), (220, 20, 60), (128, 0, 128)

# Sky gradient sequence (dawn -> day -> sunset -> night)
sky_colors = [pink, light_blue, red, purple, dark_blue]

# Circle settings
r_small, r_big = 100, 130
period = 4.0

def lerp(a, b, t):
    return a + (b - a) * t

center = (350, 350)

# Main loop
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            raise SystemExit

    # Time & animation phase (0 → 1 → 0)
    t = time.time() % period
    phase = abs(math.sin(math.pi * t / (period / 2)))

    # --- Sky: interpolate through multiple sky colors ---
    num_stages = len(sky_colors) - 1
    pos = phase * num_stages
    stage = int(pos)
    if stage >= num_stages:           # guard for phase == 1.0
        stage = num_stages - 1
    local_t = pos - stage
    c1 = sky_colors[stage]
    c2 = sky_colors[stage + 1]
    bg_color = tuple(int(lerp(c1[i], c2[i], local_t)) for i in range(3))
    s.fill(bg_color)

    # --- Sun color interpolation (yellow -> gold -> orange) ---
    if phase < 0.5:
        f = phase / 0.5
        sun_c1, sun_c2 = yellow, gold
    else:
        f = (phase - 0.5) / 0.5
        sun_c1, sun_c2 = gold, orange
    sun_color = tuple(int(lerp(sun_c1[i], sun_c2[i], f)) for i in range(3))

    # Sun radius
    radius = int(lerp(r_small, r_big, phase))
    pygame.draw.circle(s, sun_color, center, radius)

    # Sun rays (use sin for bounded, smooth offset)
    num_rays = 15
    ray_inner = radius + 15
    ray_outer = radius + 150
    steps = 20
    amp = 20

    for i in range(num_rays):
        angle = (2 * math.pi / num_rays) * i
        points = []
        dx, dy = math.cos(angle), math.sin(angle)
        px, py = -dy, dx  # perpendicular offset
        for j in range(steps + 1):
            r = lerp(ray_inner, ray_outer, j / steps)
            # bounded smooth offset
            offset = amp * math.sin(j * 0.5 + time.time() * 3)
            points.append((
                center[0] + int(dx * r + px * offset),
                center[1] + int(dy * r + py * offset)
            ))
        pygame.draw.lines(s, sun_color, False, points, 2)

    # Sunglasses
    glass_shift = int(20 * math.sin(time.time() * 2))
    lens_radius = max(1, radius // 4)
    lens_y = center[1]
    lens_gap = lens_radius + 10

    left_pos  = (center[0] - lens_gap + glass_shift, lens_y)
    right_pos = (center[0] + lens_gap + glass_shift, lens_y)

    pygame.draw.circle(s, (0, 0, 0), left_pos, lens_radius)
    pygame.draw.circle(s, (0, 0, 0), right_pos, lens_radius)
    pygame.draw.line(s, (0, 0, 0), left_pos, right_pos, 4)

    # Update screen
    pygame.display.flip()
    clock.tick(60)

