/*
    Copyright (c) 2024 Josh Britain (jbritain)
    Licensed under the MIT license

      _____   __   _
     / ___/  / /  (_)  __ _   __ _  ___   ____
    / (_ /  / /  / /  /  ' \ /  ' \/ -_) / __/
    \___/  /_/  /_/  /_/_/_//_/_/_/\__/ /_/

    By jbritain
    https://jbritain.net

*/

#include "/lib/common.glsl"

#ifdef vsh

out vec2 texcoord;

void main() {
  gl_Position = ftransform();
  texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
}

#endif

// ===========================================================================================

#ifdef fsh
#include "/lib/post/tonemap.glsl"
#include "/lib/post/processing.glsl"
#include "/lib/util/textRenderer.glsl"
#include "/lib/post/bloom.glsl"

in vec2 texcoord;

uniform sampler2D debugtex;

layout(location = 0) out vec4 color;

void main() {
  color = texture(colortex0, texcoord);

  #ifdef BLOOM

  vec3 bloom = color.rgb + upSample(colortex2, scaleFromBloomTile(texcoord, tiles[0])).rgb;

  float rain = texture(colortex5, texcoord).r;
  color.rgb = mix(
      color.rgb,
      bloom,
      clamp01(
        0.01 * BLOOM_STRENGTH + rain * 0.1 + wetness * 0.1 * EBS.y + blindness
      )
    );
  color.rgb *= 1.0 - 0.8 * blindness;
  #endif

  color.rgb *= 1.0 - 0.95 * blindness;

  color.rgb *= 2.0;
  color.rgb = tonemap(color.rgb);

  color = postProcess(color);

  #ifdef OLD_TV_EFFECT
    vec2 uv = texcoord;
    float scanline = sin(uv.y * viewHeight * 1.5) * 0.04;
    color.rgb -= scanline;
    vec2 center = uv - 0.5;
    float vignette = 1.0 - dot(center, center) * 0.8;
    color.rgb *= vignette;
    float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
    color.rgb = mix(vec3(gray), color.rgb, 0.7);
    color.rgb *= vec3(1.05, 1.0, 0.95);
    vec2 border = abs(uv - 0.5) * 2.0;
    float borderRadius = 0.85;
    float corner = length(max(border - borderRadius, 0.0));
    float borderMask = smoothstep(0.0, 0.05, corner);
    float outerBorder = step(0.92, max(border.x, border.y));
    color.rgb *= 1.0 - max(borderMask, outerBorder);
  #endif

  #ifdef LETTER_BOX
    float barSize = 0.12;
    if (texcoord.y < barSize || texcoord.y > 1.0 - barSize) {
      color.rgb = vec3(0.0);
    }
  #endif

  #ifdef DEBUG_ENABLE
  if (hideGUI) {
    color = texture(debugtex, texcoord);
    color = texture(colortex2, texcoord);
  }

  beginText(ivec2(gl_FragCoord.xy / 2.0), ivec2(0, viewHeight / 2.0) + ivec2(8, -8));
  printString((_D, _e, _b, _u, _g, _space, _m, _o, _d, _e, _space, _i, _s, _space, _a, _c, _t, _i, _v, _e));
  printLine();
  printString((_F, _r, _a, _m, _e, _colon, _space));
  printInt(frameCounter);
  printLine();

  if (!hideGUI) {
    printString((_P, _r, _e, _s, _s, _space, _F, _1, _space, _a, _n, _d, _space, _c, _a, _l, _l, _space, _s, _h, _o, _w, _opprn, _clprn));
  }

  endText(color.rgb);

  #endif
}

#endif
