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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMPlayer.h"
#include "MOMRenderer.h"
#include "MOMConnectionInfo.h"
#include "MOMContentItem.h"

#include "MOMException.h"
#include "UtilsMOM.h"
#include "MOMEvent.h"

namespace WebCore {

PassRefPtr<MOMPlayer> MOMPlayer::create(ScriptExecutionContext* context)
{
    RefPtr<MOMPlayer> player;
    mom_player playerh;
    mom_error err = mom_player_create(true, &playerh);
    if (err == ERR_NONE)
    {
        player = MOMPlayer::create(context, playerh);
        mom_player_dispose(playerh);
    }

    return player;
}

PassRefPtr<MOMPlayer> MOMPlayer::create(ScriptExecutionContext* context, mom_player player)
{
    return adoptRef(new MOMPlayer(context, player));
}

mom_error MOMPlayer::eventDispatcher(mom_event_t which, void* target, mom_event_params* params, unsigned int queue)
{
    return MOMEvent::dispatchMOMEvent(which, static_cast<MOMPlayer*>(target), params, queue);
}
    
MOMPlayer::MOMPlayer(ScriptExecutionContext* context, mom_player player)
    : MOMRenderer(context, mom_player_renderer(player), true),
      m_player(mom_player_keep(player))
{
    mom_player_set_events(player, this, MOMPlayer::eventDispatcher);
}

MOMPlayer::~MOMPlayer()
{
    mom_player_set_events(m_player, NULL, 0);
    mom_player_dispose(m_player);
}

int MOMPlayer::offset() const
{
    return mom_player_get_offset(player());
}

int MOMPlayer::duration() const
{
    return mom_player_get_duration(player());
}

bool MOMPlayer::locked() const
{
    return mom_player_is_locked(player());
}

bool MOMPlayer::blanked() const
{
    return mom_player_is_blanked(player());
}

bool MOMPlayer::enableVideoMode() const
{
    return mom_player_video_mode_enable_get(player());
}

void MOMPlayer::setEnableVideoMode(bool enableVideoMode)
{
    mom_player_video_mode_enable_set(player(), enableVideoMode);
}

bool MOMPlayer::muteOnConnect() const
{
    return mom_renderer_mute_on_connect_get(mom_player_renderer(player()));
}

void MOMPlayer::setMuteOnConnect(bool muteOnConnect)
{
    mom_renderer_mute_on_connect_set(mom_player_renderer(player()), muteOnConnect);
}

PassRefPtr<MOMContentItem> MOMPlayer::currentConnection() const
{
    return MOMContentItem::create(scriptExecutionContext(), mom_player_get_content(player()));
}

int MOMPlayer::connect( const String & source, int mode, int offset, ExceptionCode& ec )
{
    if ( mode >= mom_stream_connect_mode__MAX )
    {
        MOMException::setFromMOMError(mom_err_INVALID, ec);
        return 0;
    }

    layers_string lsource = UtilsMOM::LayersStringFromString(source);
    mom_error err = mom_player_connect(player(), lsource, static_cast<mom_stream_connect_mode>(mode), offset);
    layers_string_dispose(lsource);

    MOMException::setFromMOMError(err, ec);

    return 0;
}
    
int MOMPlayer::connect( MOMContentItem* source, int mode, int offset, ExceptionCode& ec )
{
    if (!source || mode >= mom_stream_connect_mode__MAX)
    {
        MOMException::setFromMOMError(mom_err_INVALID, ec);
        return 0;
    }

    mom_error err = mom_player_connect_content_item(player(), source->itemAsItem(), static_cast<mom_stream_connect_mode>(mode), offset);
    MOMException::setFromMOMError(err, ec);

    return 0;
}

void MOMPlayer::disconnect()
{
    mom_player_disconnect(player());
}

void MOMPlayer::play( int speed, ExceptionCode& ec )
{
    mom_error err = mom_player_play(player(), speed);
    MOMException::setFromMOMError(err, ec);
}

void MOMPlayer::rendererStop()
{
    mom_player_stop(player());
}

void MOMPlayer::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_player_seek(player(), offset, ref);
    MOMException::setFromMOMError(err, ec);
}

void MOMPlayer::seek( int offset, ExceptionCode& ec )
{
	seek(offset, 0, ec);
}

void MOMPlayer::sendAccessEvent()
{
    mom_player_resend_parental_controls(player());
}

PassRefPtr<MOMConnectionInfo> MOMPlayer::checkConnect( MOMContentItem* asset )
{
    MDAConnectionInfo info;
    mom_player_connect_check(player(), asset ? asset->itemAsItem() : NULL, FALSE, &info);
    return MOMConnectionInfo::create(info);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
