/* ====================================================================
 * 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_app_core.h"
#include "ftd_event.h"

typedef struct FTDEventObjectClass FTDEventObjectClass;

FTDEventObject *ftd_event_get_object(void);
GType ftd_event_object_get_type (void);

struct FTDEventObjectClass
{
    GObjectClass parent;
};

enum
{
  BRING_TO_FRONT_SIGNAL,
  OPEN_POSTING_SIGNAL,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE(FTDEventObject, ftd_event_object, G_TYPE_OBJECT)

static void
ftd_event_object_init (FTDEventObject *obj)
{
    g_static_rw_lock_init(&obj->rwlock);

    obj->event = ftd_event_new();
}

static void
event_finalize (GObject *obj)
{
    FTDEventObject *event = FTD_EVENT_OBJECT(obj);

    ftd_event_free(event->event);
    event->event = NULL;

    g_static_rw_lock_free(&event->rwlock);
}

static void
ftd_event_object_class_init (FTDEventObjectClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    gobject_class->finalize = event_finalize;

#if 0
    signals[BRING_TO_FRONT_SIGNAL] = g_signal_new  ("bring_to_front",
                                                    G_OBJECT_CLASS_TYPE (klass),
                                                    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                                                    0,
                                                    NULL, NULL,
                                                    g_cclosure_marshal_VOID__VOID,
                                                    G_TYPE_NONE, 0);

    signals[OPEN_POSTING_SIGNAL] = g_signal_new    ("open_posting",
                                                    G_OBJECT_CLASS_TYPE (klass),
                                                    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                                                    0,
                                                    NULL, NULL,
                                                    g_cclosure_marshal_VOID__INT,
                                                    G_TYPE_NONE, 1, G_TYPE_INT);
#endif
}

/*
 * Maakt een nieuw event aan.
 * Alles in de event is 0 met uitzondering van postId, cat en sCat: deze zijn -1.
 *
 * @return  Een nieuw event.
 */
FtdEvent *
ftd_event_new(void)
{
    FtdEvent *event;

    event = g_malloc0(sizeof(FtdEvent));
    event->postId        = -1;
    event->stamp         = 0;
    event->status        = FTDSTATUS_NONE;
    event->fileName      = NULL;
    event->title         = NULL;
    event->poster        = NULL;
    event->link          = NULL;
    event->cat           = -1;
    event->sCat          = -1;
    event->tags          = NULL;
    event->extra_tag     = NULL;
    event->updated       = 0;
    event->rating        = 0;
    event->groups        = NULL;

    event->announcement  = NULL;
    event->description   = NULL;
    event->sig           = NULL;
    event->image         = NULL;
    event->fileType      = 0;
    event->aantalFiles   = 0;
    event->fileStart     = 0;
    event->mp3Tracks     = NULL;

    event->aantalParFiles= 0;
    event->kudos         = 0;
    event->parType       = 0;
    event->parBlockSize  = 0;
    event->numViews      = 0;
    event->locked        = 0;
    event->reason        = NULL;

    event->forum = NULL;

    return event;
}

/**
 * Geeft een event vrij.
 *
 * @arg     event       Het event om vrij te geven.
 */
void
ftd_event_free(FtdEvent *event)
{
    FtdList *list;

    g_assert(event);
    if (event->fileName) {
        ftd_string_free(event->fileName);
    }
    if (event->title) {
        ftd_string_free(event->title);
    }
    if (event->poster) {
        ftd_string_free(event->poster);
    }
    if (event->link) {
        ftd_string_free(event->link);
    }

    if( event->mp3Tracks ) {
        list = event->mp3Tracks;
        while (list) {
            FtdString *track = ftd_list_value(list);
            g_assert(track);
            ftd_string_free(track);
            list = ftd_list_next(list);
        }
        ftd_list_free(event->mp3Tracks);
    }

    if (event->image) {
        ftd_string_free(event->image);
    }
    if (event->description) {
        ftd_string_free(event->description);
    }
    if (event->sig) {
        ftd_string_free(event->sig);
    }
    if (event->announcement) {
        ftd_string_free(event->announcement);
    }
    if (event->groups) {
        ftd_splitted_list_free( &event->groups );
    }
    if (event->forum) {
        ftd_events_forum_free(event->forum);
    }
    if( event->tags ) {
        ftd_splitted_list_free( &event->tags );
    }
    if( event->extra_tag ) {
        ftd_string_free( event->extra_tag );
    }
    if (event->reason) {
        ftd_string_free(event->reason);
    }

    g_free(event);
}

FTDEventObject *
ftd_event_object_new(void)
{
    return g_object_new(FTD_EVENT_TYPE_OBJECT, NULL);
}

void
ftd_event_object_destroy(FTDEventObject *obj)
{
    g_object_unref(obj);
}

FtdEvent *
ftd_event_duplicate(const FtdEvent *event_orig)
{
    FtdEvent *event_nieuw;
    FtdList *list;

    event_nieuw = g_malloc0(sizeof(FtdEvent));
    memcpy(event_nieuw, event_orig, sizeof(FtdEvent));

    // Alloceer de strings en lists opnieuw
    event_nieuw->fileName = ftd_string_new(ftd_string_value(event_orig->fileName));
    event_nieuw->title = ftd_string_new(ftd_string_value(event_orig->title));
    event_nieuw->poster = ftd_string_new(ftd_string_value(event_orig->poster));
    event_nieuw->link = ftd_string_new(ftd_string_value(event_orig->link));
    event_nieuw->extra_tag = ftd_string_new(ftd_string_value(event_orig->extra_tag));
    event_nieuw->announcement = ftd_string_new(ftd_string_value(event_orig->announcement));
    event_nieuw->description = ftd_string_new(ftd_string_value(event_orig->description));
    event_nieuw->sig = ftd_string_new(ftd_string_value(event_orig->sig));
    event_nieuw->image = ftd_string_new(ftd_string_value(event_orig->image));
    event_nieuw->groups = ftd_splitted_list_duplicate(event_orig->groups);
    event_nieuw->tags = ftd_splitted_list_duplicate(event_orig->tags);
    event_nieuw->reason = ftd_string_new(ftd_string_value(event_orig->reason));

    // Dupliceer de MP3 tracks
    event_nieuw->mp3Tracks = NULL;
    list = event_orig->mp3Tracks;
    while (list) {
        FtdString *track = ftd_list_value(list);

        event_nieuw->mp3Tracks = ftd_list_append(event_nieuw->mp3Tracks, ftd_string_new(ftd_string_value(track)));

        list = ftd_list_next(list);
    }

    // Dupliceer de forum informatie
    if (event_orig->forum) {
        event_nieuw->forum = ftd_events_forum_new();
        event_nieuw->forum->lastUpdated = event_orig->forum->lastUpdated;

        list = event_orig->forum->forum_reacties;
        while (list) {
            FtdEventForumReactie *reactie_nieuw;
            FtdEventForumReactie *reactie_oud = ftd_list_value(list);

            reactie_nieuw = ftd_events_forum_reactie_new();
            memcpy(reactie_nieuw, reactie_oud, sizeof(FtdEventForumReactie));
            reactie_nieuw->author = ftd_string_new(ftd_string_value(reactie_oud->author));
            reactie_nieuw->body = ftd_string_new(ftd_string_value(reactie_oud->body));
            reactie_nieuw->avatar = ftd_string_new(ftd_string_value(reactie_oud->avatar));

            event_nieuw->forum->forum_reacties = ftd_list_append(event_nieuw->forum->forum_reacties, reactie_nieuw);
            list = ftd_list_next(list);
        }
    }

    return event_nieuw;
}

FtdEvent *
ftd_event_object_get_event(FTDEventObject *obj)
{
    FTDEventObject *event_obj = FTD_EVENT_OBJECT(obj);
    FtdEvent *event;

    g_static_rw_lock_reader_lock(&event_obj->rwlock);

    event = ftd_event_duplicate(event_obj->event);

    g_static_rw_lock_reader_unlock(&event_obj->rwlock);

    return event;
}

void
ftd_event_object_set_event(FTDEventObject *obj, const FtdEvent *event)
{
    FTDEventObject *event_obj = FTD_EVENT_OBJECT(obj);

    g_static_rw_lock_writer_lock(&event_obj->rwlock);

    ftd_event_free(event_obj->event);
    event_obj->event = ftd_event_duplicate(event);

    g_static_rw_lock_writer_unlock(&event_obj->rwlock);
}
