#version 330 core

in vec2 vUV;
out vec4 FragColor;

uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;
uniform float uSpeed;

#define PI 3.14159265359

float seg0(int d){ if(d==0)return 1.; if(d==1)return 0.; if(d==2)return 1.; if(d==3)return 1.; if(d==4)return 0.; if(d==5)return 1.; if(d==6)return 1.; if(d==7)return 1.; if(d==8)return 1.; return 1.; }
float seg1(int d){ if(d==0)return 1.; if(d==1)return 0.; if(d==2)return 0.; if(d==3)return 0.; if(d==4)return 1.; if(d==5)return 1.; if(d==6)return 1.; if(d==7)return 0.; if(d==8)return 1.; return 1.; }
float seg2(int d){ if(d==0)return 1.; if(d==1)return 1.; if(d==2)return 1.; if(d==3)return 1.; if(d==4)return 1.; if(d==5)return 0.; if(d==6)return 0.; if(d==7)return 1.; if(d==8)return 1.; return 1.; }
float seg3(int d){ if(d==0)return 0.; if(d==1)return 0.; if(d==2)return 1.; if(d==3)return 1.; if(d==4)return 1.; if(d==5)return 1.; if(d==6)return 1.; if(d==7)return 0.; if(d==8)return 1.; return 1.; }
float seg4(int d){ if(d==0)return 1.; if(d==1)return 0.; if(d==2)return 1.; if(d==3)return 0.; if(d==4)return 0.; if(d==5)return 0.; if(d==6)return 1.; if(d==7)return 0.; if(d==8)return 1.; return 0.; }
float seg5(int d){ if(d==0)return 1.; if(d==1)return 1.; if(d==2)return 0.; if(d==3)return 1.; if(d==4)return 1.; if(d==5)return 1.; if(d==6)return 1.; if(d==7)return 1.; if(d==8)return 1.; return 1.; }
float seg6(int d){ if(d==0)return 1.; if(d==1)return 0.; if(d==2)return 1.; if(d==3)return 1.; if(d==4)return 0.; if(d==5)return 1.; if(d==6)return 1.; if(d==7)return 0.; if(d==8)return 1.; return 1.; }

float SDF_plane(vec2 p, vec2 c, vec2 n){
    return dot(p-c, n);
}

vec2 rot(vec2 p, float a){
    float c = cos(a), s = sin(a);
    return mat2(c,-s,s,c)*p;
}

float DE_seg(vec2 p, vec2 a, float r){
    p -= a;
    p = rot(p, r * 1.57079632679);

    float segw = .12;
    float segt = -.02;

    float d = -1e6;

    vec2 n1 = normalize(vec2(-1., -1.));
    vec2 n2 = normalize(vec2(-1.,  1.));
    vec2 n3 = vec2(0., -1.);
    vec2 n4 = vec2(0.,  1.);
    vec2 n5 = normalize(vec2( 1., -1.));
    vec2 n6 = normalize(vec2( 1.,  1.));

    d = max(d, SDF_plane(p, vec2(0.), n1) - segt);
    d = max(d, SDF_plane(p, vec2(0.), n2) - segt);
    d = max(d, SDF_plane(p, vec2(0.,-segw), n3) - segt);
    d = max(d, SDF_plane(p, vec2(0., segw), n4) - segt);
    d = max(d, SDF_plane(p, vec2(1.,0.), n5) - segt);
    d = max(d, SDF_plane(p, vec2(1.,0.), n6) - segt);

    return d;
}

float DE_7seg(vec2 p, float idf){

    int id = int(clamp(floor(idf + 0.5), 0., 9.));

    float d = 1e6;

    d = min(d, DE_seg(p, vec2(0.,0.), 0.) + (1.-seg0(id))*1e5);
    d = min(d, DE_seg(p, vec2(0.,0.), 1.) + (1.-seg1(id))*1e5);
    d = min(d, DE_seg(p, vec2(1.,0.), 1.) + (1.-seg2(id))*1e5);
    d = min(d, DE_seg(p, vec2(0.,1.), 0.) + (1.-seg3(id))*1e5);
    d = min(d, DE_seg(p, vec2(0.,1.), 1.) + (1.-seg4(id))*1e5);
    d = min(d, DE_seg(p, vec2(1.,1.), 1.) + (1.-seg5(id))*1e5);
    d = min(d, DE_seg(p, vec2(0.,2.), 0.) + (1.-seg6(id))*1e5);

    return d;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    vec2 p = (fragCoord - 0.5*iResolution.xy) / iResolution.y;

    p *= 5.;
    p.y = -p.y;
    p.x -= .5;
    p.y += 1.;

    float startHour   = 22.;
    float startMinute = 0.;

    float totalMinutes = startHour*60. + startMinute + iTime * uSpeed;

    float minute = floor(mod(totalMinutes, 60.));
    float hour   = floor(mod(totalMinutes/60., 24.));

    float d = 1e9;

    d = min(d, DE_7seg(p-vec2( 2.,0.), mod(minute,10.)));
    d = min(d, DE_7seg(p-vec2( 0.,0.), floor(minute/10.)));
    d = min(d, DE_7seg(p-vec2(-2.,0.), mod(hour,10.)));
    d = min(d, DE_7seg(p-vec2(-4.,0.), floor(hour/10.)));

    d = min(d, length(p-vec2(-.5,0.5))-.05);
    d = min(d, length(p-vec2(-.5,1.5))-.05);

    float mask = step(0., d);

    float glow = exp(-d * 6.);

    vec3 onColor  = vec3(0.3, 1.0, 0.4);
    vec3 offColor = vec3(0.0, 0.05, 0.0);

    vec3 col = mix(offColor, onColor, glow);

    col = mix(col, vec3(0.8, 1.0, 0.8), step(d, 0.0));
    //fragColor = vec4(col,1.0);
    // Make dark/off areas transparent by setting alpha based on luminance
    float lum = dot(col, vec3(0.299, 0.587, 0.114));
    float alphaBase = smoothstep(0.02, 0.20, lum); // low luminance -> near 0 alpha
    fragColor = vec4(col, alphaBase);
}

void main()
{
    vec2 fragCoord = vUV * iResolution.xy;
    vec4 outCol;
    mainImage(outCol, fragCoord);
    outCol.a *= uAlpha;
    FragColor = outCol;
}
