#!/usr/bin/env python3
"""
Daily Curiosity #4 — Чому хмари білі, а грозові — майже чорні?
Schematic: a thin/white cloud (sunlight scatters and passes through) and
a dense/dark cloud (so much water and ice that most light is absorbed/scattered
sideways before reaching the observer below).
"""
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, Circle, FancyBboxPatch, Polygon
import numpy as np

# ---- Canvas ----
fig, ax = plt.subplots(figsize=(12, 7), dpi=160)
ax.set_xlim(0, 12)
ax.set_ylim(0, 7)
ax.set_aspect('equal')
ax.axis('off')

# Sky gradient — bright on top, dim near horizon (storm side)
sky = np.linspace(0, 1, 256).reshape(-1, 1)
sky = np.repeat(sky, 256, axis=1)
ax.imshow(
    sky,
    extent=(0, 12, 0, 7),
    cmap=plt.cm.colors.LinearSegmentedColormap.from_list(
        "stormy_sky", ["#0a1622", "#13243a", "#22405e", "#2a567f"][::-1]
    ),
    aspect='auto',
    zorder=0,
)

# Ground silhouette
ground = Polygon(
    [(0, 0), (12, 0), (12, 0.65), (0, 0.65)],
    facecolor='#0a1018', edgecolor='none', zorder=2,
)
ax.add_patch(ground)

# ---- Title and subtitle (Ukrainian) ----
ax.text(
    6, 6.55,
    "Чому хмари білі, а грозові — майже чорні?",
    ha='center', va='center',
    fontsize=24, fontweight='bold', color='#f4f9ff',
    zorder=10,
)
ax.text(
    6, 6.05,
    "Усе залежить від того, скільки світла встигає пройти крізь хмару до нас",
    ha='center', va='center',
    fontsize=12, style='italic', color='#cfe2ff',
    zorder=10,
)

# ---- Sun (top-left) ----
sun = Circle((1.2, 5.55), 0.36, color='#ffe17a', zorder=8, ec='#ffc347', lw=1.4)
ax.add_patch(sun)
for ang in range(0, 360, 30):
    a = np.deg2rad(ang)
    x0 = 1.2 + 0.46 * np.cos(a)
    y0 = 5.55 + 0.46 * np.sin(a)
    x1 = 1.2 + 0.7 * np.cos(a)
    y1 = 5.55 + 0.7 * np.sin(a)
    ax.plot([x0, x1], [y0, y1], color='#ffc347', lw=1.2, zorder=8)
ax.text(1.2, 4.6, "Сонце", ha='center', va='center',
        fontsize=10, color='#ffe17a', zorder=10)

# ---- Helper: draw a fluffy cloud as a cluster of circles ----
def draw_cloud(cx, cy, scale, fc, ec, alpha=1.0, zorder=5):
    # Several overlapping circles to make a cumulus shape
    blobs = [
        (-1.0, -0.05, 0.55),
        (-0.55, 0.30, 0.65),
        (-0.05, 0.45, 0.7),
        ( 0.45, 0.35, 0.65),
        ( 0.95, 0.05, 0.55),
        (-0.4, -0.25, 0.5),
        ( 0.35, -0.25, 0.55),
        ( 0.0, 0.05, 0.6),
    ]
    for dx, dy, r in blobs:
        c = Circle(
            (cx + dx * scale, cy + dy * scale),
            r * scale, facecolor=fc, edgecolor=ec, lw=1.2,
            alpha=alpha, zorder=zorder,
        )
        ax.add_patch(c)

# ---- Left cloud: thin, white (lots of light comes through) ----
cloud_left_cx, cloud_left_cy = 3.2, 3.5
draw_cloud(cloud_left_cx, cloud_left_cy, 0.95, "#f6fbff", "#cfdcec", zorder=5)

# Sun rays passing through left cloud and reaching observer
ray_color_thru = '#ffe17a'
for (x0, y0, x1, y1) in [
    (1.55, 5.30, 2.40, 4.30),
    (1.65, 5.10, 2.80, 4.05),
    (1.80, 4.95, 3.20, 3.95),
]:
    ax.plot([x0, x1], [y0, y1], color=ray_color_thru, lw=1.6, zorder=6)
# Rays exiting bottom of cloud (scattered, white = all colors)
for (x0, y0, x1, y1) in [
    (2.6, 3.05, 2.4, 1.4),
    (3.0, 2.95, 3.0, 1.4),
    (3.4, 3.00, 3.6, 1.4),
    (3.8, 3.10, 4.05, 1.4),
]:
    ax.plot([x0, x1], [y0, y1], color='#f6fbff', lw=1.5, zorder=6, alpha=0.95)

ax.text(cloud_left_cx, 4.55, "тонка хмара",
        ha='center', va='center', fontsize=12.5, fontweight='bold',
        color='#f6fbff', zorder=11)
ax.text(cloud_left_cx, 2.05, "світло проходить — \nми бачимо її білою",
        ha='center', va='center', fontsize=10.5, style='italic',
        color='#e3eefb', zorder=11)

# ---- Right cloud: thick, dark (most light absorbed/scattered sideways) ----
cloud_right_cx, cloud_right_cy = 8.8, 3.7
# Big base shadow blob
draw_cloud(cloud_right_cx, cloud_right_cy + 0.05, 1.25, "#3a4250", "#252b35", zorder=4)
# Slightly highlighted top edge (catches direct sun)
top_highlight_blobs = [
    (-0.55, 0.55, 0.45),
    (0.0, 0.70, 0.55),
    (0.55, 0.55, 0.45),
]
for dx, dy, r in top_highlight_blobs:
    c = Circle(
        (cloud_right_cx + dx * 1.25, cloud_right_cy + dy * 1.25),
        r * 1.25, facecolor='#8a96a9', edgecolor='none',
        alpha=0.7, zorder=5,
    )
    ax.add_patch(c)

# Sun rays hit the top of the storm cloud
for (x0, y0, x1, y1) in [
    (1.75, 5.25, 7.30, 4.95),
    (1.95, 5.10, 8.40, 5.10),
    (2.15, 4.95, 9.40, 4.85),
]:
    ax.plot([x0, x1], [y0, y1], color=ray_color_thru, lw=1.4, zorder=6, alpha=0.85)

# Few faint, scattered rays escape sideways from the storm cloud — not down
for (x0, y0, x1, y1) in [
    (7.2, 4.10, 6.4, 3.7),
    (10.4, 4.10, 11.2, 3.7),
    (7.0, 3.40, 6.2, 3.0),
    (10.6, 3.40, 11.4, 3.0),
]:
    ax.plot([x0, x1], [y0, y1], color='#9aa6ba', lw=1.2,
            zorder=6, alpha=0.8, linestyle='--')

# Rain drops below
rng = np.random.default_rng(42)
for i in range(70):
    rx = cloud_right_cx + rng.uniform(-1.6, 1.6)
    ry_top = 2.55 - rng.uniform(0, 1.6)
    ax.plot([rx, rx - 0.05], [ry_top, ry_top - 0.18],
            color='#7fbde4', lw=1.0, alpha=0.85, zorder=5)

# A subtle lightning bolt
bolt = Polygon(
    [(8.85, 2.55), (8.55, 1.85), (8.85, 1.85),
     (8.50, 0.95), (8.95, 1.65), (8.65, 1.65), (9.00, 2.30)],
    closed=True, facecolor='#ffe17a', edgecolor='#ffc347',
    lw=1.0, zorder=7,
)
ax.add_patch(bolt)

ax.text(cloud_right_cx, 5.35, "груба, щільна\nгрозова хмара",
        ha='center', va='center', fontsize=12.5, fontweight='bold',
        color='#f4f9ff', zorder=11)
ax.text(cloud_right_cx, 2.05, "крапель так багато, що \nдо нас доходить мало світла",
        ha='center', va='center', fontsize=10.5, style='italic',
        color='#cfdcec', zorder=11)

# ---- Observer (small figure on the ground) ----
def draw_observer(cx, cy):
    # Head
    ax.add_patch(Circle((cx, cy + 0.32), 0.12, facecolor='#f4d6a6',
                        edgecolor='#3b2a18', lw=0.8, zorder=9))
    # Body
    ax.plot([cx, cx], [cy + 0.20, cy - 0.10], color='#f4d6a6', lw=2.4, zorder=9)
    # Legs
    ax.plot([cx, cx - 0.10], [cy - 0.10, cy - 0.35], color='#3b2a18', lw=2.0, zorder=9)
    ax.plot([cx, cx + 0.10], [cy - 0.10, cy - 0.35], color='#3b2a18', lw=2.0, zorder=9)
    # Arms — looking up
    ax.plot([cx, cx - 0.18], [cy + 0.10, cy + 0.25], color='#f4d6a6', lw=1.8, zorder=9)
    ax.plot([cx, cx + 0.18], [cy + 0.10, cy + 0.25], color='#f4d6a6', lw=1.8, zorder=9)

draw_observer(3.2, 0.95)
draw_observer(8.8, 0.95)

# ---- Comparative legend (bottom center) ----
legend_box = FancyBboxPatch(
    (4.3, 0.05), 3.4, 0.6,
    boxstyle="round,pad=0.06,rounding_size=0.12",
    fc="#0a1622", ec="#cfe2ff", lw=1.0, zorder=7,
)
ax.add_patch(legend_box)
ax.text(6.0, 0.50, "Ключ: товщина хмари",
        ha='center', va='center', fontsize=10, fontweight='bold',
        color='#f4f9ff', zorder=10)
ax.text(6.0, 0.22,
        "тонка → біла   •   товста → темна",
        ha='center', va='center', fontsize=9.5,
        color='#cfe2ff', zorder=10)

plt.tight_layout()
out = "/sessions/gifted-sharp-dijkstra/mnt/daily-curiosity/daily-curiosity-04.png"
plt.savefig(out, dpi=160, facecolor='#0a1622', bbox_inches='tight', pad_inches=0.15)
print(f"Saved: {out}")
