#if !defined INCLUDE_LIGHT_COLORS_LIGHT_COLOR
#define INCLUDE_LIGHT_COLORS_LIGHT_COLOR

#include "/include/sky/atmosphere.glsl"
#include "/include/utility/color.glsl"
#include "/include/rand.glsl"

uniform float moon_phase_brightness;

float SUN_IX = randFromDay(1.0, 1.8, 10);

// Magic brightness adjustment so that auto exposure isn't needed
float get_sun_exposure() {
	float base_scale = 7.0 * SUN_IX;

	float blue_hour = linear_step(0.05, 1.0, exp(-190.0 * sqr(sun_dir.y + 0.09604)));

	float daytime_mul = 1.0 + 0.5 * (time_sunset + time_sunrise) + 40.0 * blue_hour;

	return base_scale * daytime_mul;
}

float min_X = 1.00;
float max_X = 1.05;

float SUN_MRX = randFromDay(min_X, max_X, 1);
float SUN_MGX = randFromDay(min_X, max_X, 2);
float SUN_MBX = randFromDay(min_X, max_X, 3);

float SUN_NRX = randFromDay(min_X, max_X, 4);
float SUN_NGX = randFromDay(min_X, max_X, 5);
float SUN_NBX = randFromDay(min_X, max_X, 6);

float SUN_ERX = randFromDay(min_X, max_X, 7);
float SUN_EGX = randFromDay(min_X, max_X, 8);
float SUN_EBX = randFromDay(min_X, max_X, 9);

float MOON_RX = randFromDay(min_X, max_X, 11);
float MOON_GX = randFromDay(min_X, max_X, 12);
float MOON_BX = randFromDay(min_X, max_X, 13);


vec3 get_sun_tint() {
	float blue_hour = linear_step(0.05, 1.0, exp(-190.0 * sqr(sun_dir.y + 0.09604)));

	vec3 morning_evening_tint = vec3(1.05, 0.84, 0.93) * 1.2;
	     morning_evening_tint = mix(vec3(1.0), morning_evening_tint, sqr(pulse(sun_dir.y, 0.17, 0.40)));

	vec3 blue_hour_tint = vec3(1.0, 0.85, 0.95);
	     blue_hour_tint = mix(vec3(1.0), blue_hour_tint, blue_hour);

	// User tint

	vec3 tint_morning = from_srgb(vec3(SUN_MRX, SUN_MGX, SUN_MBX));
	vec3 tint_noon    = from_srgb(vec3(SUN_NRX, SUN_NGX, SUN_NBX));
	vec3 tint_evening = from_srgb(vec3(SUN_ERX, SUN_EGX, SUN_EBX));

	vec3 user_tint = mix(tint_noon, tint_morning, time_sunrise);
	     user_tint = mix(user_tint, tint_evening, time_sunset);

	return morning_evening_tint * blue_hour_tint * user_tint;
}

float get_moon_exposure() {
	const float base_scale = 0.66 * MOON_I;

	return base_scale * moon_phase_brightness;
}

vec3 get_moon_tint() {
	vec3 base_tint = from_srgb(vec3(MOON_RX, MOON_GX, MOON_BX));

	return base_tint;
}

vec3 get_light_color() {
	vec3 light_color  = mix(get_sun_exposure() * get_sun_tint(), get_moon_exposure() * get_moon_tint(), step(0.5, sunAngle));
	     light_color *= sunlight_color * atmosphere_transmittance(light_dir.y, planet_radius);
	     light_color *= clamp01(rcp(0.02) * light_dir.y); // fade away during day/night transition
		 light_color *= 1.0 - 0.25 * pulse(abs(light_dir.y), 0.15, 0.11);
		 light_color *= 1.0 - rainStrength;

	return light_color;
}

float get_skylight_boost() {
	float night_skylight_boost = 4.0 * (1.0 - smoothstep(-0.16, 0.0, sun_dir.y))
	                           - 3.0 * linear_step(0.1, 1.0, exp(-2.42 * sqr(sun_dir.y + 0.81)));

	return 1.0 + max0(night_skylight_boost);
}

#endif // INCLUDE_LIGHT_COLORS_LIGHT_COLOR
