#version 330 core 
#define GLSL_VERSION 3.00
#define PI 3.14159265359

out vec4 FragColor;

in vec2 uv;

uniform vec3 data;
//uniform float time;
//varying vec2 uv;

// vortex by grok
void main() {
    float x = uv.y;
    //float y = uv.x*3-1; // oval 1:3
    //float y = uv.x*2-.5; // oval 1:2
    //float y = uv.x*1.5-.25; // oval 1:1.5
    float y = uv.x; // circle



    // Center the coordinates around (0.5, 0.5)
    vec2 centered = uv - vec2(0.5, 0.5);

    //float time=data.x;
    float rad=PI/180.0;
    //float time=(data.x * 1.0 / 86400) * 25000; // 50 breaks
    //float time = (data.x * rad) * 25; // 50 breaks

    float phase = data.x;

    



//-- // Example: Day-night cycle
   //-- float light = 0.5 + 0.5 * sin(uTime); // 0 to 1 over 86400 seconds
   //-- 
   //-- // Example: Cloud movement
   //-- vec2 cloudOffset = vec2(sin(uTime), cos(uTime)) * 0.5;
   //-- 
   //-- // Example: Star twinkle every 10 seconds
   float twinkleSpeed = 86400.0 / 3.0;

    //float time = sin(phase * twinkleSpeed);
    float time = phase * twinkleSpeed;





    
    // Convert to polar coordinates
    float r = length(centered);           // Radius from center
    float theta = atan(centered.y, centered.x); // Angle from center
    
    // Define constants
    float k = 1.0;              // Rotation speed factor
    float epsilon = 0.01;       // Small value to prevent division by zero
    float noiseAmplitude = 0.5; // Strength of the noise effect
    float numArms = 15;        // Number of spiral arms
    
    // Generate fake friction noise based on angle, radius, and time
    //float noise = sin(theta * 10.0 + r * 5.0 + time * 1.0);
    float noise = sin(theta * 1.0 + r * 5.0 - time * .2);
    
    // Calculate angular speed, faster near the center
    float omega = k / (r + epsilon);
    
    // Total rotation angle, including noise for irregularity
    //float rotation = omega / phase + noiseAmplitude * noise;
    //float theta_rot = theta + rotation;
    //float rotation = phase;
    //float theta_rot = theta + rotation;
    //vec2 vortex_pos = vec2(cos(theta_rot), sin(theta_rot)) * r;

    float rotation = omega * phase + noiseAmplitude * noise;
    float theta_rot = theta + rotation;
    
    // Create a spiral pattern
    float pattern = sin(numArms * theta_rot) * 1; // 3
    
    // Map pattern to intensity (range 0 to 1)
    float intensity = 0.5 + 0.5 * pattern;
    //float intensity = vortex_pos.x + vortex_pos.y * pattern;
    
    // Define purplish color
    vec3 purple = vec3(0.5, 0.0, 0.5);
    
    // Apply intensity to color
    vec3 color = intensity * purple;



    // ========================================
    // edge fade out circle a quad 0,0 to 1,1 uv
    //vec3 color = vec3(1.0, 0.2, 0.1);

    // Step 1: Generate fake noise using cos and sin
    noise = 0.0; // reusing var
    noise += 0.5 * sin(x * 3.0 + y * 2.0 + time * 0.5);
    noise += 0.3 * cos(x * 5.0 + y * 4.0 + time * 1.0);
    noise += 0.2 * sin(x * 7.0 + y * 6.0 + time * 1.5);
    noise += 0.1 * cos(x * 9.0 + y * 8.0 + time * 2.0);
    noise = (noise + 1.0) * 0.5; // Scale noise to [0, 1]

    // Step 2: Calculate distance from center for edge fade
    vec2 center = vec2(0.5, 0.5);          // Center of the quad
    float dist = distance(vec2(x,y), center);     // Distance from center
    float edgeFade = smoothstep(0.4, 0.5, dist); // Fade from 0.4 to 0.5

    float t=(color.r+color.g+color.b); // alpha
    // Step 3: Modulate alpha with noise and edge fade
    float alpha = 1.0 - edgeFade;  // Base alpha: 1.0 at center, 0.0 at edges
    alpha *= noise; // Add noise variation
    t *= alpha; // blend edge fade noise with fire noise alpha




    
    // Output final color with full opacity
    //gl_FragColor = vec4(color, 1.0);
    FragColor = vec4(color, t);
    //FragColor = vec4(1.0,0.0,0.0, 1.0);
}
