// Copyright (c) 2022 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#pragma once
#include "display/shader/Shader.hpp"
#include "scene/components/Components.hpp"

namespace Dawn {
  class TestBackgroundShader : public Shader {
    public:
      shaderparameter_t paramProjection;
      shaderparameter_t paramView;
      shaderparameter_t paramTime;

      std::map<shaderparameter_t, enum ShaderParameterType>
      getParameters() override {
        std::map<shaderparameter_t, enum ShaderParameterType> ps;
        ps[paramTime] = SHADER_PARAMETER_TYPE_FLOAT;
        return ps;
      }
      
      void setDefaultParameters(Material &material) override {
        material.floatValues[this->paramTime] = 0.0f;
      }

      void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override {
        this->setMatrix(this->paramProjection, proj);
        this->setMatrix(this->paramView, view);
      }

      void setMeshParameters(glm::mat4 transform) override {}

      void bindTexture(
        shaderparameter_t param,
        Texture *texture
      ) override {}

      void compile() override {
        this->compileShader(R"GLSL(
            #version 330 core
            layout (location = 0) in vec3 aPos;
            layout (location = 1) in vec2 aTexCoord;

            uniform mat4 u_Proj;
            uniform mat4 u_View;

            out vec2 o_TextCoord;
            void main() {
              gl_Position = u_Proj * u_View * vec4(aPos, 1.0);
              o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);
            }
          )GLSL", R"GLSL(
            #version 330 core
            out vec4 o_Color;
            in vec2 o_TextCoord;
            uniform float u_Time;

            void main() {
              highp float tSpan = 4.0;
              highp float halfTSpan = tSpan / 2.0;
              highp float t = mod(u_Time, tSpan);
              if(t > halfTSpan) {
                t = halfTSpan + (halfTSpan - t);
              }
              t = t / halfTSpan;

              highp float x = o_TextCoord.x;
              highp float y = o_TextCoord.y;
              float h = sin(x + u_Time) / 4 + 0.5;

              highp float r = (0.6 + ((y * t) / 5.0));
              highp float g = 0.4 + ((x * t) / (10 + (t * 3)));
              highp float b = 0.8 + ((0.2 * t) - (x / 5));
              highp float global = 0.7 + ((0.1 + (y * 0.6)) / h / 4.0);

              
              o_Color = vec4(
                r * global,
                g * global,
                b * global,
                1.0
              );
            }
          )GLSL"
        );

        this->paramProjection = this->getParameterByName("u_Proj");
        this->paramView = this->getParameterByName("u_View");
        this->paramTime = this->getParameterByName("u_Time");
      }
  };
}