#include "/lib/all_the_libs.glsl"

// --- 0. SETTINGS ---
#define BUMP_STRENGTH 2.0 

/* DRAWBUFFERS:089 */

// --- UNIFORMS ---
uniform sampler2D gtexture;

// --- VARYINGS ---
varying vec2 texcoord;
varying vec4 glcolor;
varying vec4 vTangent;
varying vec3 vViewPos;
varying vec2 LightmapCoords;

#include "/global/lighting.fsh"
#include "/lib/distort.glsl"

void main() {
    // 1. SETUP COORDINATES
    vec2 newCoord = texcoord;
    vec3 viewDir = normalize(-vViewPos); 

    // 2. TBN MATRIX
    vec3 normalFace = normalize(Normal);
    vec3 bitangent = normalize(cross(vTangent.xyz, normalFace) * vTangent.w);
    mat3 TBN = mat3(vTangent.xyz, bitangent, normalFace);

    // 3. READ TEXTURES
    vec4 color = texture2D(gtexture, newCoord) * glcolor;
    color.rgb = to_linear(color.rgb);
    vec3 albedo = color.rgb;

    // 4. NORMAL MAPPING
    vec3 normalMap = texture2D(normals, newCoord).xyz;
    normalMap = normalMap * 2.0 - 1.0; 
    normalMap.xy *= BUMP_STRENGTH; 

    float zSq = 1.0 - dot(normalMap.xy, normalMap.xy);
    if (zSq < 0.0) normalMap = normalize(vec3(normalMap.xy, 0.0)); 
    else normalMap.z = sqrt(zSq);
    
    vec3 finalNormal = normalize(TBN * normalMap);

    // 5. SPECULAR (Read initial data)
    vec4 specData = texture2D(specular, newCoord);
    float smoothness = specData.r;
    float f0 = specData.g;
    bool isMetal = f0 >= 0.9;

    // --- 4.5 WETNESS LOGIC (RAIN) ---
    // Only apply if it is raining and the surface is facing UP
    if (wetness > 0.01) {
        // Dot product of (0,1,0) and Normal checks if surface is flat
        float upAmount = max(0.0, dot(normalFace, vec3(0.0, 1.0, 0.0)));
        
        // Calculate a "wet factor" based on rain intensity and angle
        float wetFactor = wetness * upAmount ;

        // 1. Darken the surface (Wet things look darker)
        albedo *= (1.0 - wetFactor * 0.4);

        // 2. Increase Smoothness (Water is glossy)
        // Mix current smoothness towards 0.95 based on how wet it is
        smoothness = mix(smoothness, 0.95, wetFactor * 0.8);

        // 3. Add weak base reflection to non-metals (Water layer)
        if (!isMetal) {
             f0 = mix(f0, 0.4, wetFactor); // 0.04 is standard water reflectance
        }
    }
    // --------------------------------

    // 6. LIGHTING
    vec3 lightDir = normalize(sunPosition); 
    if (length(sunPosition) < 0.1) lightDir = normalize(vec3(0.2, 1.0, 0.2));
    vec3 reflectDir = reflect(normalize(vViewPos), finalNormal); 

    float NdotL = max(0.0, dot(finalNormal, lightDir));
    float NdotH = max(0.0, dot(reflectDir, lightDir));
    
    // Smoothness is now modified by rain!
    float specularStrength = pow(NdotH, 10.0 + (100.0 * smoothness)); 
    specularStrength *= smoothness * f0 * 5.0; 

    // 7. COMBINE
    vec3 blockLight = tweak_lightmap(); 
    vec3 diffuse = isMetal ? color.rgb * 0.1 : albedo * blockLight;
    vec3 specularCol = (isMetal ? albedo : vec3(1.0)) * specularStrength;

    color.rgb = diffuse + specularCol;

    // Output (Fixed to match your top declaration)

    gl_FragData[1] = vec4(smoothness, f0, 0.0, 1.0);

    gl_FragData[2] = vec4(finalNormal * 0.5 + 0.5, 1.0);

    gl_FragData[0]=color;

   
    
}