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

#include "config.h"

#if ENABLE(RMAI)

#include "MOMLocalSystem.h"
#include "MOMOutputCollection.h"
#include "MOMNetworkInterfaceCollection.h"
#include "MOMSatelliteManager.h"
#include "MOMCable.h"

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

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

namespace WebCore {

PassRefPtr<MOMLocalSystem> MOMLocalSystem::create(ScriptExecutionContext* context)
{
    return adoptRef(new MOMLocalSystem(context));
}

MOMLocalSystem::MOMLocalSystem(ScriptExecutionContext* context)
    : m_context(context)
{
}

String MOMLocalSystem::softwareVersion() const
{
    layers_string version = mom_localsystem_software_version();
    return UtilsMOM::StringFromLayersString(version);
}

String MOMLocalSystem::hardwareVersion() const
{
    layers_string version = mom_localsystem_hardware_version();
    return UtilsMOM::StringFromLayersString(version);
}

bool MOMLocalSystem::systemReady() const
{
    return mom_localsystem_get_system_ready();
}

void MOMLocalSystem::setSystemReady(bool systemReady)
{
    mom_localsystem_set_system_ready(systemReady);
}

bool MOMLocalSystem::standbyState() const
{
    return mom_localsystem_get_standby_mode();
}

void MOMLocalSystem::setStandbyState(bool standbyState)
{
    mom_localsystem_set_standby_mode(standbyState);
}

bool MOMLocalSystem::mute() const
{
    return mom_localsystem_get_mute();
}

void MOMLocalSystem::setMute(bool mute)
{
    mom_localsystem_set_mute(mute);
}

int MOMLocalSystem::volume() const
{
    return mom_localsystem_get_volume();
}

void MOMLocalSystem::setVolume(int volume, ExceptionCode& ec)
{
    if ( volume < 0 || volume > 100 )
        MOMException::setFromMOMError(mom_err_INVALID, ec);
    else
        mom_localsystem_set_volume(volume);
}

String MOMLocalSystem::macrovisionModeByte() const
{
    layers_string mode = mom_localsystem_get_macrovision_mode_byte();
    return UtilsMOM::StringFromLayersString(mode);
}

String MOMLocalSystem::macrovisionAPS() const
{
    layers_string aps = mom_localsystem_get_macrovision_aps();
    return UtilsMOM::StringFromLayersString(aps);
}

String MOMLocalSystem::serialNumber() const
{
    layers_string serial = mom_localsystem_get_serial_number();
    return UtilsMOM::StringFromLayersString(serial);
}

int MOMLocalSystem::subtitleType() const
{
    return mom_localsystem_get_subtitle_type();
}

int MOMLocalSystem::tvStandard() const
{
    return mom_localsystem_get_tv_standard();
}

String MOMLocalSystem::boxName() const
{
    layers_string name = mom_localsystem_get_box_name();
    return UtilsMOM::StringFromLayersString(name);
}

void MOMLocalSystem::setBoxName(const String & boxName)
{
    layers_string lname = UtilsMOM::LayersStringFromString(boxName);
    mom_localsystem_set_box_name(lname);
    layers_string_dispose(lname);
}

String MOMLocalSystem::boxId() const
{
    layers_string name = mom_localsystem_get_box_id();
    return UtilsMOM::StringFromLayersString(name);
}

MOMOutputCollection* MOMLocalSystem::outputs() const
{
     if (!m_outputs)
        m_outputs = MOMOutputCollection::create();
    return m_outputs.get();
}

MOMNetworkInterfaceCollection* MOMLocalSystem::networkInterfaces() const
{
    if (!m_networkInterfaces)
        m_networkInterfaces = MOMNetworkInterfaceCollection::create(m_context);
    return m_networkInterfaces.get();
}

bool MOMLocalSystem::dvrEnabled() const
{
    return mom_localsystem_get_dvr_enabled();
}

bool MOMLocalSystem::earlyVideo() const
{
    return !mom_localsystem_get_system_ready() && mom_early_video_is_enabled();
}

MOMSatelliteManager* MOMLocalSystem::satelliteManager() const
{
     if (!m_satelliteManager)
        m_satelliteManager = MOMSatelliteManager::create(m_context);
    return m_satelliteManager.get();
}

MOMCable* MOMLocalSystem::smatvManager() const
{
     if (!m_smatvManager)
        m_smatvManager = MOMCable::create();
    return m_smatvManager.get();
}

void MOMLocalSystem::sendEASEvent()
{
    mom_localsystem_send_eas_event();
}

int MOMLocalSystem::setMacrovisionModeByte( const String & mode )
{
    layers_string lmode = UtilsMOM::LayersStringFromString(mode);
    mom_error err = mom_localsystem_set_macrovision_mode_byte(lmode);
    layers_string_dispose(lmode);
    return err;
}

int MOMLocalSystem::setMacrovisionAPS( const String & aps )
{
    layers_string laps = UtilsMOM::LayersStringFromString(aps);
    mom_error err = mom_localsystem_set_macrovision_aps(laps);
    layers_string_dispose(laps);
    return err;
}

String MOMLocalSystem::getProperty( const String & property )
{
    layers_string lprop = UtilsMOM::LayersStringFromString(property);
    layers_string lvalue = mom_localsystem_get_property(lprop);
    String value = UtilsMOM::StringFromLayersString(lvalue);
    layers_string_dispose(lprop);
    layers_string_dispose(lvalue);
    return value;
}

int MOMLocalSystem::setProperty( const String & property, const String & value )
{
    layers_string lprop = UtilsMOM::LayersStringFromString(property);
    layers_string lvalue = UtilsMOM::LayersStringFromString(value);
    mom_error err = mom_localsystem_set_property(lprop, lvalue);
    layers_string_dispose(lprop);
    layers_string_dispose(lvalue);
    return err;
}

void MOMLocalSystem::notifyEvent( int notificationType, int data )
{
    mom_localsystem_notify_event( static_cast<mom_notification_type>(notificationType), data );
}

} // namespace WebCore

#endif // ENABLE(RMAI)
