#version 330

in vec4 vertex_color;
in vec2 texture_coord;
in vec3 normal;
in vec3 frag_pos;

out vec4 color;

struct Directional_Light {
    vec3 color;
    float ambient_intensity;
    vec3 direction;
    float diffuse_intensity;
};

struct Material {
    float shininess;
    float specular_intensity;
};

uniform sampler2D tex;
uniform Directional_Light sun;
uniform Material material;
uniform vec3 eye_position;


void main() {
    vec4 ambient_color = vec4(sun.color, 1.0f) * sun.ambient_intensity;

    float diffuse_factor = max(dot(normalize(normal), normalize(sun.direction)), 0.0f);
    vec4 diffuse_color = vec4(sun.color, 1.0f) * sun.diffuse_intensity * diffuse_factor;

    vec4 specular_color = vec4(0, 0, 0, 0);

    if (diffuse_factor > 0.0f) {
        vec3 frag_to_eye = normalize(eye_position - frag_pos);
        vec3 reflected_vertex = normalize(reflect(sun.direction, normalize(normal)));
        float specular_factor = dot(frag_to_eye, reflected_vertex);

        if (specular_factor > 0) {
            specular_factor = pow(specular_factor, material.shininess);
            specular_color = vec4(sun.color * material.specular_intensity * specular_factor, 1.0f);
        }
    }

    color = texture(tex, texture_coord) * (ambient_color + diffuse_color + specular_color);
}
