export const screenVertexShader = `
varying vec2 vUv;

void main() {
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;

export const screenFragmentShader = `
varying vec2 vUv;
uniform sampler2D videoTexture;
uniform float u_effect_type;     // 0: none, 1: warmup, 2: powerdown
uniform float u_effect_strength; // 0.0 to 1.0
uniform float u_time;

// 2D Random function
float random (vec2 st) {
    return fract(sin(dot(st.xy,
                         vec2(12.9898,78.233)))*
        43758.5453123);
}

// 2D Noise function
float noise (vec2 st) {
    vec2 i = floor(st);
    vec2 f = fract(st);

    float a = random(i);
    float b = random(i + vec2(1.0, 0.0));
    float c = random(i + vec2(0.0, 1.0));
    float d = random(i + vec2(1.0, 1.0));

    vec2 u = f*f*(3.0-2.0*f);
    return mix(a, b, u.x) +
            (c - a)* u.y * (1.0 - u.x) +
            (d - b) * u.x * u.y;
} 

void main() {
    vec4 finalColor;

    // Shimmering edge effect - ALWAYS ON
    vec4 videoColor = texture2D(videoTexture, vUv);

    // Shimmering edge effect
    float dist = distance(vUv, vec2(0.5));
    float shimmer = noise(vUv * 20.0 + vec2(u_time * 2.0, 0.0));
    float edgeFactor = smoothstep(0.3, 0.5, dist);
    
    vec3 shimmerColor = vec3(0.7, 0.8, 1.0) * shimmer * edgeFactor * 0.5;

    vec4 baseColor = vec4(videoColor.rgb + shimmerColor, videoColor.a);

    if (u_effect_type < 0.9) {
        //  normal video
        finalColor = baseColor;
    } else if (u_effect_type < 1.9) { // "Summon Vision" (Warm-up) effect
        // This is now a multi-stage effect controlled by u_effect_strength (0.0 -> 1.0)
        float noiseVal = noise(vUv * 50.0 + vec2(0.0, u_time * -125.0));
        vec3 mistColor = vec3(0.8, 0.7, 1.0) * noiseVal;
        vec4 videoColor = texture2D(videoTexture, vUv);

        // Stage 1: Fade in the mist (u_effect_strength: 0.0 -> 0.5)
        // The overall opacity of the surface fades from 0 to 1.
        float fadeInOpacity = smoothstep(0.0, 0.5, u_effect_strength);

        // Stage 2: Fade out the mist to reveal the video (u_effect_strength: 0.5 -> 1.0)
        // The mix factor between mist and video goes from 0 (all mist) to 1 (all video).
        float revealMix = smoothstep(0.5, 1.0, u_effect_strength);

        vec3 mixedColor = mix(mistColor, baseColor.rgb, revealMix);
        finalColor = vec4(mixedColor, fadeInOpacity);

    } else { // "Vision Fades" (Power-down) effect
        // Multi-stage effect: Last frame -> fade to mist -> fade to transparent

        float noiseVal = noise(vUv * 50.0 + vec2(0.0, u_time * 123.0));
        vec3 mistColor = vec3(0.8, 0.7, 1.0) * noiseVal;
        vec4 videoColor = texture2D(videoTexture, vUv);

        // Stage 1: Fade in the mist over the last frame (u_effect_strength: 0.0 -> 0.5)
        float mistMix = smoothstep(0.0, 0.5, u_effect_strength);
        vec3 mixedColor = mix(baseColor.rgb, mistColor, mistMix);

        // Stage 2: Fade out the entire surface to transparent (u_effect_strength: 0.5 -> 1.0)
        float fadeOutOpacity = smoothstep(1.0, 0.5, u_effect_strength);

        finalColor = vec4(mixedColor, fadeOutOpacity);
    }

    gl_FragColor = finalColor;
}
`;
