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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMRenderer.h"
#include "MOMComponentCollection.h"
#include "MOMComponent.h"
#include "MOMStream.h"

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

namespace WebCore {

PassRefPtr<MOMRenderer> MOMRenderer::create(ScriptExecutionContext* context, mom_renderer renderer, bool isPlayer)
{
    return adoptRef(new MOMRenderer(context, renderer, isPlayer));
}

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

MOMRenderer::MOMRenderer(ScriptExecutionContext* context, mom_renderer renderer, bool isPlayer)
    : ActiveDOMObject(context),
      m_renderer(mom_renderer_keep(renderer)),
      m_isPlayer(isPlayer)
{
    if (!isPlayer) mom_renderer_set_events(renderer, this, MOMRenderer::eventDispatcher);
}

MOMRenderer::~MOMRenderer()
{
    if (!m_isPlayer) mom_renderer_set_events(renderer(), NULL, 0);
    mom_renderer_dispose(renderer());
}

EventTargetInterface MOMRenderer::eventTargetInterface() const
{
    return MOMRendererEventTargetInterfaceType;
}

int MOMRenderer::offset() const
{
    return mom_renderer_get_offset(renderer());
}

int MOMRenderer::duration() const
{
    return mom_renderer_get_duration(renderer());
}

void MOMRenderer::connect( MOMStream* stream, int mode, ExceptionCode& ec )
{
    mom_error err = mom_renderer_connect(renderer(), stream->stream(), static_cast<mom_stream_connect_mode>(mode), NULL );
    MOMException::setFromMOMError(err, ec);
}

void MOMRenderer::disconnect()
{
    mom_renderer_dispose(renderer());
}

void MOMRenderer::play( int speed, ExceptionCode& ec )
{
    mom_error err = mom_renderer_play(renderer(), speed);
    MOMException::setFromMOMError(err, ec);
}

void MOMRenderer::rendererStop()
{
    mom_renderer_stop(renderer());
}

void MOMRenderer::seek( int offset, int reference, ExceptionCode& ec )
{
    MDASeekReference ref = MDASeekSet;
    mom_error err = static_cast<mom_error>(ERR_NONE);
    switch (reference)
    {
    case 0:
        ref = MDASeekSet;
        break;
    case 1:
        ref = MDASeekCurr;
        break;
    case 2:
        ref = MDASeekEnd;
        break;
    default:
        err = mom_err_INVALID;
        break;
    }
    if (err == ERR_NONE)
        err = mom_renderer_seek(renderer(), offset, ref);
    MOMException::setFromMOMError(err, ec);
}

PassRefPtr<MOMComponentCollection> MOMRenderer::getComponents( int componentType )
{
    return MOMComponentCollection::create(mom_renderer_get_components(renderer(), componentType, FALSE));
}

void MOMRenderer::selectComponent( MOMComponent* component )
{
    mom_component_select(component->component());
}

void MOMRenderer::removeComponent( MOMComponent* component )
{
    mom_component_unselect(component->component());
}

} // namespace WebCore

#endif // ENABLE(RMAI)
