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

out vec4 FragColor;

in vec2 uv;

uniform vec3 data;

void main() {
    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;
   //-- float twinkle = sin(uTime * twinkleSpeed);

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

    float time = phase / (2.0 * 3.14159) * twinkleSpeed;
    ///vec2 flame_uv = uv + vec2(0.0, offset_y);


    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

    // fire effect fake noise
    // Calculate flicker using multiple sin and cos functions
    float flicker = 0.0;
    flicker += 0.5 * sin(x * 2.0 - time * 1.0);
    flicker += 0.3 * cos(y * 4.0 - time * 2.0);
    flicker += 0.2 * sin(x * 8.0 - time * 4.0);
    flicker += 0.1 * cos(y * 16.0 - time * 8.0);

    // Amplitude based on y position
    float amplitude = max(0.0, 1.0 - x); // 2.0
    float total_flicker = amplitude * flicker;

    // Adjusted y position
    float adjusted_x = x + total_flicker;
    float adjusted_y = y + total_flicker - .25;
    //adjusted_y *= adjusted_x;

    //adjusted_y = sin(x * 2 - time) + cos(y * 2 - time);

    // Determine color based on adjusted_y
    vec3 color;
    float t=0;
    if (adjusted_y < 0.0) { // black
        color = vec3(0.0, 0.0, 0.0); // No fire below y=0
    } else if (adjusted_y < 0.5) { // red to yellow
        t = adjusted_y / 0.2;
        color = mix(vec3(1.0, 0.0, 0.0), vec3(1.0, 0.5, 0.0), t);
    } else { // yellow to white
        t = 1.0 - (adjusted_y - 0.5) / 0.5;
        color = mix(vec3(1.0, 0.5, 0.0), vec3(1.0, 1.0, 0.5), t);
    }


    // ========================================
    // 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
    float noise = 0.0;
    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

    // 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



    // Set the fragment color
    //color=vec3(0.0,1.0,0.0); // test green
    FragColor = vec4(color, t);
}
