/** @file MOMContentCatalogue.cpp
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#include "config.h"

#if ENABLE(RMAI)

#include "MOMContentCatalogue.h"
#include "MOMContentItem.h"
#include "MOMStringCollection.h"
#include "MOMException.h"
#include "MOMContentSearch.h"
#include "UtilsMOM.h"

namespace WebCore {

MOMContentCatalogue::MOMContentCatalogue(ScriptExecutionContext *context, mom_content_item item)
    : MOMContentItem(context, item)
{
}

MOMContentItem* MOMContentCatalogue::rootFolder() const
{
    if (!m_rootFolder)
    {
        mom_content_paged_list root = mom_content_catalogue_get_root_folder(itemAsContentCatalogue());
        if (!root)
            return NULL;
        m_rootFolder = MOMContentItem::create(scriptExecutionContext(), (mom_content_item)root);
    }
    return m_rootFolder.get();
}

MOMStringCollection* MOMContentCatalogue::genres() const
{
    if (!m_genres)
        m_genres = MOMStringCollection::create(mom_content_catalogue_get_genres(itemAsContentCatalogue()));
    return m_genres.get();
}

PassRefPtr<MOMContentItem> MOMContentCatalogue::createSearch()
{
    mom_content_search search = mom_content_catalogue_create_search(itemAsContentCatalogue());
    return search ? MOMContentItem::createAdopt(scriptExecutionContext(), (mom_content_item)search) : nullptr;
}

void MOMContentCatalogue::signOn( const String & userId, ExceptionCode& ec )
{
    layers_string luserId = userId.isNull() ? NULL : UtilsMOM::LayersStringFromString(userId);
    mom_error err = mom_content_catalogue_signon(itemAsContentCatalogue(), luserId);
    layers_string_dispose(luserId);
    MOMException::setFromMOMError(err, ec);
}

void MOMContentCatalogue::signOn(ExceptionCode& ec )
{
	signOn(String(), ec);
}

void MOMContentCatalogue::signOff(ExceptionCode& ec)
{
    mom_error err = mom_content_catalogue_signoff(itemAsContentCatalogue());
    MOMException::setFromMOMError(err, ec);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
