/*
 * You may redistribute this program and/or modify it under the terms of
 * the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <event2/event.h>

#include "memory/MemAllocator.h"
#include "memory/BufferAllocator.h"
#include "util/Timeout.h"

/**
 * This is slow so it should only be run manually.
 */

struct Context
{
    struct Timeout* timeout;

    uint32_t count;
};

void callback(void* context)
{
    printf("Timeout was called!\n");
    *((uint32_t*) context) = 1;
}

void intervalCallback(void* vcontext)
{
    struct Context* context = (struct Context*) vcontext;
    context->count++;
    if (context->count > 2) {
        Timeout_resetTimeout(context->timeout, 5000);
    }
    if (context->count > 3) {
        Timeout_clearTimeout(context->timeout);
    }
    printf("Interval was called!\n");
}

int main()
{
    uint8_t buffer[1024];
    struct MemAllocator* allocator = BufferAllocator_new(buffer, 1024);
    struct Context timeoutContext = {NULL, 0};
    struct Context intervalContext = {NULL, 0};
    struct event_base* base = event_base_new();
    timeoutContext.timeout =
        Timeout_setTimeout(callback, &timeoutContext, 2000, base, allocator);
    intervalContext.timeout =
        Timeout_setInterval(intervalCallback, &intervalContext, 500, base, allocator);
    event_base_loop(base, 0);
}
