#include "OSDManager.h"

static float g_timer = 0.0f;
static OSDUpdateFn g_onUpdate;
static OSDEndFn g_onEnd;

// Reshade shader uniform variables
reshade::api::effect_uniform_variable u_td_show = {}; // Time dilation
reshade::api::effect_uniform_variable u_td_enabled = {};
reshade::api::effect_uniform_variable u_td_world = {}; // Time dilation world
reshade::api::effect_uniform_variable u_td_AI = {}; // Time dilation AI
reshade::api::effect_uniform_variable u_GodMode_show = {};
reshade::api::effect_uniform_variable u_GodMode_enabled = {};
reshade::api::effect_uniform_variable u_IgnoreHits_show = {};
reshade::api::effect_uniform_variable u_IgnoreHits_enabled = {};
reshade::api::effect_uniform_variable u_Stealth_show = {};
reshade::api::effect_uniform_variable u_Stealth_enabled = {};
reshade::api::effect_uniform_variable u_Stamina_show = {};
reshade::api::effect_uniform_variable u_Stamina_enabled = {};
reshade::api::effect_uniform_variable u_Mana_show = {};
reshade::api::effect_uniform_variable u_Mana_enabled = {};

void ShowOSD(float duration, OSDUpdateFn onUpdate, OSDEndFn onEnd) {
    g_timer = duration;
    g_onUpdate = std::move(onUpdate);
    g_onEnd = std::move(onEnd);
}

void UpdateOSD(reshade::api::effect_runtime* runtime, float deltaTime) {
    if (g_timer <= 0.0f) return;

    g_timer -= deltaTime;

    if (g_onUpdate) g_onUpdate(runtime);

    if (g_timer <= 0.0f) {
        if (g_onEnd) g_onEnd(runtime);

        g_onUpdate = nullptr;
        g_onEnd = nullptr;
    }
}

void FindAllUniformVariables(reshade::api::effect_runtime* runtime, const char* effect_name) {
    u_td_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowTD");
    u_td_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "TD_Enabled");
    u_td_world = runtime->find_uniform_variable(OSD_SHADER_NAME, "TD_World");
    u_td_AI = runtime->find_uniform_variable(OSD_SHADER_NAME, "TD_AI");
    u_GodMode_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowGodMode");
    u_GodMode_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "GodMode_Enabled");
    u_IgnoreHits_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowIgnoreHits");
    u_IgnoreHits_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "IgnoreHits_Enabled");
    u_Stealth_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowStealth");
    u_Stealth_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "Stealth_Enabled");
    u_Stamina_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowStamina");
    u_Stamina_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "Stamina_Enabled");
    u_Mana_show = runtime->find_uniform_variable(OSD_SHADER_NAME, "OSD_ShowMana");
    u_Mana_enabled = runtime->find_uniform_variable(OSD_SHADER_NAME, "Mana_Enabled");
}

void ResetAllUniformVariables(reshade::api::effect_runtime* runtime, const char* effect_name) {
    runtime->enumerate_uniform_variables(effect_name, [runtime](reshade::api::effect_runtime* rt, reshade::api::effect_uniform_variable OSDvar) {
        runtime->reset_uniform_value(OSDvar);
    });
}