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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMVODAsset.h"
#include "MOMPlayer.h"
#include "MOMStringCollection.h"

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

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

namespace WebCore {

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

void MOMVODAsset::connect( MOMPlayer* player, int mode, int offset, ExceptionCode& ec )
{
    if ( mode >= mom_stream_connect_mode__MAX )
    {
        MOMException::setFromMOMError(mom_err_INVALID, ec);
        return;
    }

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

void MOMVODAsset::connect( MOMPlayer* player, ExceptionCode& ec )
{
	return connect(player, 0, ec);
}

void MOMVODAsset::connect( MOMPlayer* player, int mode, ExceptionCode& ec )
{
	return connect(player, mode, 0, ec);
}

int MOMVODAsset::purchase( const String & pin, ExceptionCode& ec )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pin);
    mom_error err = mom_asset_item_purchase(itemAsAssetItem(), lpin);
    layers_string_dispose(lpin);
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
        MOMException::setFromMOMError(err, ec);
    return MOMException::codeFromMOMError(err);
}

int MOMVODAsset::purchase(ExceptionCode& ec )
{
	return purchase(String(), ec);
}

void MOMVODAsset::cancel(ExceptionCode& ec)
{
    mom_error err = mom_asset_item_cancel(itemAsAssetItem());
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
        MOMException::setFromMOMError(err, ec);
}

void MOMVODAsset::setBookmark( int bookmark, ExceptionCode& ec )
{
    mom_error err = mom_asset_item_set_bookmark(itemAsAssetItem(), bookmark);
    if (err == mom_err_BUSY)
        MOMException::setFromMOMError(err, ec);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
