uniform vec4 grad_color;
uniform vec2 grad_pos;
uniform vec2 tex_size;

uniform float gradient_r;
uniform float gradient_r_x;

uniform int poly_size;
uniform float poly_x[128];
uniform float poly_y[128];

uniform int BTN_GRADIENT;

uniform sampler2D u_texture;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;

const float PI = 3.14159;

float distSqr(vec2 p0, vec2 p1)
{
	return (p1.x - p0.x)*(p1.x - p0.x) + (p1.y - p0.y)*(p1.y - p0.y);
}

float distToLine(vec2 p0, vec2 p1, vec2 p)
{
	float l2 = distSqr(p0,p1); 

	if (l2 == 0.0) return distance(p0, p); 

	float t = dot(p - p0, (p1 - p0) / l2);

	if (t < 0.0) return distance(p, p0);  

	else if (t > 1.0) return distance(p, p1); 

	vec2 projection = p0 + (p1 - p0)*t;

	//return projection;
	return distance(p, projection);
}

float flatSinWave(float R, float t)
{
	//Distance to edge
	float d_left = t;
	float d_right = 1.0 - t;

	float min_d = min(d_left,d_right);

	float ratio = min_d/R;
	if(ratio > 1.0) ratio = 1.0;

	return sin(PI * 0.5 * ratio);
}

void main()
{
	vec4 color = texture2D(u_texture, v_texCoord);

	if(color.a > 0)
	{	
			//Calculate the gradient
			//vec2 p = vec2(grad_pos.x/tex_size.x, grad_pos.y/tex_size.y);
			//float dist = distance(vec2(grad_pos.x,grad_pos.y),v_texCoord);//distance(vec2(0,v_texCoord.y),vec2(0,p.y));
			
			float dist = 0;
			float dist_top = 0;
			//vec2 p = v_texCoord * tex_size;
			//float dist = distance(p, grad_pos*tex_size);
/*			if(grad_pos.x > 0)
			{
				dist = distance(vec2(v_texCoord.x*tex_size.x,0),vec2(grad_pos.x * tex_size.x,0));

			}*/
			//if(grad_pos.y > 0)
			float x_ratio = 1.0;
			if(BTN_GRADIENT == 0)
			{
				dist = distance(vec2(0,v_texCoord.y*tex_size.y),vec2(0,grad_pos.y * tex_size.y));
				dist_top = distance(vec2(0,v_texCoord.y*tex_size.y),vec2(0,tex_size.y));

				dist = min(dist, dist_top);

				//Check also distance to the X direction
				float distance_left = distance(vec2(v_texCoord.x*tex_size.x,0),vec2(0,0));
				float distance_right = distance(vec2(v_texCoord.x*tex_size.x,0),vec2(tex_size.x,0));

				float dist_x = min(distance_left, distance_right);

				dist_x = dist_x/(gradient_r_x);
				if(dist_x > 1.0) dist_x = 1.0;

				//x_ratio = sin(PI * 0.5 * dist_x);
				x_ratio = flatSinWave(0.2, v_texCoord.x);

			}
			else
			{
				//dist = distance(vec2(0,v_texCoord.y*tex_size.y),vec2(0,grad_pos.y * tex_size.y));
				dist = sin(PI * v_texCoord.y);
				x_ratio = sin(PI * v_texCoord.x);
			}

/*			if(grad_pos.y > 0 && grad_pos.x > 0)
			{
				dist = distance(v_texCoord*tex_size, grad_pos* tex_size);
			}*/


			if(dist > gradient_r) dist = gradient_r;
			dist /= gradient_r;

			float ratio = dist;

			//TESTAIL
			if(BTN_GRADIENT == 0)
			{
				dist = sin(PI * v_texCoord.y);
			}
			else
			{
				dist = flatSinWave(0.5,v_texCoord.y);//sin(PI * v_texCoord.y);
				x_ratio = flatSinWave(0.25,v_texCoord.x);//sin(PI * v_texCoord.x);
			}
		
			//color = grad_color *0.5  + grad_color * 0.7*dist;
			//color = grad_color *0.3  + grad_color * 0.7*dist;

			color = grad_color *0.3  + grad_color * 1.0*dist * x_ratio;


			// color.a = 0.9;

			// if(grad_color.a > 0.6)
			// {
			// 	color.a = 1.0;
			// }

			//color.a = grad_color.a;
		
		
		vec3 color_m = vec3(color.r, color.g, color.b) * 0.9;//1.0;//1.2;

		//TESTAILUA
		color.a = grad_color.a;//FINAL_ALPHA;

		vec4 final_color = vec4(color_m.r, color_m.g, color_m.b, color.a);

		gl_FragColor = final_color;
		return;

	}

	gl_FragColor = vec4(0);

}
