import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import colorsys

# --- 1. THE SPIRITUAL MATHEMATICS ENGINE ---
def calculate_spectral_data(x):
    """
    Implements the 'Grand Unified' mapping: Color = i^(x * pi)
    
    The math:
    i = e^(i * pi/2)
    i^(x*pi) = e^(i * (x * pi^2) / 2)
    Angle (radians) = x * (pi^2) / 2
    Angle (degrees) = x * 90 * pi
    """
    # Calculate the angle in the complex plane
    angle_deg = (x * 90 * np.pi) 
    
    # Normalize to 0-360 for the color wheel
    hue_deg = angle_deg % 360
    
    # Normalize to 0-1 for Python's color systems
    hue_norm = hue_deg / 360.0
    
    return hue_deg, hue_norm

# --- 2. CONFIGURING THE VISUALIZATION (Pure Ada) ---
plt.style.use('dark_background') # The truth is best seen in the dark
fig = plt.figure(figsize=(16, 10))
grid = plt.GridSpec(2, 2, height_ratios=[2, 1])

# --- 3. PLOT A: THE COMPLEX SPIRAL (The "Circle of Fifths" for Light) ---
ax_polar = fig.add_subplot(grid[0, 0], projection='polar')
ax_polar.set_title("The Spectral Spiral: $i^{x\pi}$", color='white', fontsize=16, pad=20)

# Generate the continuous spiral
x_smooth = np.linspace(0, 6, 2000)
angles_smooth = []
radii_smooth = []
colors_smooth = []

for x in x_smooth:
    h_deg, h_norm = calculate_spectral_data(x)
    angles_smooth.append(np.deg2rad(h_deg))
    radii_smooth.append(1.0 + (x * 0.1)) # Spiral outward slightly to show time/progression
    colors_smooth.append(colorsys.hsv_to_rgb(h_norm, 1.0, 1.0))

# Plot the spiral path
ax_polar.scatter(angles_smooth, radii_smooth, c=colors_smooth, s=2, alpha=0.6)

# Plot the INTEGER "STATIONS" (0, 1, 2, 3...)
key_integers = [0, 1, 2, 3, 4, 5, 6]
for i in key_integers:
    h_deg, h_norm = calculate_spectral_data(i)
    rad = np.deg2rad(h_deg)
    r_pos = 1.0 + (i * 0.1)
    
    # Convert HSV to RGB
    c_rgb = colorsys.hsv_to_rgb(h_norm, 1.0, 1.0)
    
    # The Marker
    ax_polar.scatter(rad, r_pos, color=c_rgb, s=300, edgecolors='white', linewidth=2, zorder=10)
    
    # The Label
    label_text = f"x={i}"
    if i == 3:
        label_text = f"x={i}\nGREEN!"
        # Add a "Holy" circle around 3
        ax_polar.scatter(rad, r_pos, s=1000, facecolors='none', edgecolors='lime', linewidth=3, linestyle='--')
        
    ax_polar.text(rad, r_pos + 0.15, label_text, ha='center', va='center', fontsize=11, fontweight='bold', color='white')

ax_polar.grid(True, alpha=0.3)
ax_polar.set_xticklabels([])
ax_polar.set_yticklabels([])


# --- 4. PLOT B: THE TRUTH TABLE (Mathematical Verification) ---
ax_text = fig.add_subplot(grid[0, 1])
ax_text.axis('off')

# Calculate the precise "Greenness" of 3
deg_3, _ = calculate_spectral_data(3)
perfect_green = 120.0
diff = abs(deg_3 - perfect_green)

text_content = (
    f"THE MATHEMATICAL PROOF\n"
    f"----------------------------------------\n"
    f"Mapping Function: Color = $i^{{x\pi}}$\n\n"
    f"At x = 0 (Red):\n"
    f"  Angle = 0.0°\n\n"
    f"At x = 2 (Blue-ish/Cyan):\n"
    f"  Angle = {(calculate_spectral_data(2)[0]):.2f}°\n\n"
    f"At x = 3 (THE REVELATION):\n"
    f"  Angle = 3 * 90 * $\pi$\n"
    f"  Angle = {deg_3:.4f}°\n"
    f"  Standard Green Hue = {perfect_green}°\n"
    f"  Difference = {diff:.2f}°\n\n"
    f"CONCLUSION:\n"
    f"  3 lands in the GREEN sector.\n"
    f"  Mathematics is done.\n"
)

ax_text.text(0.1, 0.5, text_content, fontsize=14, fontfamily='monospace', color='lime', va='center')


# --- 5. PLOT C: THE UNIVERSAL SAILING STRIP (Linear View) ---
ax_strip = fig.add_subplot(grid[1, :])
ax_strip.set_title("Universal Sailing Strip (x=0 to x=6)", color='white', fontsize=14)

# Create the visualization strip
x_strip = np.linspace(0, 6, 1000)
image_strip = []

for x in x_strip:
    _, h_norm = calculate_spectral_data(x)
    image_strip.append(colorsys.hsv_to_rgb(h_norm, 1.0, 1.0))

# Display as an image
image_strip = np.array([image_strip for _ in range(100)]) # Make it a tall strip
ax_strip.imshow(image_strip, extent=[0, 6, 0, 1], aspect='auto')

# Mark the locations
for i in key_integers:
    ax_strip.axvline(i, color='white', linestyle=':', alpha=0.5)
    ax_strip.text(i, -0.2, str(i), ha='center', color='white', fontsize=12, fontweight='bold')
    if i == 3:
         ax_strip.text(i, 0.5, "GREEN", ha='center', color='black', fontsize=12, fontweight='bold', rotation=90)

ax_strip.axis('off')

# --- 6. FINALIZE ---
plt.suptitle("SPECTRAL GRAND UNIFICATION: 3 === GREEN?!", fontsize=22, color='white', fontweight='bold', y=0.98)
plt.tight_layout()
plt.show()