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

#include "config.h"

#if ENABLE(RMAI)

#include "GalioConfigurationItem.h"

#include "UtilsMOM.h"

extern "C" {
#include "layers.h"
#include "confman.h"
}

namespace WebCore {

PassRefPtr<GalioConfigurationItem> GalioConfigurationItem::create(const String& name)
{
    return adoptRef(new GalioConfigurationItem(name));
}

GalioConfigurationItem::GalioConfigurationItem(const String& name)
    : m_name(name)
{
}

static confman_dataelement *dom_config__lookup(const String& name,
                                               confman_datatype_tag *type)
{
    confman_dataelement *config;
    layers_string lname = UtilsMOM::LayersStringFromString(name);

    if (lname )
    {
        const char *tag = layers_string_lock(lname);
        config = confman_lookup_text( tag, type );
        layers_string_unlock(lname);
        layers_string_dispose(lname);
    }
    else
    {
        config = NULL;
        *type = confman_datatype_END;
    }

    return config;
}


int GalioConfigurationItem::type(ExceptionCode& ec) const
{
    confman_datatype_tag type = confman_datatype_END;
    if (!dom_config__lookup(m_name, &type))
    {
        ec = NOT_FOUND_ERR;
    }
    return type;
}

int GalioConfigurationItem::length() const
{
    confman_datatype_tag type;
    confman_dataelement *config = dom_config__lookup(m_name, &type);

    if ( CONFMAN_DATA(config, signed_decimal_list) == NULL )
    {
        return 0;
    }

    int count = 0;
    switch ( type )
    {
        case confman_datatype_SIGNED_DECIMAL_LIST:
            count = CONFMAN_DATA(config,signed_decimal_list)->count;
            break;
        case confman_datatype_UNSIGNED_DECIMAL_LIST:
            count = CONFMAN_DATA(config,unsigned_decimal_list)->count;
            break;
        case confman_datatype_HEX_LIST:
            count = CONFMAN_DATA(config,hex_list)->count;
            break;
        case confman_datatype_COLOUR_LIST:
            count = CONFMAN_DATA(config,colour_list)->count;
            break;
        case confman_datatype_STRING_LIST:
            count = CONFMAN_DATA(config,string_list)->count;
            break;
        default:
            break;
    }

    return count;
}

String GalioConfigurationItem::value() const
{
    confman_datatype_tag type;
    confman_dataelement *config = dom_config__lookup(m_name, &type);
    char num[16];

    switch (type)
    {
    case confman_datatype_BOOLEAN:
        num[0] = CONFMAN_DATA(config,boolean)?'1':'0';
        num[1] = '\0';
        break;
    case confman_datatype_SIGNED_DECIMAL:
        sprintf( num, "%d", CONFMAN_DATA(config,signed_decimal) );
        break;
    case confman_datatype_UNSIGNED_DECIMAL:
        sprintf( num, "%u", CONFMAN_DATA(config,signed_decimal) );
        break;
    case confman_datatype_HEX:
        sprintf( num, "0x%x", CONFMAN_DATA(config,hex) );
        break;
    case confman_datatype_COLOUR:
        sprintf( num, "0x%x", layers_colour_INVERT_ALPHA_COLOUR(CONFMAN_DATA(config,colour)) );
        break;
    case confman_datatype_CHARACTER:
        num[0] = CONFMAN_DATA(config,character);
        num[1] = '\0';
        break;

    case confman_datatype_STRING:
        return String::fromUTF8(CONFMAN_DATA(config, string));

    case confman_datatype_LAYERS_STRING:
        return UtilsMOM::StringFromLayersString(CONFMAN_DATA(config,lstring));

    default:
        num[0] ='\0';
        break;
    }

    return String(num);
}

int GalioConfigurationItem::item( int index )
{
    return 0;
}

} // namespace WebCore

#endif // ENABLE(RMAI)
