import package::shared::{VertexInput, VertexOutput, sample_shadow_map, flowmap_path_lighting_with_shadow, all_spotlights_lighting, uniforms, TERRAIN_BOUNDS, flowmap_texture, flowmap_sampler};

@vertex
fn vs_main(input: VertexInput) -> VertexOutput {
    var output: VertexOutput;

    let instance_model = mat4x4<f32>(
        input.instance_model_0,
        input.instance_model_1,
        input.instance_model_2,
        input.instance_model_3
    );

    let world_pos = instance_model * vec4<f32>(input.position, 1.0);
    output.world_position = world_pos.xyz;
    output.clip_position = uniforms.projection * uniforms.view * world_pos;

    let normal_matrix = mat3x3<f32>(
        instance_model[0].xyz,
        instance_model[1].xyz,
        instance_model[2].xyz
    );
    output.world_normal = normalize(normal_matrix * input.normal);

    output.light_space_position = uniforms.light_view_projection * world_pos;

    return output;
}

@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
    let debug = 0u;

    if debug == 4u {
        let proj_coords = input.light_space_position.xyz / input.light_space_position.w;
        let ndc_coords = proj_coords * vec3<f32>(0.5, -0.5, 1.0) + vec3<f32>(0.5, 0.5, 0.0);
        let in_bounds = ndc_coords.x >= 0.0 && ndc_coords.x <= 1.0 &&
                       ndc_coords.y >= 0.0 && ndc_coords.y <= 1.0 &&
                       ndc_coords.z >= 0.0 && ndc_coords.z <= 1.0;
        if in_bounds {
            return vec4<f32>(ndc_coords.x, ndc_coords.y, ndc_coords.z, 1.0);
        } else {
            return vec4<f32>(0.0, 0.0, 0.0, 1.0);
        }
    }

    if debug == 3u {
        let shadow = sample_shadow_map(input.light_space_position);
        return vec4<f32>(shadow, shadow, shadow, 1.0);
    }

    if debug == 1u {
        let flowmap_uv = (vec2<f32>(input.world_position.x, input.world_position.z) + TERRAIN_BOUNDS * 0.5) / TERRAIN_BOUNDS;
        let flowmap_sample = textureSampleLevel(flowmap_texture, flowmap_sampler, flowmap_uv, 0.0).rgb;
        return vec4<f32>(flowmap_sample, 1.0);
    }

    if debug == 2u {
        let world_pos_2d = vec2<f32>(input.world_position.x, input.world_position.z);
        let tile_size = 10.0;
        let tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5;
        let flowmap_uv = (tile_center + TERRAIN_BOUNDS * 0.5) / TERRAIN_BOUNDS;

        let flowmap_sample = textureSampleLevel(flowmap_texture, flowmap_sampler, flowmap_uv, 0.0).rgb;
        let x = (flowmap_sample.r) * 2.0 - 1.0;
        let y = flowmap_sample.g * 2.0 - 1.0;
        let direction_to_path = normalize(vec2<f32>(x, y));
        let perpendicular_to_path = normalize(vec2<f32>(-direction_to_path.y, direction_to_path.x));

        let local_pos = world_pos_2d - tile_center;

        let arrow_scale = 0.05;
        let parallel_coord = dot(local_pos, direction_to_path) * arrow_scale;
        let perpendicular_coord = dot(local_pos, perpendicular_to_path) * arrow_scale;

        let to_path = step(0.95, fract(parallel_coord));
        let to_perp = step(0.95, fract(perpendicular_coord));

        return vec4<f32>(to_perp, to_perp, to_perp, 1.0);
    }

    let shadow = sample_shadow_map(input.light_space_position);

    let tile_scale = 2.0;
    let spotlight_strokes = all_spotlights_lighting(input.world_position, input.clip_position, input.world_normal, tile_scale, shadow);
    let brightness = spotlight_strokes;

    return vec4<f32>(brightness, brightness, brightness, 1.0);
}
