#version 330 core
in vec2 vUV;
out vec4 FragColor;
uniform float uTime;
uniform vec3 uResolution;
uniform sampler2D uBackground;
uniform float uAlpha;

// Basic colors
const vec4 BRIGHT_RED = vec4(1.0, 0.0, 0.0, 1.0);
const vec4 BRIGHT_YELLOW = vec4(1.0, 1.0, 0.0, 1.0);
const vec4 BRIGHT_GREEN = vec4(0.0, 1.0, 0.0, 1.0);
const vec4 BRIGHT_CYAN = vec4(0.0, 1.0, 1.0, 1.0);
const vec4 BRIGHT_BLUE = vec4(0.0, 0.0, 1.0, 1.0);
const vec4 BRIGHT_PURPLE = vec4(1.0, 0.0, 1.0, 1.0);
const vec4 RED = vec4(0.8, 0.0, 0.0, 1.0);
const vec4 YELLOW = vec4(0.8, 0.8, 0.0, 1.0);
const vec4 GREEN = vec4(0.0, 0.8, 0.0, 1.0);
const vec4 CYAN = vec4(0.0, 0.8, 0.8, 1.0);
const vec4 BLUE = vec4(0.0, 0.0, 0.8, 1.0);
const vec4 PURPLE = vec4(0.8, 0.0, 0.8, 1.0);
const vec4 DARK_RED = vec4(0.4, 0.0, 0.0, 1.0);
const vec4 DARK_YELLOW = vec4(0.4, 0.4, 0.0, 1.0);
const vec4 DARK_GREEN = vec4(0.0, 0.4, 0.0, 1.0);
const vec4 DARK_CYAN = vec4(0.0, 0.4, 0.4, 1.0);
const vec4 DARK_BLUE = vec4(0.0, 0.0, 0.4, 1.0);
const vec4 DARK_PURPLE = vec4(0.4, 0.0, 0.4, 1.0);
const vec4 WHITE = vec4(1.0, 1.0, 1.0, 1.0);
const vec4 BRIGHT_GRAY = vec4(0.8, 0.8, 0.8, 1.0);
const vec4 DARK_GRAY = vec4(0.4, 0.4, 0.4, 1.0);
const vec4 TRANSPARENT = vec4(0.0, 0.0, 0.0, 0.0);

const float FREQUENCY_MULTIPLIER = 5.0;
const float AMPLITUDE_MULTIPLIER = 0.25;
const float BASE_POSITION = 0.5;
const float HEIGHT = 0.0500;

vec4 copperbar(in vec2 uv,
               in float base, in float offset, in float frequency, in float amplitude,
               in vec4 innerColor, in vec4 outerColor) {
    float alpha = (uTime + offset) * frequency;
    float position = base + (sin(alpha) * amplitude);
    float ratio = abs(uv.y - position) / HEIGHT;
    if (ratio > 1.0) {
        return TRANSPARENT; // outside bar -> transparent
    }
    vec4 col = mix(innerColor, outerColor, ratio);
    col.a = 1.0;
    return col;
}

vec4 choose(in vec4 colors[5]) {
    for (int i = 0; i < 5; ++i) {
        vec4 color = colors[i];
        if (color.a > 0.001) {
            return color;
        }
    }
    return TRANSPARENT;
}

void main()
{
    vec2 fragCoord = vUV * uResolution.xy;
    vec2 uv = fragCoord / uResolution.xy;

    vec4 bar = choose(vec4[5](
        copperbar(uv, BASE_POSITION + 0.0, 0.00, FREQUENCY_MULTIPLIER * 1.0, AMPLITUDE_MULTIPLIER * 1.0, BRIGHT_CYAN,    DARK_BLUE),
        copperbar(uv, BASE_POSITION + 0.0, 0.10, FREQUENCY_MULTIPLIER * 1.0, AMPLITUDE_MULTIPLIER * 1.0, BRIGHT_YELLOW, DARK_RED),
        copperbar(uv, BASE_POSITION + 0.0, 0.20, FREQUENCY_MULTIPLIER * 1.0, AMPLITUDE_MULTIPLIER * 1.0, BRIGHT_YELLOW, DARK_GREEN),
        copperbar(uv, BASE_POSITION + 0.0, 0.30, FREQUENCY_MULTIPLIER * 1.0, AMPLITUDE_MULTIPLIER * 1.0, PURPLE, DARK_YELLOW),
        copperbar(uv, BASE_POSITION + 0.0, 0.40, FREQUENCY_MULTIPLIER * 1.0, AMPLITUDE_MULTIPLIER * 1.0, BRIGHT_GRAY, BLUE)
    ));

    vec4 bg = texture(uBackground, vUV);
    // composite: bar over bg using global uAlpha
    float a = bar.a * uAlpha;
    FragColor = mix(bg, vec4(bar.rgb, 1.0), a);
}
