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

#include "config.h"

#if ENABLE(RMAI)

#include "GalioConfigurationComponent.h"
#include "GalioConfigurationItem.h"
#include "GalioConfigurationEvent.h"

#include "UtilsMOM.h"

extern "C" {
#include "layers.h"
#include "confman.h"
extern void layers_log_printf( const char *fmt, ... );
}

namespace WebCore {

PassRefPtr<GalioConfigurationComponent> GalioConfigurationComponent::create(ScriptExecutionContext* context, confman_dynamic_component comp)
{
    return adoptRef(new GalioConfigurationComponent(context, comp));
}

GalioConfigurationComponent::GalioConfigurationComponent(ScriptExecutionContext* context, confman_dynamic_component comp)
    : ActiveDOMObject(context),
      m_component(comp)
{
}

GalioConfigurationComponent::~GalioConfigurationComponent()
{
}

EventTargetInterface GalioConfigurationComponent::eventTargetInterface() const
{
    return GalioConfigurationComponentEventTargetInterfaceType;
}

void GalioConfigurationComponent::addItem( const String & name, int type, const String & defaultValue, ExceptionCode& ec )
{
    layers_string lname = UtilsMOM::LayersStringFromString(name);
    layers_string lvalue = UtilsMOM::LayersStringFromString(defaultValue);
    confman_datatype_tag datatype = type ? static_cast<confman_datatype_tag>(type) : confman_datatype_STRING;
    if ( !confman_dynamic_component_add_item(m_component, lname, datatype, lvalue))
    {
        ec = SYNTAX_ERR;
    }
    layers_string_dispose(lname);
    layers_string_dispose(lvalue);
}

void GalioConfigurationComponent::declare(ExceptionCode& ec)
{
    if ( !confman_dynamic_component_declare(m_component, confman_declare_ALL))
    {
        ec = SYNTAX_ERR;
    }
}

void GalioConfigurationComponent::restore()
{
    confman_dynamic_component_restore(m_component);
}

#if CONFMAN_USE_ACCESS_FUNCTIONS
static void config_changed(void * handle, char const * item)
{
    layers_log_printf("config_changed handle %p item %s\n", handle, item?item:"<null>");
    
    if ( handle == NULL || item == NULL ) return;

    GalioConfigurationComponent* object = static_cast<GalioConfigurationComponent*>(handle);

    String itemName = String(item);
    RefPtr<GalioConfigurationEvent> event = GalioConfigurationEvent::create(itemName, GalioConfigurationItem::create(itemName));

    object->dispatchEvent(event.release());
}
#endif

bool GalioConfigurationComponent::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
{
    bool didHaveListeners = hasEventListeners();
    bool rc = EventTarget::addEventListener(eventType, listener, useCapture);
    if (!didHaveListeners && hasEventListeners())
    {
#if CONFMAN_USE_ACCESS_FUNCTIONS
        confman_register_notification_callback(
            confman_dynamic_component_prefix(m_component),
            config_changed,
            this);
#endif
    }

    return rc;
}

bool GalioConfigurationComponent::removeEventListener(const AtomicString& eventType, EventListener* listener, bool useCapture)
{
    bool didHaveListeners = hasEventListeners();
    bool rc = EventTarget::removeEventListener(eventType, listener, useCapture);

    if (didHaveListeners && !hasEventListeners())
    {
#if CONFMAN_USE_ACCESS_FUNCTIONS
        confman_unregister_notification_callback(
            confman_dynamic_component_prefix(m_component),
            config_changed,
            this);
#endif
    }

    return rc;
}

} // namespace WebCore

#endif // ENABLE(RMAI)
