uniform lowp sampler2D g_Texture;
uniform lowp vec4 g_Color;

uniform lowp float blurSize;
uniform lowp int invert;

varying highp vec2 texCoord;
varying lowp vec2 position;

#define _lightCount 16
uniform lowp vec4 lightPos[_lightCount];
uniform lowp int lightActive;

#define _screenSize 0.0015
#define _screenSize2 0.003

#define _red 0.299
#define _green 0.587
#define _blue 0.114

void main()
{
	lowp vec4 colorNew = texture2D(g_Texture, texCoord) ;
	lowp vec4 colorOld = vec4(colorNew) ;
	
	if (blurSize != 0){
		colorNew = texture2D(g_Texture, texCoord) * (1 - (blurSize* 4.25));
		colorNew += texture2D(g_Texture, texCoord + vec2(_screenSize,0.0)) * blurSize;
		colorNew += texture2D(g_Texture, texCoord - vec2(_screenSize ,0.0)) * blurSize;
		colorNew += texture2D(g_Texture, texCoord + vec2(0.0,_screenSize2 )) * blurSize;
		colorNew += texture2D(g_Texture, texCoord - vec2(0.0,_screenSize2 )) * blurSize;
		colorNew.a = 1;
	}
	
	if (lightActive == 1){
		lowp vec4 colorDarken = vec4(0.0 , 0.0 , 0.0 , 1.0 );
		
		lowp vec2 xyNew = texCoord.xy;
		xyNew.y *= 0.52;
		xyNew = floor(xyNew * 400) / 400;
		
		for (int i=0; i <_lightCount; i++){
			if (lightPos[i].z != 0){
				lowp float dist = distance(xyNew.xy , lightPos[i].xy);
				if(dist < lightPos[i].z){
					colorDarken = colorNew;
					break;
				}else if((dist > lightPos[i].z) && (dist < lightPos[i].z + 0.01)){
					colorDarken = vec4(colorNew.rgb * 0.5, 0.5);
					break;
					
				}
			}
		}
		colorNew = colorDarken;
	}else if (lightActive == 2){
		lowp vec2 xyNew = texCoord.xy;
		xyNew.y *= 0.52;
		xyNew = floor(xyNew * 400) / 400;
		
		int graying = 1;
		
		for (int i=0; i <_lightCount; i++){
			if (lightPos[i].z != 0){
				lowp float dist = distance(xyNew.xy , lightPos[i].xy);
				if (lightPos[i].z < 0)
				{
					if(dist < -lightPos[i].z){
						graying = 5;
						break;
					}else if((dist > -lightPos[i].z) && (dist < (-lightPos[i].z) + 0.01)){
						if (graying == 2) {
							graying = 3;
						}else{
							graying = 5;
						}
					}
					
				}else{
					if(dist < lightPos[i].z){
						if (graying == 5) {
							graying = 3;
							break;
						}else{
							graying = 2;
						}
					}else if((dist > lightPos[i].z) && (dist < lightPos[i].z + 0.01)){
						graying = 3;
						
					}
				}
			}else{
				break;
			}
		}
		
		if ((graying == 1) || (graying == 5)){
			float gray = dot(colorNew.rgb, vec3(_red , _green , _blue));
			if ((colorNew.r < 0.2) && (colorNew.g < 0.5)){gray *= 0.2;};
			colorNew = vec4(gray, gray, gray,1);
			
		}else if (graying == 3){
			float gray = dot(colorNew.rgb, vec3(_red, _green, _blue))  ;
			if ((colorNew.r < 0.2) && (colorNew.g < 0.5)){gray *= 0.2;};
			colorNew.rgb = (vec3(gray , gray , gray ) + colorNew.rgb) * 0.5;
		}
		
	}
	
	if (invert != 0){
		colorNew = vec4(colorNew.g + 0.1 ,colorNew.b,colorNew.r + 0.1 ,colorNew.a);
	}
	
		lowp float changer = 1.2 + (1 - texCoord.y) * 0.2;
		lowp float pp = sqrt(colorNew.r * colorNew.r * _red + colorNew.g * colorNew.g * _green + colorNew.b * colorNew.b * _blue);
		colorNew.rgb =  vec3(pp + (colorNew.r - pp) * changer ,pp + (colorNew.g - pp) * changer ,pp + (colorNew.b - pp) * changer);
	
	gl_FragColor = colorNew;
}
