#version 330 core

struct Material {
	sampler2D diffuse0;
	sampler2D diffuse1;
	sampler2D diffuse2;
	sampler2D diffuse3;
	sampler2D specular0;
	sampler2D specular1;
	sampler2D specular2;
	sampler2D specular3;
	sampler2D ambient0;
	sampler2D ambient1;
	bool use_diff_color;
	vec3 diff_color;
	bool use_spec_color;
	vec3 spec_color;
	int use_amb_color;
	vec3 amb_color;
};

out vec4 fragmentColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoord;
in vec3 ViewPos;
uniform Material material;

void main()
{
	const vec3 light_direction = vec3(-1.0, -1.0, -1.0);
	const vec3 light_color = vec3(1.0, 1.0, 1.0);
	const float light_amb = 0.1;
	const float light_spec = 0.4;
	const float light_diff = 0.7;

	vec3 diffuseTexCol;
	if (material.use_diff_color) {
		diffuseTexCol = material.diff_color;
	} else {
		diffuseTexCol = vec3(texture(material.diffuse0, TexCoord));
	}

	vec3 specularTexCol;
	if (material.use_spec_color) {
		specularTexCol = material.spec_color;
	} else {
		specularTexCol = vec3(texture(material.specular0, TexCoord));
	}

	vec3 ambTexCol;
	if (material.use_amb_color == 0) {
		ambTexCol = vec3(texture(material.ambient0, TexCoord));
	} else if (material.use_amb_color == 1) {
		ambTexCol = diffuseTexCol * 0.5;
	} else if (material.use_amb_color == 2) {
		ambTexCol = material.amb_color;
	}

	vec3 normal_n = normalize(Normal);
	vec3 light_dir_n = normalize(light_direction);

	vec3 view_dir = normalize(ViewPos - FragPos);
	vec3 reflect_dir = reflect(light_dir_n, normal_n);
	float shine = pow(max(dot(view_dir, reflect_dir), 0.0), 35);

	vec3 ambient_light = light_amb * diffuseTexCol * light_color;
	vec3 diffuse_light = max(dot(normal_n, -light_dir_n), 0.0) *light_diff * diffuseTexCol * light_color;
	vec3 specular_light = shine * light_spec * specularTexCol * light_color;

	vec3 color = ambient_light + diffuse_light + specular_light;

	fragmentColor = vec4(color, 1.0);
}
