#include "utils.h"

// memmove
#include <string.h>

#define KEY_COUNT (SAPP_KEYCODE_MENU + 1)

struct {
    bool keys[KEY_COUNT];
    bool prev_keys[KEY_COUNT];
    HMM_Vec2 mouse_pos;
    HMM_Vec2 mouse_delta;
    int delta_frames_passed;
} utils_state = {0};

bool is_key_down(sapp_keycode k) {
    return utils_state.keys[k];
}

bool is_key_pressed(sapp_keycode k) {
    return !utils_state.prev_keys[k] && utils_state.keys[k];
}

void set_key_state(sapp_keycode k, bool is_down) {
    utils_state.prev_keys[k] = utils_state.keys[k];
    utils_state.keys[k] = is_down;
}

void utils_update(void) {
    memmove(utils_state.prev_keys, utils_state.keys, sizeof(utils_state.keys));
    if (utils_state.delta_frames_passed++ > 0) {
        utils_state.mouse_delta = HMM_V2(0, 0);
    }
}

HMM_Vec2 mouse_pos(void) {
    return utils_state.mouse_pos;
}

HMM_Vec2 mouse_delta(void) {
    return utils_state.mouse_delta;
}

void set_mouse_pos_and_delta(float x, float y, float dx, float dy) {
    utils_state.mouse_pos = HMM_V2(x, y);
    utils_state.mouse_delta = HMM_V2(dx, dy);
    utils_state.delta_frames_passed = 0;
}
