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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMPurchasableAsset.h"
#include "MOMProgramme.h"
#include "UtilsMOM.h"
#include "MOMException.h"

extern "C" {
#include "cdf/mom_ppv.h"
}

namespace WebCore {

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

String MOMPurchasableAsset::price() const
{
    layers_string s;
    mom_error err = mom_purchasable_asset_get_price(itemAsPurchasableAsset(), &s);
    return err ? String() : UtilsMOM::StringFromLayersStringAndDispose(s);
}

int MOMPurchasableAsset::purchasable() const
{
    mom_purchasable_asset_status n;
    mom_error err = mom_purchasable_asset_get_purchasable(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::purchaseWindowStart() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_purchase_window_start(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::purchaseWindowEnd() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_purchase_window_end(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

bool MOMPurchasableAsset::advertisable() const
{
    BOOL n;
    mom_error err = mom_purchasable_asset_get_advertisable(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::advertisingWindowStart() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_advertising_window_start(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::advertisingWindowEnd() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_advertising_window_end(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

bool MOMPurchasableAsset::previewable() const
{
    BOOL n;
    mom_error err = mom_purchasable_asset_get_previewable(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::previewWindowStart() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_preview_window_start(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::previewWindowEnd() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_preview_window_end(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

bool MOMPurchasableAsset::cancellable() const
{
    BOOL n;
    mom_error err = mom_purchasable_asset_get_cancellable(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::cancelWindowEnd() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_cancel_window_end(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

String MOMPurchasableAsset::previewUri() const
{
    layers_string s;
    mom_error err = mom_purchasable_asset_get_preview_uri(itemAsPurchasableAsset(), &s);
    return err ? String() : UtilsMOM::StringFromLayersStringAndDispose(s);
}

String MOMPurchasableAsset::contactTelephone() const
{
    layers_string s;
    mom_error err = mom_purchasable_asset_get_contact_telephone(itemAsPurchasableAsset(), &s);
    return err ? String() : UtilsMOM::StringFromLayersStringAndDispose(s);
}

int MOMPurchasableAsset::contactTelephoneWindowStart() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_contact_telephone_window_start(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::purchased() const
{
    mom_asset_item_purchase_state n;
    mom_error err = mom_purchasable_asset_get_purchased(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

int MOMPurchasableAsset::purchaseDate() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_purchase_date(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}

String MOMPurchasableAsset::purchaseDiagnostic() const
{
    layers_string s;
    mom_error err = mom_purchasable_asset_get_purchase_diagnostic(itemAsPurchasableAsset(), &s);
    return err ? String() : UtilsMOM::StringFromLayersStringAndDispose(s);
    return String();
}

String MOMPurchasableAsset::cancelDiagnostic() const
{
    layers_string s;
    mom_error err = mom_purchasable_asset_get_cancel_diagnostic(itemAsPurchasableAsset(), &s);
    return err ? String() : UtilsMOM::StringFromLayersStringAndDispose(s);
}

PassRefPtr<MOMProgramme> MOMPurchasableAsset::programme(ExceptionCode& ec) const
{
    mom_programme programme;
    mom_error err = mom_purchasable_asset_get_programme(itemAsPurchasableAsset(), &programme);
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        MOMException::setFromMOMError(err, ec);
        return nullptr;
    }
    return err ? nullptr : MOMProgramme::createAdopt(scriptExecutionContext(), programme);
}

int MOMPurchasableAsset::purchaseTypes() const
{
    uint32_t n;
    mom_error err = mom_purchasable_asset_get_purchase_types(itemAsPurchasableAsset(), &n);
    return err ? 0 : n;
}


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

int MOMPurchasableAsset::cancel(ExceptionCode& ec)
{
    mom_error err = mom_purchasable_asset_cancel(itemAsPurchasableAsset());
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        MOMException::setFromMOMError(err, ec);
        return 0;
    }
    return MOMException::codeFromMOMError(err);
}

bool MOMPurchasableAsset::isReady(ExceptionCode& ec)
{
    BOOL is_ready = FALSE;
    mom_error err = mom_purchasable_asset_is_ready(itemAsPurchasableAsset(), &is_ready);
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        MOMException::setFromMOMError(err, ec);
        return 0;
    }
    return is_ready;
}

PassRefPtr<MOMProgramme> MOMPurchasableAsset::findNextShowing(ExceptionCode& ec)
{
    mom_programme programme;
    mom_error err = mom_purchasable_asset_find_next_showing(itemAsPurchasableAsset(), &programme);
    if (err == mom_err_BUSY || err == mom_err_NOTAUTHORISED)
    {
        MOMException::setFromMOMError(err, ec);
        return nullptr;
    }
    return err ? nullptr : MOMProgramme::createAdopt(scriptExecutionContext(), programme);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
