#include "ingame.h"
#include "title.h"
#include "scene.h"

#include "twn_game_api.h"

#define STB_PERLIN_IMPLEMENTATION
#include <stb_perlin.h>
#include <SDL2/SDL.h>

#include <stdlib.h>


static void ingame_tick(State *state) {
    SceneIngame *scn = (SceneIngame *)state->scene;

    if (input_is_mouse_captured(&ctx.input)) {
        const float sensitivity = 0.6f; /* TODO: put this in a better place */
        scn->yaw += (float)ctx.input.mouse_relative_position.x * sensitivity;
        scn->pitch -= (float)ctx.input.mouse_relative_position.y * sensitivity;
        scn->pitch = clampf(scn->pitch, -89.0f, 89.0f);

        const float yaw_rad = scn->yaw * (float)DEG2RAD;
        const float pitch_rad = scn->pitch * (float)DEG2RAD;

        scn->cam.target = m_vec_norm(((Vec3){
            cosf(yaw_rad) * cosf(pitch_rad),
            sinf(pitch_rad),
            sinf(yaw_rad) * cosf(pitch_rad)
        }));
    }

    const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
    const float speed = 0.04f; /* TODO: put this in a better place */
    if (input_is_action_pressed(&ctx.input, "player_left"))
        scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));

    if (input_is_action_pressed(&ctx.input, "player_right"))
        scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));

    if (input_is_action_pressed(&ctx.input, "player_forward"))
        scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));

    if (input_is_action_pressed(&ctx.input, "player_backward"))
        scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));

    if (input_is_action_pressed(&ctx.input, "player_jump"))
        scn->cam.pos.y += speed;

    if (input_is_action_pressed(&ctx.input, "player_run"))
        scn->cam.pos.y -= speed;

    /* toggle mouse capture with end key */
    if (input_is_action_just_pressed(&ctx.input, "mouse_capture_toggle")) {
        input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input));
    }

    set_camera(&scn->cam);

    #define TERRAIN_FREQUENCY 0.1f

    for (int ly = 64; ly--;) {
        for (int lx = 64; lx--;) {
            float x = SDL_truncf(scn->cam.pos.x + 32 - lx);
            float y = SDL_truncf(scn->cam.pos.z + 32 - ly);

            float d0 = stb_perlin_noise3((float)x       * TERRAIN_FREQUENCY, (float)y       * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
            float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y       * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
            float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
            float d3 = stb_perlin_noise3((float)x       * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;

            unfurl_triangle("/assets/grass.png",
                            (Vec3){ (float)x,     d0, (float)y },
                            (Vec3){ (float)x + 1, d1, (float)y },
                            (Vec3){ (float)x,     d3, (float)y - 1 },
                            (Vec2){ 128, 128 },
                            (Vec2){ 128, 0 },
                            (Vec2){ 0, 128 });

            unfurl_triangle("/assets/grass.png",
                            (Vec3){ (float)x + 1, d1, (float)y },
                            (Vec3){ (float)x + 1, d2, (float)y - 1 },
                            (Vec3){ (float)x,     d3, (float)y - 1 },
                            (Vec2){ 128, 0 },
                            (Vec2){ 0, 0 },
                            (Vec2){ 0, 128 });
        }
    }

    push_skybox("/assets/miramar/miramar_*.tga");
    push_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
}


static void ingame_end(State *state) {
    free(state->scene);
}


Scene *ingame_scene(State *state) {
    (void)state;

    SceneIngame *new_scene = ccalloc(1, sizeof *new_scene);
    new_scene->base.tick = ingame_tick;
    new_scene->base.end = ingame_end;

    new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };

    audio_play_ex("music/mod65.xm", "soundtrack", (PlayAudioArgs){
        .repeat = true,
        .volume = 1.0f
    });

    input_set_mouse_captured(&ctx.input, true);

    return (Scene *)new_scene;
}