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

out vec4 FragColor;

//in vec3 FragPos;
in vec3 ourNormal;
in vec2 TexCoord;
//in float time;
//in float height;
//in float scale;
uniform vec3 data; // time offset scale

uniform int viewport_width;
uniform int viewport_height;

//uniform sampler2D texture1;
//uniform vec3 sunPos;
//uniform vec3 moonPos;

// Simple 2D noise function
float noise3(vec2 p) {
    // A basic pseudo-random function (not true Perlin noise, but sufficient for perturbation)
    return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}

// Smooth noise by interpolation (optional enhancement)
float smoothNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f); // Smoothstep
    float a = noise3(i);
    float b = noise3(i + vec2(1.0, 0.0));
    float c = noise3(i + vec2(0.0, 1.0));
    float d = noise3(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}


void main() {
    //vec2 uv = gl_FragCoord.xy / vec2(viewport_width,viewport_height);

    //height = scale + sin(TexCoord.x * 1.0) * 0.5;

    //vec3 resultColor = vec3(1.0, 1.0, 1.0);
    
    // Calculate UV coordinates based on position
    //vec2 uv = TexCoord.xy / (1000.0 * min(1.0, 1.0)); // max w/h view
    //vec2 uv = TexCoord.xy * 25.0; // max w/h view

    //float inv=1.0-(height * .1); 

    //vec3 pos = position;
    // Calculate relative position to origin
    //float wlev=data.y-1.1;
    float wlev=0.0;

   float phase=data.x;
    float scale=128.0;




//-- // 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 / 1;
   //-- 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);






    //ivec2 iTexCoord = ivec2(TexCoord.x, TexCoord.y);
    vec2 iTexCoord = vec2(TexCoord.x, TexCoord.y) * scale;
    //iTexCoord -= scale/2;
    vec2 vertexPosition = iTexCoord * (scale + sin(iTexCoord.x * 1.0) * 0.5);
    vec3 sPos = vec3(0.0, wlev, 0.0);
    sPos.x = vertexPosition.x;
    sPos.y = vertexPosition.y; // Assuming z is your second axis
    //sPos.y += sin(iTexCoord.x * 50.0 + time) * 0.2; // Example height with animation



    ////vec2 vertexPosition = iTexCoord * (scale + sin(iTexCoord.x * 1.0) * 0.5);
    ////vec3 pos = vPos;
    ////pos.x = vertexPosition.x;
    ////pos.z = vertexPosition.y; // Assuming z is your second axis

    ////// Compute height (modify this to match your exact logic)
    ////height = sin(iTexCoord.x * 1.0) * 0.5; // Example height, adjust as needed


    // Wave parameters: directions, frequencies, speeds, amplitudes
    vec2 dirs[4] = vec2[](
        vec2(1.0, 0.0),              // Horizontal waves
        vec2(0.0, 1.0),              // Vertical waves
        normalize(vec2(1.0, 1.0)),   // Diagonal waves (45°)
        normalize(vec2(-1.0, 1.0))   // Diagonal waves (135°)
    );
    float freqs[4] = float[](2.0, 2.0, 3.0, 3.0);    // Frequencies (waves per unit)
    float speeds[4] = float[](1.0, 1.2, 1.5, 1.3);   // Speeds (phase shift per second)
    float amps[4] = float[](0.2, 0.2, 0.1, 0.1);     // Amplitudes (wave height)

    // Compute base wave height with cross waves
    float height = 0.0;
    for (int i = 0; i < 4; i++) {
        float phase = dot(dirs[i], iTexCoord) * freqs[i] + time * speeds[i];
        height += amps[i] * sin(phase);
    }

    // Add noise to perturb the UV coordinates for irregularity
    float noiseValue = smoothNoise(iTexCoord + vec2(time * 0.1, time * 0.05));
    vec2 uvPerturbed = iTexCoord + vec2(noiseValue * 0.1, noiseValue * 0.1);

    // Recompute height with perturbed UVs for more natural waves
    height = 0.0;
    for (int i = 0; i < 4; i++) {
        float phase = dot(dirs[i], uvPerturbed) * freqs[i] + time * speeds[i];
        height += amps[i] * sin(phase);
    }

    // Optional: Add small-scale noise directly to height
    height += 0.05 * smoothNoise(iTexCoord * 5.0 + vec2(time * 0.2));

    // Displace the vertex along the y-axis (assuming y-up)
    sPos.z += height;
    //pos.y += height;

    // Compute approximate normal using finite differences
    //float eps = 0.01;
    //float h1 = 0.0, h2 = 0.0;
    //for (int i = 0; i < 4; i++) {
    //    h1 += amps[i] * sin(dot(dirs[i], uvPerturbed + vec2(eps, 0.0)) * freqs[i] + time * speeds[i]);
    //    h2 += amps[i] * sin(dot(dirs[i], uvPerturbed + vec2(0.0, eps)) * freqs[i] + time * speeds[i]);
    //}
    //vec3 dx = vec3(eps, h1 - height, 0.0);
    //vec3 dz = vec3(0.0, h2 - height, eps);
    //float aNormal = normalize(cross(dz, dx));


    FragColor = vec4(height, 0.0, 0.0, 1.0);
}
