// Copyright (C) 2022 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#version 320 es
precision highp float;

#include "include/camera.glsl"
#include "include/environment.glsl"

in vec3 v_position;

uniform float u_sea_phase;

#define u_right u_view[0].xyz
#define u_forward u_view[1].xyz
#define u_up u_view[2].xyz
#define u_origin u_view[3].xyz

layout(binding=0) uniform highp sampler2DArray u_sea_polar_sampler;
layout(binding=1) uniform highp sampler2D u_sea_detail_sampler;

const float c_sea_radius = 637.1;
const float c_sea_radius_sq = c_sea_radius * c_sea_radius;
const float c_sky_radius = c_sea_radius + 10.0;
const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0);
const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0);
const float c_detail_scale = 2.0;

layout(location = 0) out vec4 o_color;

vec3 sky(in vec3 ray_direction) {
    float d = max(0.0, dot(ray_direction, u_light_direction));
    return mix(u_horizon_color, u_sky_color, max(0.0, dot(ray_direction, u_up))) + u_sun_color * pow(d, 1000.0);
}

void main(void) {
    vec3 direction = normalize(v_position);
    vec3 earth_center = u_origin - u_up * c_sea_radius;
    float p_dist = dot(direction, earth_center);
    vec3 pc = earth_center - direction * p_dist;
    if (p_dist <= 0.0 || dot(pc, pc) >= c_sea_radius_sq) {
        // sky
        o_color = vec4(sky(direction), 1.0);
    } else {
        // sea
        vec3 sea_position = direction * (p_dist - sqrt(c_sea_radius_sq - dot(pc, pc))) - u_origin;
        vec3 sea_direction = normalize(sea_position);
        //TODO: vec2
        float s = dot(u_forward, sea_direction);
        if (dot(u_right, sea_direction) > 0.0) {
            // [1.0 -1.0] -> [0.0 0.5]
            s = (1.0 - s) * 0.25;
        } else {
            // [-1.0 1.0] -> [0.5 1.0] -> [0.0 0.5] + 0.5
            s = (1.0 + s) * 0.25 + 0.5;
        }
        float t = sqrt(length(sea_position)); //TODO: more accurate
        vec3 sea_polar1 = normalize(
            c_normal_shift + texture(u_sea_polar_sampler, vec3(s, t + u_sea_phase, 0.0)).xyz * c_normal_scale);
        vec3 sea_polar2 = normalize(
            c_normal_shift + texture(u_sea_polar_sampler, vec3(s, t - u_sea_phase, 1.0)).xyz * c_normal_scale);
        //TODO: vec2
        s = (u_sea_phase + dot(sea_position, u_right)) * c_detail_scale;
        t = (u_sea_phase + dot(sea_position, u_forward)) * c_detail_scale;
        vec3 sea_detail = normalize(c_normal_shift + texture(u_sea_detail_sampler, vec2(s, t)).xyz * c_normal_scale);
        //TODO: better blending, with earth normal
        vec4 normal = u_view * vec4(normalize(sea_polar1 + sea_polar2 + sea_detail), 0.0);
        float d = max(0.0, dot(normal.xyz, u_light_direction));
        s = pow(max(0.0, dot(normal.xyz, normalize(u_light_direction - direction))), 500.0) * step(0.0, d);
        o_color = vec4(
            u_sky_color * d + //TODO: sea color
            u_light_color * s +
            sky(reflect(direction, normal.xyz)), 1.0);
    }
}
