#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;
in vec3 ourNormal;
in vec3 FragPos;

uniform sampler2D texture1;
uniform vec3 sunPos;
uniform vec3 moonPos;
uniform int colorOnly;
uniform vec3 viewPos;

void main()
{
	//colorSample = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
    //colorSample = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
   vec3 colorSample=texture(texture1, TexCoord).rgb;

    float ambientStrength = 0.25f;
    vec3 sunColor = vec3(1.0f, 0.93f, 0.74f); // luminate color
    vec3 moonColor = vec3(0.38f, 0.5f, 0.5f); // _sky and _moon
    //vec3 objColor = vec3(0.2f, 0.9f, 0.1f); // for sphere color
    //vec3 norm=normalize(ourNormal);

    float sunY=sunPos.y*0.01; // 1000th = normalized?~
    float sunStrength=clamp(sunY,0.0,1.3); // sun brightness
    float moonStrength=clamp(1.0-sunStrength,0.0,0.5);
    //vec3 blendColor = mix(sunColor,moonColor,0.0-sunStrength); // tone?
    float lightStrength = max(sunStrength,moonStrength);
    vec3 ambient = ambientStrength * colorSample;
    vec3 ambientColorOnly = (ambientStrength * 2.0) * ourColor;

    vec3 lightPos = mix(moonPos,sunPos,sunStrength);
    vec3 lightColor = mix(moonColor,sunColor,sunStrength);


    // diffuse 
    vec3 norm = normalize(ourNormal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = lightColor * (diff * ambient);
    vec3 diffuseColorOnly = lightColor * (ambientColorOnly * lightStrength);
    
    // specular
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);  
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 0.5); // 0.5 shininess
    vec3 specular = lightColor * (spec * 0.5); // 0.5 specular  
        
   //-- vec3 result = ambient + diffuse + specular;
   //-- FragColor = vec4(result, 1.0);





    //vec3 diff = blendColor * colorSample;
    
    //vec3 result=(ambient + (diff * 0.35)) * colorSample;
    //vec3 result = ambient + diffuse + specular;
    vec3 result = ambient + diffuse;
    //vec3 result = diffuse;
    //vec3 result = ourNormal;
    //vec3 result = ambient;

    //result=colorSample;

    if(colorOnly==1) { // each triangle color variation
        //diff = blendColor * ourColor;

        //result=(ambient + (diff * 0.35)) * ourColor;
        //result=(ambient + ((lightColor * ourColor) * 0.35)) * ourColor;
        //result = ambientColorOnly + diffuseColorOnly;
        result = ambientColorOnly + diffuseColorOnly;
    }

    FragColor = vec4(result, 1.0f);

   //vec3 result=(ambient + ((diffuse + diffuse2) * 0.5)) * colorSample;
   //FragColor = vec4(result, 1.0f);
   // FragColor = vec4(colorSample, 1.0f); // clean
    //FragColor = vec4(TexCoord.x, TexCoord.y, 0.0, 1.0f); // clean

};
