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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMException.h"
#include "MOMProgramme.h"

#include "JSMOMProgramme.h"

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

using namespace JSC;

namespace WebCore {
    
JSValue JSMOMProgramme::purchasableAsset(ExecState* exec) const
{
    MOMProgramme& imp = static_cast<MOMProgramme&>(wrapped());

    mom_purchasable_asset asset;
    mom_error err = mom_programme_extension_get_purchasable_asset(imp.itemAsProgramme(), &asset);

    if (err == ERR_NONE)
        return toJS(exec, globalObject(), MOMContentItem::create(imp.scriptExecutionContext(), (mom_content_item)asset));

    if (err == mom_err_NOTPRESENT)
        return jsUndefined();

    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        ExceptionCode ec = 0;
        MOMException::setFromMOMError(err, ec);
        setDOMException(exec, ec);
    }
    return jsNull();
}

JSValue JSMOMProgramme::bookmark(ExecState* exec) const
{
    MOMProgramme& imp = static_cast<MOMProgramme&>(wrapped());

    int32_t bookmark;
    mom_error err = mom_programme_get_bookmark(imp.itemAsProgramme(), &bookmark);

    if (err == ERR_NONE)
        return jsNumber(bookmark);

    ExceptionCode ec = 0;
    MOMException::setFromMOMError(err, ec);
    setDOMException(exec, ec);
    return jsUndefined();
}

} // namespace WebCore

#endif // ENABLE(RMAI)
