#extension GL_EXT_gpu_shader4 : enable

// STRUCTURES
struct Light
{
	vec2 Pos;
	vec3 Colour;
	float Reach;
	float Brightness;
	bool Flicker;


	
};

// VARIABLES
uniform Light LightSources[100];
uniform int AmountLightSources;
uniform float time;
uniform sampler2D tex;
uniform float SunBrightness;
uniform int TexLayer;


// FUNCTIONS
int Seed;
void GenSeed() { Seed = (214013*Seed+2531011); }
void SetSeed(int pSeed) {Seed = pSeed;}
float GetRand() {GenSeed(); return ((Seed >> 16) & 0x7FFF);}
float GetRandf(float range) {return (float(GetRand()) / float(0x7FFF) * range);}

float GetFlicker() {return sin(50.0/time)*(GetRandf(1000.5)-GetRandf(2000.0))*12.0;}





void main(void)
{

	// SetSeed(int(time));
	
	float AmbientBrightness = SunBrightness*1.3; //sin(time*0.0005);
	if(AmbientBrightness <= 0.03)
		AmbientBrightness = 0.03;
	
	//Light PlayerLight;
	//PlayerLight.Pos = vec2(1280*0.525,720*0.45);

	
	vec4 lightColor = {0.0,0.0,0.0,0.0};
	vec4 tempColour = {0.0,0.0,0.0,0.0};
	vec4 TorchColour = {0.7,0.0,0.0,0.0};
	
	 if(AmbientBrightness >= 0.48 && AmbientBrightness <= 0.9)
	{
		lightColor.r = smoothstep(0.9,0.48,AmbientBrightness);
		lightColor.g = smoothstep(0.9,0.48,AmbientBrightness)*0.2;
		lightColor.b = smoothstep(0.9,0.48,AmbientBrightness)*0.1;
	}
	else if(AmbientBrightness >= 0.1 && AmbientBrightness < 0.48)
	{
		lightColor.r = smoothstep(0.1,0.48,AmbientBrightness);
		lightColor.g = smoothstep(0.1,0.48,AmbientBrightness)*0.2;
		lightColor.b = smoothstep(0.1,0.48,AmbientBrightness)*0.1;
	}
		
	if(lightColor.r >= 0.6)
		lightColor.r = 0.6;
	
	

	
	float maxRatio = 1.1;
	
	vec2 pos = gl_FragCoord.xy;
	
	// float dist = distance(pos,vec2(1280*0.525,720*0.45));
	// float dist2 = distance(pos,vec2(1280*0.475,720*0.45));
	
	// float ratio = ((20.0/dist)+(20.0/dist2));
	
	// Flicker Function
	// dist += sin(50.0/time)*(GetRandf(1000.5)-GetRandf(2000.0))*12.0;
	float reach = 192;
	
	float ratio = AmbientBrightness; //0.03;
	int ActualLights = 0;
	if(AmountLightSources > 0)
	{
		for(int i = 0; i < AmountLightSources; i++)
		{
			if(distance(pos,LightSources[i].Pos) <= reach)
			{
				ratio += smoothstep(reach,-100.0,distance(pos,LightSources[i].Pos));
				tempColour += TorchColour*smoothstep(190.0,-10.0,distance(pos,LightSources[i].Pos))*0.6;
				ActualLights++;
		
			}
		}
		
		if(ActualLights > 0)
		{
			// tempColour /= ActualLights;
			lightColor += tempColour;
		}
	}
	

	//if(distance(pos,PlayerLight.Pos) <= 128)
	//{
	//	ratio += smoothstep(100.0,-100.0,distance(pos,PlayerLight.Pos))*0.3;
	//}
	
	// float ratio = 30.0/dist;
	
	
	if(ratio > maxRatio)
		ratio = maxRatio;
		
	
	
		
	
	
	
	vec4 color = texture2D(tex, gl_TexCoord[0].xy);
	
	
	color = mix(color,lightColor,0.2)*ratio;
	
	if(gl_Color.r != 1.0)
		color.r = gl_Color.r;
	if(gl_Color.g != 1.0)
		color.g = gl_Color.g;
	if(gl_Color.b != 1.0)
		color.b = gl_Color.b;
	
	
	color.a = texture2D(tex, gl_TexCoord[0].xy).a;
	if(gl_Color.a != 1.0 && color.a > 0.0)
	{
		color.a = gl_Color.a;
	}
	
	
	
   gl_FragColor = color;
}