/*
 * This program source code file is part of the KTag project, a DIY laser tag
 * game with customizable features and wide interoperability.
 *
 * 🛡 <https://ktag.clubk.club> 🃞
 *
 * Copyright © 2025-2026 Joseph P. Kearney and the KTag developers.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 *
 * There should be a copy of the GNU Affero General Public License in the LICENSE
 * file in the root of this repository.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "System_Events.h"

static EventGroupHandle_t The_System_Events = NULL;

static const char *TAG = "System Events";

void Initialize_System_Events(void)
{
    if (The_System_Events == NULL)
    {
        The_System_Events = xEventGroupCreate();
        KLOG_INFO(TAG, "System Events initialized.");
    }
}

EventGroupHandle_t Get_System_Events(void)
{
    return The_System_Events;
}

bool Wait_For_System_Event(EventBits_t event_bit, const char* timeout_message, uint32_t timeout_ms)
{
    EventBits_t bits = xEventGroupWaitBits(
        The_System_Events,
        event_bit,
        pdFALSE,
        pdTRUE,
        pdMS_TO_TICKS(timeout_ms));

    if ((bits & event_bit) == 0)
    {
        KLOG_ERROR(TAG, "%s", timeout_message);
        return false;
    }
    
    return true;
}
