
#include <stdio.h>
#include <stdlib.h>

#include "../mecha/mecha.h"

#include "queue_playlist_engine.h"
#include "randomlist_playlist_engine.h"

#include "playlist_engine_factory.h"

struct s_PlaylistEngineFactory
{
    hshTvoid_list *playlistEngines;
};

typedef IPlaylist * (*IPlaylistConstructorDelegate) (const char *playlistName);

static void playlist_engine_factory_add_playlist (PlaylistEngineFactory *instance, 
        const char *playlistType, IPlaylistConstructorDelegate delegate);

PlaylistEngineFactory *
playlist_engine_factory_new (void)
{
    PlaylistEngineFactory *instance = (PlaylistEngineFactory *) malloc (sizeof (PlaylistEngineFactory));

    instance->playlistEngines = hshFvoid_init (NULL);

    playlist_engine_factory_add_playlist (instance, "queue", 
            (IPlaylistConstructorDelegate) queue_playlist_engine_init);

    playlist_engine_factory_add_playlist (instance, "randomlist", 
            (IPlaylistConstructorDelegate) randomlist_playlist_engine_init);

    return (instance);
}

void
playlist_engine_factory_destroy (void *ptr)
{
    PlaylistEngineFactory *instance = (PlaylistEngineFactory *) ptr;

    hshFvoid_destroy (instance->playlistEngines);

    free (instance);
}

static void
playlist_engine_factory_add_playlist (PlaylistEngineFactory *instance, const char *playlistType, 
        IPlaylistConstructorDelegate delegate)
{
    hshFvoid_replace (instance->playlistEngines, playlistType, (void *) delegate);
}

IPlaylist *
playlist_engine_factory_get_playlist (PlaylistEngineFactory *instance, const char *playlistType, const char *playlistName)
{
    IPlaylistConstructorDelegate constructor;

    constructor = (IPlaylistConstructorDelegate) hshFvoid_find (instance->playlistEngines, playlistType);

    if (constructor == NULL)
    {
        return (NULL);
    }
    else
    {
        return (constructor (playlistName));
    }
}
