#include "/lib/all_the_libs.glsl"

uniform sampler2D gcolor;


varying vec2 texcoord;

// [DISTORTION] - Keep this matching your shadow.vsh
vec3 Distort(vec3 position) {
    float centerDist = length(position.xy);
    float distortion = centerDist + 0.1;
    return vec3(position.xy / distortion, position.z * 0.5);
}

// [NEW IRIS LOGIC]
vec3 GetShadowColor(vec3 shadowCoord) {
    float pixelSize = 1.0 / 4096.0; 
    float bias = 0.001;
    
    vec3 shadowTotal = vec3(0.0);

    // 3x3 Loop for Softness
    for(int x = -1; x <= 1; x++) {
        for(int y = -1; y <= 1; y++) {
            vec2 offset = vec2(x, y) * pixelSize;
            vec2 sampleCoord = shadowCoord.xy + offset;

            // 1. Check shadowtex0 (Does ANYTHING exist here?)
            // If the depth in the map is closer to the sun than our pixel, something is blocking it.
            float depthAll = texture2D(shadowtex0, sampleCoord).r;
            float isBlockedByAnything = step(depthAll, shadowCoord.z - bias);

            if (isBlockedByAnything == 0.0) {
                // Not blocked by anything -> Full Sun
                shadowTotal += vec3(1.0);
                continue; 
            }

            // 2. Check shadowtex1 (Is it an OPAQUE object?)
            float depthOpaque = texture2D(shadowtex1, sampleCoord).r;
            float isBlockedByOpaque = step(depthOpaque, shadowCoord.z - bias);

            if (isBlockedByOpaque == 1.0) {
                // Blocked by opaque object -> Dark Shadow
                shadowTotal += vec3(0.0);
            } 
            else {
                // 3. Blocked by something, but NOT opaque -> Must be translucent!
                // Sample the color from shadowcolor0
                vec4 tint = texture2D(shadowcolor0, sampleCoord);
                
                // Iris Formula: Color * (1.0 - Alpha) gives the light passing through
                // (You can also just use tint.rgb if you want stronger colors)
                shadowTotal += tint.rgb;    
            }
        }
    }
    
    return shadowTotal / 9.0;
}

void main() {
    vec3 Color = texture2D(gcolor, texcoord).rgb;
    float depth = texture2D(depthtex0, texcoord).r;

    if (depth < 1.0) {
        // --- Coordinate Math (Kept exactly as you had it) ---
        vec4 clipPos = vec4(texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
        vec4 viewPos = gbufferProjectionInverse * clipPos;
        viewPos /= viewPos.w;
        vec4 worldPos = gbufferModelViewInverse * viewPos;

        vec4 shadowPos = shadowModelView * worldPos;
        shadowPos = shadowProjection * shadowPos;
        vec3 distortedPos = Distort(shadowPos.xyz);
        vec3 shadowCoords = distortedPos * 0.5 + 0.5;

        vec3 incomingLight = GetShadowColor(shadowCoords);

    
        
        // As night falls, force the shadow multiplier closer to 1.0 (white/no shadow)
        // This makes shadows appear "lighter" and less harsh.
        // Adjust '0.8' up to make night shadows even fainter.
        incomingLight = mix(incomingLight, vec3(1.0), nightStrength * 0.8);

        // --- END NEW LOGIC ---

        const vec3 SUN_COLOR = vec3(1.6, 1.0, 0.6);
        float What = sunriseStrength + sunsetStrength;
    
   
        vec3 finalColor = SUN_COLOR * (dayStrength + What);
        
        // 2. Cool Shadows: Changed from Grey to Deep Purple-Blue
        // This is the key to the "Summer Evening" childhood memory look.
        // --- DYNAMIC AMBIENT LOGIC ---

        // 1. Define your two ambient states
        vec3 dayShadow = vec3(0.15, 0.15, 0.30); // Deep, dark blue (Current look)
        vec3 nightLight = vec3(0.80, 0.80, 0.95); // Brighter, desaturated blue (Moonlight)

        // 2. Calculate how much "Day" it is
        // (We clamp it to make sure it stays between 0.0 and 1.0)
        float timeMix = clamp(dayStrength + sunriseStrength + sunsetStrength, 0.0, 1.0);

        // 3. Mix them based on time
        // If it's Day (1.0), use dayShadow. If it's Night (0.0), use nightLight.
        vec3 ambientColor = mix(nightLight, dayShadow, timeMix); 

        // Calculate Day/Night strength
        float timeWeight = dayStrength + sunriseStrength + sunsetStrength;
        
        // 3. The Mix:
        // We add the ambient (shadow) color as the base.
        // Then we add the Sun ONLY where the shadow mask (incomingLight) allows.
        vec3 finalSunlight = finalColor * timeWeight * incomingLight;
        
        // Apply lighting to the texture
        Color *= (ambientColor + finalSunlight);
        
        // --- Grading & Exposure ---
        
        // Desaturate slightly to prevent eye strain from the orange
        float gray = dot(Color, vec3(0.299, 0.587, 0.114));
        Color = mix(vec3(gray), Color, 1.15); // Slight vibrance boost, but controlled
        
       
        // Use an exposure variable. 0.6 is roughly dividing by 1.6.
        Color *= 0.6; 
        
        // Warm Tint (Sepia) for that old photo feel
        Color *= vec3(1.05, 1.0, 0.95);
    }

    gl_FragColor = vec4(Color, 1.0);
}