#version 330 core
out vec4 FragColor;


struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct DirectionalLight {
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
struct PointLight {
    bool disabled;

    vec3 position;

    float constant;
    float linear;
    float quadratic;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
struct SpotLight {
    bool disabled;

    vec3 position;
    vec3 direction;
    float cutOff;
    float outerCutOff;

    float constant;
    float linear;
    float quadratic;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

struct Decal {
    sampler2D tex;
    float opacity;
    vec4 uvCoords;
};

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

#define MAX_POINT_LIGHTS 4
#define MAX_SPOT_LIGHTS 4
#define MAX_DECALS 4

// Texture & Decals
uniform Material material;
uniform sampler2D mainTexture;
uniform Decal decals[MAX_DECALS];
uniform int numberOfDecals;

uniform vec3 viewPos;

// Lights
uniform DirectionalLight dirLight;

uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform int numberOfPointLights;

uniform SpotLight spotLights[MAX_SPOT_LIGHTS];
uniform int numberOfSpotLights;

// func declarations
vec3 calcTextures();
vec4 calcDirectLight(DirectionalLight light, vec3 normal, vec3 viewDir, vec3 albedo);
vec4 calcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo);
vec4 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo);

void main()
{
    vec4 result = vec4(0.0f);

    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);

    vec3 albedo = calcTextures();
    albedo = vec3(1,1,1);

    //result += calcDirectLight(dirLight, norm, viewDir, albedo);

    for(int i = 0; i < numberOfPointLights; i++){
        if (pointLights[i].disabled) continue;
        result += calcPointLight(pointLights[i], norm, FragPos, viewDir, albedo);
    }

    for(int i = 0; i < numberOfSpotLights; i++){
        if (spotLights[i].disabled) continue;
        result += calcSpotLight(spotLights[i], norm, FragPos, viewDir, albedo);
    }

    FragColor = result;
}


vec3 calcTextures(){
    vec4 textureAsVec = texture(mainTexture, TexCoords);

    vec4 decalMix;
    for(int i = 0; i < numberOfDecals; i++){
        // We invert the 2nd one to make uv scaling easier
        vec2 decalCoords = TexCoords * (1.0 / decals[i].uvCoords.xy) + decals[i].uvCoords.zw;;

        vec4 decal = texture(decals[i].tex, decalCoords);

        decalMix += mix(decalMix, decal, 0.2f);
    }

    vec4 finalMix = mix(textureAsVec, decalMix, 0.2f);

    return vec3(finalMix);
}

// calculates the color when using a directional light.
vec4 calcDirectLight(DirectionalLight light, vec3 norm, vec3 viewDir, vec3 albedo)
{
    vec3 lightDir = normalize(-light.direction);

    // diffuse shading
    float diff = max(dot(norm, lightDir), 0.0);

    // specular shading
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    // combine results
    vec3 ambient  = light.ambient * albedo;
    vec3 diffuse  = light.diffuse * diff * albedo;
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

    return vec4(ambient + diffuse + specular, 1.0f);
}

vec4 calcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // diffuse shading
    float diff = max(dot(normal, lightDir), 0.0);

    // specular shading
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    // attenuation
    float distance = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

    // combine results
    vec3 ambient = light.ambient * albedo;
    vec3 diffuse = light.diffuse * diff * albedo;
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;

    return vec4(ambient + diffuse + specular, 1.0f);
}

vec4 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // diffuse shading
    float diff = max(dot(normal, lightDir), 0.0);

    // specular shading
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    // attenuation
    float distance = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

    // spotlight intensity
    float theta = dot(lightDir, normalize(-light.direction));
    float epsilon = light.cutOff - light.outerCutOff;
    float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);

    // combine results
    vec3 ambient = light.ambient * albedo;
    vec3 diffuse = light.diffuse * diff * albedo;
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
    ambient *= attenuation * intensity;
    diffuse *= attenuation * intensity;
    specular *= attenuation * intensity;

    return vec4(ambient + diffuse + specular, 1.0f);
}