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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMParentalRatingSchemeCollection.h"
#include "MOMParentalRatingScheme.h"
#include "MOMContentItem.h"
#include "MOMStringCollection.h"

#include "UtilsMOM.h"

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

namespace WebCore {

PassRefPtr<MOMParentalRatingSchemeCollection> MOMParentalRatingSchemeCollection::create()
{
    return adoptRef(new MOMParentalRatingSchemeCollection());
}

MOMParentalRatingSchemeCollection::MOMParentalRatingSchemeCollection()
{
}

int MOMParentalRatingSchemeCollection::length() const
{
    return mom_parentalcontrols_scheme_count();
}

bool MOMParentalRatingSchemeCollection::isPINEntryLocked() const
{
    return mom_parentalcontrols_is_pin_entry_locked();
}

bool MOMParentalRatingSchemeCollection::isPurchasePINEntryLocked() const
{
    return mom_parentalcontrols_is_purchase_pin_entry_locked();
}

PassRefPtr<MOMParentalRatingScheme> MOMParentalRatingSchemeCollection::item( int index )
{
    mom_parentalrating_scheme scheme = mom_parentalcontrols_scheme_get(index);
    return scheme ? MOMParentalRatingScheme::create(scheme) : nullptr;
}

int MOMParentalRatingSchemeCollection::setPIN( const String & oldPcPIN, const String & newPcPIN )
{
    layers_string loldPIN = UtilsMOM::LayersStringFromString(oldPcPIN);
    layers_string lnewPIN = UtilsMOM::LayersStringFromString(newPcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_pin(loldPIN, lnewPIN);

    layers_string_dispose(loldPIN);
    layers_string_dispose(lnewPIN);

    return result;
}

int MOMParentalRatingSchemeCollection::setPurchasePIN( const String & oldPurchasePIN, const String & newPurchasePIN )
{
    layers_string loldPIN = UtilsMOM::LayersStringFromString(oldPurchasePIN);
    layers_string lnewPIN = UtilsMOM::LayersStringFromString(newPurchasePIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_purchase_pin(loldPIN, lnewPIN);

    layers_string_dispose(loldPIN);
    layers_string_dispose(lnewPIN);

    return result;
}

int MOMParentalRatingSchemeCollection::setPINEnable( const String & pcPIN, bool enable )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_pin_enable(lpin, enable);

    layers_string_dispose(lpin);

    return result;
}

int MOMParentalRatingSchemeCollection::setPurchasePINEnable( const String & pcPIN, bool enable )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_purchase_pin_enable(lpin, enable);

    layers_string_dispose(lpin);

    return result;
}

bool MOMParentalRatingSchemeCollection::getPINEnable()
{
    return mom_parentalcontrols_get_pin_enable();
}

bool MOMParentalRatingSchemeCollection::getPurchasePINEnable()
{
    return mom_parentalcontrols_get_purchase_pin_enable();
}

int MOMParentalRatingSchemeCollection::unlockWithPIN( const String & pcPIN, MOMContentItem* target, int duration )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_unlock_with_pin(lpin, target ? target->itemAsItem() : NULL, duration*60);

    layers_string_dispose(lpin);

    return result;
}

int MOMParentalRatingSchemeCollection::verifyPIN( const String & pcPIN )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_verify_pin(lpin);

    layers_string_dispose(lpin);

    return result;
}

int MOMParentalRatingSchemeCollection::verifyPurchasePIN( const String & purchasePIN )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(purchasePIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_verify_purchase_pin(lpin);

    layers_string_dispose(lpin);

    return result;
}

int MOMParentalRatingSchemeCollection::setBlockUnrated( const String & pcPIN, bool block )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_block_unrated(lpin, block);

    layers_string_dispose(lpin);

    return result;
}

bool MOMParentalRatingSchemeCollection::getBlockUnrated()
{
    return mom_parentalcontrols_get_block_unrated();
}

int MOMParentalRatingSchemeCollection::setShowTitlesWhenLocked( const String & pcPIN, bool show, bool suppressEvents )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_show_titles(lpin, show, suppressEvents);

    layers_string_dispose(lpin);

    return result;
}

bool MOMParentalRatingSchemeCollection::getShowTitlesWhenLocked()
{
    return mom_parentalcontrols_get_show_titles();
}

int MOMParentalRatingSchemeCollection::setParentalControlEnable( const String & pcPIN, bool enable )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_parental_control(lpin, enable);

    layers_string_dispose(lpin);

    return result;
}

bool MOMParentalRatingSchemeCollection::getParentalControlEnable()
{
    return !mom_parentalcontrols_get_global_unlock();
}

int MOMParentalRatingSchemeCollection::setContentAdvisories( const String & pcPIN, const String & advisories )
{
    layers_string lpin = UtilsMOM::LayersStringFromString(pcPIN);
    layers_string ladvisories = UtilsMOM::LayersStringFromString(advisories);

    mom_parentalcontrols_pin_result result = mom_parentalcontrols_set_content_advisories(lpin, ladvisories);

    layers_string_dispose(lpin);
    layers_string_dispose(ladvisories);

    return result;
}

PassRefPtr<MOMStringCollection> MOMParentalRatingSchemeCollection::getContentAdvisories()
{
    return MOMStringCollection::create(mom_parentalcontrols_get_content_advisories());
}

} // namespace WebCore

#endif // ENABLE(RMAI)
