#include "adapters/hooks/player/PlayerHookCallbacks.h"

#include <atomic>

namespace ta::adapters::hooks::player
{
    namespace
    {
        std::atomic<FixedUpdateCallback> fixed_update_callback{nullptr};
        std::atomic<LocalGameStopCallback> local_game_stop_callback{nullptr};
    }

    void set_fixed_update_callback(const FixedUpdateCallback callback) noexcept
    {
        fixed_update_callback.store(callback, std::memory_order_release);
    }

    void set_local_game_stop_callback(const LocalGameStopCallback callback) noexcept
    {
        local_game_stop_callback.store(callback, std::memory_order_release);
    }

    void clear_callbacks() noexcept
    {
        fixed_update_callback.store(nullptr, std::memory_order_release);
        local_game_stop_callback.store(nullptr, std::memory_order_release);
    }

    void dispatch_fixed_update(Il2CppObject* instance) noexcept
    {
        if (const auto callback = fixed_update_callback.load(std::memory_order_acquire))
            callback(instance);
    }

    int dispatch_local_game_stop(const int exit_status) noexcept
    {
        if (const auto callback = local_game_stop_callback.load(std::memory_order_acquire))
            return callback(exit_status);
        return exit_status;
    }
}
