/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib-object.h>
#include "ftd4linux.h"
#include "ftd_event.h"
#include "ftd_eventinfo_cache.h"

typedef struct FTDEventInfoCacheClass FTDEventInfoCacheClass;

FTDEventInfoCache *ftd_event_info_cache_get_object(void);
GType ftd_event_info_cache_get_type (void);

struct _cache_element
{
    time_t stamp;
    gboolean is_active;
    FtdEvent *event;
};

struct FTDEventInfoCache
{
    GObject parent;

    FtdTree *cache;
    guint auto_purge_id;
};

struct FTDEventInfoCacheClass
{
    GObjectClass parent;
};

enum
{
  BRING_TO_FRONT_SIGNAL,
  OPEN_POSTING_SIGNAL,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE(FTDEventInfoCache, ftd_event_info_cache, G_TYPE_OBJECT)

static gint
tree_compare_func(gconstpointer a, gconstpointer b)
{
    return (GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b));
}

static gboolean
foreach_purge_func(gpointer key, gpointer value, gpointer data)
{
    struct _cache_element *elem = value;
    FtdList **list = data;

    if (elem->is_active) {
        return FALSE;
    }

    if (elem->stamp + 30 < time(NULL)) {
        *list = ftd_list_append(*list, key);
        ftd_event_free(elem->event);
        g_free(elem);
    }

    return FALSE;
}

static gboolean
auto_purge_func(gpointer data)
{
    FtdTree *tree = data;
    FtdList *list_total = NULL;
    FtdList *list;

    ftd_tree_foreach(tree, foreach_purge_func, &list_total);

    list = list_total;
    while (list) {
        gpointer postid = ftd_list_value(list);

        ftd_tree_remove(tree, postid);

        list = ftd_list_next(list);
    }

    ftd_list_free(list_total);

    return TRUE;
}

static void
ftd_event_info_cache_init (FTDEventInfoCache *obj)
{
    obj->cache = ftd_tree_new(tree_compare_func);

    // Ruim elke 30 seconden de cache op van inactieve elementen
    obj->auto_purge_id = g_timeout_add(30 * 1000, auto_purge_func, obj->cache);
}

static gboolean
foreach_clean_func(gpointer key, gpointer value, gpointer data)
{
    struct _cache_element *elem = value;

    ftd_event_free(elem->event);
    g_free(elem);

    return FALSE;
}

static void
ftd_event_info_cache_finalize (GObject *obj)
{
    FTDEventInfoCache *cache = FTD_EVENT_INFO_CACHE(obj);

    g_source_remove(cache->auto_purge_id);

    ftd_tree_foreach(cache->cache, foreach_clean_func, NULL);
    ftd_tree_destroy(cache->cache);
}

static void
ftd_event_info_cache_class_init (FTDEventInfoCacheClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    gobject_class->finalize = ftd_event_info_cache_finalize;
}

FTDEventInfoCache *
ftd_event_info_cache_new(void)
{
    return g_object_new(FTD_EVENT_INFO_CACHE_TYPE_OBJECT, NULL);
}

void
ftd_event_info_cache_destroy(FTDEventInfoCache *obj)
{
    g_object_unref(obj);
}

FtdEvent *
ftd_event_info_cache_lookup(FTDEventInfoCache *obj, int postId)
{
    FTDEventInfoCache *cache = FTD_EVENT_INFO_CACHE(obj);
    struct _cache_element *elem;

    elem = ftd_tree_lookup(cache->cache, GINT_TO_POINTER(postId));

    if (elem) {
        // update de timestamp
        elem->stamp = time(NULL);
        return ftd_event_duplicate(elem->event);
    }

    return NULL;
}

void
ftd_event_info_cache_append_eventinfo(FTDEventInfoCache *obj, const FtdEvent *event)
{
    FTDEventInfoCache *cache = FTD_EVENT_INFO_CACHE(obj);
    struct _cache_element *elem = g_malloc0(sizeof(struct _cache_element));

    elem->is_active = TRUE;     // Standaard gaan we ervan uit dat een posting die toegevoegd wordt aan de cache actief is
    elem->stamp = time(NULL);
    elem->event = ftd_event_duplicate(event);

    ftd_tree_insert(cache->cache, GINT_TO_POINTER(event->postId), elem);
}

void
ftd_event_info_cache_set_active(FTDEventInfoCache *obj, int postId, gboolean is_active)
{
    FTDEventInfoCache *cache = FTD_EVENT_INFO_CACHE(obj);
    struct _cache_element *elem = ftd_tree_lookup(cache->cache, GINT_TO_POINTER(postId));

    if (elem) {
        elem->is_active = is_active;
    }
}
