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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMGuideSearch.h"
#include "MOMContentItem.h"
#include "MOMChannel.h"
#include "MOMChannelList.h"

#include "MOMException.h"
#include "MOMEvent.h"
#include "EventNames.h"

extern "C" {
#include "layers.h"
#include "cdf/mom_channel_list.h"
#include "cdf/mom_content_list.h"
}

namespace WebCore {

PassRefPtr<MOMGuideSearch> MOMGuideSearch::create(ScriptExecutionContext* context)
{
    return adoptRef(new MOMGuideSearch(context));
}

mom_error MOMGuideSearch::eventDispatcher(mom_event_t which, void* target, mom_event_params* params, unsigned int queue)
{
    return MOMEvent::dispatchMOMEvent(which, static_cast<MOMGuideSearch*>(target), params, queue);
}

MOMGuideSearch::MOMGuideSearch(ScriptExecutionContext* context)
    : ActiveDOMObject(context)
{
    if (mom_guide_search_create(&m_search) == ERR_NONE)
    {
        mom_guide_search_set_events(m_search, this, MOMGuideSearch::eventDispatcher);
    }
}

MOMGuideSearch::~MOMGuideSearch()
{
    if (m_search)
    {
        mom_guide_search_stop( m_search );
        mom_guide_search_set_events( m_search, NULL, 0 );
        mom_guide_search_dispose( m_search );
        m_search = NULL;
    }
}

EventTargetInterface MOMGuideSearch::eventTargetInterface() const
{
    return MOMGuideSearchEventTargetInterfaceType;
}

MOMContentItem* MOMGuideSearch::channels() const
{
    if (!m_channels)
        m_channels = MOMContentItem::create(scriptExecutionContext(), (mom_content_item)mom_guide_search_channels(m_search));
    return m_channels.get();
}

int MOMGuideSearch::from() const
{
    return mom_guide_search_from(m_search);
}

int MOMGuideSearch::to() const
{
    return mom_guide_search_to(m_search);
}

int MOMGuideSearch::startChannel() const
{
    return mom_guide_search_start_channel(m_search);
}

int MOMGuideSearch::endChannel() const
{
    return mom_guide_search_end_channel(m_search);
}

MOMContentItem* MOMGuideSearch::programmes() const
{
    if (!m_programmes)
        m_programmes = MOMContentItem::create(scriptExecutionContext(), (mom_content_item)mom_guide_search_programmes(m_search));
    return m_programmes.get();
}

bool MOMGuideSearch::find( MOMChannelList* channels, int startChannel, int endChannel, int from, int to, ExceptionCode& ec )
{
    if (channels == NULL) {
        ec = SYNTAX_ERR;
        return false;
    }
    BOOL ready = FALSE;
    mom_error err = mom_guide_search_find(m_search, channels->itemAsChannelList(), startChannel, endChannel, from, to, 0, &ready );
    if (err == ERR_NONE && ready)
        err = mom_guide_search_update(m_search);
    MOMException::setFromMOMError(err, ec);
    return ready;
}

bool MOMGuideSearch::findProgrammes( MOMChannel* channel, int startTime, int count, ExceptionCode& ec )
{
    if (channel == NULL) {
        ec = SYNTAX_ERR;
        return false;
    }
    BOOL ready = FALSE;
    mom_channel_list channels;
    mom_error err = mom_channel_list_create(&channels);
    if (err == ERR_NONE)
    {
        err = mom_content_list_add_item((mom_content_list)channels, channel->itemAsItem(), NULL);
        if (err == ERR_NONE)
            err = mom_guide_search_find(m_search, channels, 0, 0, startTime, 0, count, &ready );
        if (err == ERR_NONE && ready)
            err = mom_guide_search_update(m_search);
        mom_content_item_dispose((mom_content_item)channels);
    }
    MOMException::setFromMOMError(err, ec);
    return ready;
}

bool MOMGuideSearch::findNowNext( MOMChannel* channel, ExceptionCode& ec )
{
    if (channel == NULL) {
        ec = SYNTAX_ERR;
        return false;
    }
    BOOL ready = FALSE;
    mom_channel_list channels;
    mom_error err = mom_channel_list_create(&channels);
    if (err == ERR_NONE)
    {
        err = mom_content_list_add_item((mom_content_list)channels, channel->itemAsItem(), NULL);
        if (err == ERR_NONE)
            err = mom_guide_search_find(m_search, channels, 0, 0, 0, 0, 2, &ready );
        if (err == ERR_NONE && ready)
            err = mom_guide_search_update(m_search);
        mom_content_item_dispose((mom_content_item)channels);
    }
    MOMException::setFromMOMError(err, ec);
    return ready;
}

void MOMGuideSearch::stop(ExceptionCode& ec)
{
    mom_error err = mom_guide_search_stop(m_search);
    MOMException::setFromMOMError(err, ec);
}

void MOMGuideSearch::update()
{
    mom_guide_search_update(m_search);
}

void MOMGuideSearch::clear()
{
    mom_guide_search_clear(m_search);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
