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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMException.h"
#include "MOMVODService.h"

#include "JSMOMVODService.h"
#include "UtilsMOM.h"

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

using namespace JSC;

namespace WebCore {
    
JSValue JSMOMVODService::price(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    layers_string price;
    if (mom_vod_asset_get_price(mom_vod_service_get_vod_asset(imp.itemAsVODService()), &price) == ERR_NONE)
        return jsString(exec->vm(), UtilsMOM::StringFromLayersString(price));
    return jsUndefined();
}

JSValue JSMOMVODService::previewUri(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    layers_string previewUri;
    if (mom_vod_asset_get_preview_uri(mom_vod_service_get_vod_asset(imp.itemAsVODService()), &previewUri) == ERR_NONE)
        return jsString(exec->vm(), UtilsMOM::StringFromLayersString(previewUri));
    return jsUndefined();
}

JSValue JSMOMVODService::rentalPeriod(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    uint32_t rental_period;
    if (mom_vod_asset_get_rental_period(mom_vod_service_get_vod_asset(imp.itemAsVODService()), &rental_period) == ERR_NONE)
        return jsNumber(rental_period);
    return jsUndefined();
}

JSValue JSMOMVODService::expirationDate(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    uint32_t date;
    if (mom_asset_item_get_expiry_date((mom_asset_item)mom_vod_service_get_vod_asset(imp.itemAsVODService()), &date) == ERR_NONE)
        return jsNumber(date);
    return jsUndefined();
}

JSValue JSMOMVODService::expiryDate(ExecState* exec) const
{
    return expirationDate(exec);
}

JSValue JSMOMVODService::purchaseDate(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    uint32_t date;
    if (mom_vod_asset_get_purchase_date(mom_vod_service_get_vod_asset(imp.itemAsVODService()), &date) == ERR_NONE)
        return jsNumber(date);
    return jsUndefined();
}

JSValue JSMOMVODService::purchased(ExecState* exec) const
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    mom_asset_item_purchase_state purchased;
    if (mom_asset_item_get_purchased((mom_asset_item)mom_vod_service_get_vod_asset(imp.itemAsVODService()), &purchased))
        return jsBoolean(purchased == mom_asset_item_PURCHASE_COMPLETED);
    return jsUndefined();
}

JSValue JSMOMVODService::isReady(ExecState* exec)
{
    MOMVODService& imp = static_cast<MOMVODService&>(wrapped());
    BOOL ready;
    mom_error err = mom_vod_service_is_ready(imp.itemAsVODService(), &ready);
    if (err == ERR_NONE)
        return jsBoolean(ready);
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        ExceptionCode ec;
        MOMException::setFromMOMError(err, ec);
        setDOMException(exec, ec);
    }
    return jsUndefined();
}
    
} // namespace WebCore

#endif // ENABLE(RMAI)
