#define MAX_LIGHTS 8
#version 120

uniform sampler2D decalMap;
uniform sampler2D normalMap;
uniform int lightEnabled[MAX_LIGHTS];

varying vec3 normal;
varying vec3 tangent1;
varying vec3 tangent2;
varying vec4 ecPos;

void main()
{
	vec3 n, halfV;
	float NdotL, NdotHV;
	float att;
	vec3 aux;
	float dist;
	vec3 lightDir;
	float spotEffect;
	vec4 color = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	vec3 surfaceNormal = normalize(normal);
	mat3 tangentBasis = mat3(tangent1[0],tangent1[1],tangent1[2],tangent2[0],tangent2[1],tangent2[2],surfaceNormal[0],surfaceNormal[1],surfaceNormal[2]);

	n = 2.0 * (texture2D(normalMap, gl_TexCoord[1].st).rgb - 0.5);
	n = tangentBasis * n;
	
	//first light0 then others
	
	if(lightEnabled[0] == 1)
	{
	
		aux = vec3(gl_LightSource[0].position - ecPos);
		dist = length(aux);
		lightDir = normalize(aux);
		NdotL = dot(n,lightDir);

		att = 1.0 / (gl_LightSource[0].constantAttenuation +
				gl_LightSource[0].linearAttenuation * dist +
				gl_LightSource[0].quadraticAttenuation * dist * dist);

		color += att * (gl_FrontMaterial.ambient * gl_LightSource[0].ambient);

		if (NdotL > 0.0)
		{
			spotEffect = dot(normalize(gl_LightSource[0].spotDirection),
						normalize(-lightDir));

			if(spotEffect > gl_LightSource[0].spotCosCutoff)
			{
				spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);
				att = spotEffect * att;

				color += att * (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * NdotL);
//				halfV[0] = 0.0;
//				halfV[1] = 0.0;
//				halfV[2] = 1.0;
				halfV = normalize(gl_LightSource[0].spotDirection);
				NdotHV = max(dot(n, halfV), 0.0);
				color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * 
						pow(NdotHV, gl_FrontMaterial.shininess);

				//now draw others
				spotEffect = dot(normalize(gl_LightSource[0].spotDirection),
					normalize(-lightDir));

				spotEffect = 1-min(1.f,(abs(spotEffect-0.99))*2.f);
				spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);

				att = spotEffect / (gl_LightSource[0].constantAttenuation +
					gl_LightSource[0].linearAttenuation * dist +
					gl_LightSource[0].quadraticAttenuation * dist * dist);

				color += att * (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * NdotL);

				color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * 
						pow(NdotHV, gl_FrontMaterial.shininess);

				spotEffect = dot(normalize(gl_LightSource[0].spotDirection),
					normalize(-lightDir));

				spotEffect = 1-min(1.0,abs(spotEffect-0.98));
				spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);

				att = spotEffect / (gl_LightSource[0].constantAttenuation +
					gl_LightSource[0].linearAttenuation * dist +
					gl_LightSource[0].quadraticAttenuation * dist * dist);

				color += att * (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * NdotL);
				color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * 
						pow(NdotHV, gl_FrontMaterial.shininess);
				
				color = vec4(min(1.5f,color[0]),min(1.5f,color[1]),min(1.5f,color[2]),min(1.5f,color[3]));
			}
		}
	}
	
	for(int i=1;i<MAX_LIGHTS;i++)
	{
		if(lightEnabled[i] == 1)
		{
			aux = vec3(gl_LightSource[i].position - ecPos);	
			dist = length(aux);
			lightDir = normalize(aux);
			NdotL = dot(n,lightDir);		

			att = 1.0 / (gl_LightSource[i].constantAttenuation +
					gl_LightSource[i].linearAttenuation * dist +
					gl_LightSource[i].quadraticAttenuation * dist * dist);
		
			color += att * (gl_FrontMaterial.ambient * gl_LightSource[i].ambient);

			if (NdotL > 0.0)
			{			
				color += att * (gl_FrontMaterial.diffuse * gl_LightSource[i].diffuse * NdotL);
				halfV = normalize(gl_LightSource[i].halfVector.xyz);
				NdotHV = max(dot(n, halfV), 0.0);
				color += att * gl_FrontMaterial.specular * gl_LightSource[i].specular * 
								pow(NdotHV, gl_FrontMaterial.shininess);
			}
		}
	}

	if(gl_FrontMaterial.ambient[0] > 1.0)
		color = vec4(gl_FrontMaterial.ambient[0],gl_FrontMaterial.ambient[1],gl_FrontMaterial.ambient[2],color[3]);

	color[3] = gl_LightModel.ambient[3] * gl_FrontMaterial.ambient[3];
	gl_FragColor = color * texture2D(decalMap,gl_TexCoord[0].st);
}