#include "ocpp21_datatype.h"

//2.1
int AbsolutePriceScheduleType_encode(cJSON *parent, const char* name, AbsolutePriceScheduleType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "timeAnchor", value->timeAnchor);
                cJSON_AddNumberToObject(object, "priceScheduleID", value->priceScheduleID);
                if (value->priceScheduleDescription_isUsed) { cJSON_AddStringToObject(object, "priceScheduleDescription", value->priceScheduleDescription); }
                if (value->currency_isUsed) { cJSON_AddStringToObject(object, "currency", value->currency); }
                if (value->language_isUsed) { cJSON_AddStringToObject(object, "language", value->language); }
                if (value->priceAlgorithm_isUsed) { cJSON_AddStringToObject(object, "priceAlgorithm", value->priceAlgorithm); }
                return 0;
            }
        }
    }
    return -1;
}

//2.2
int ACChargingParametersType_encode(cJSON *parent, const char* name, ACChargingParametersType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddNumberToObject(object, "energyAmount", value->energyAmount);
                cJSON_AddNumberToObject(object, "evMinCurrent", value->evMinCurrent);
                cJSON_AddNumberToObject(object, "evMaxCurrent", value->evMaxCurrent);
                cJSON_AddNumberToObject(object, "evMaxVoltage", value->evMaxVoltage);
                return 0;
            }
        }
    }
    return -1;
}

//2.3
int AdditionalInfoType_encode(cJSON *parent, const char* name, AdditionalInfoType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "additionalIdToken", value->additionalIdToken);
                cJSON_AddStringToObject(object, "type", value->type);
                return 0;
            }
        }
    }
    return -1;
}

//2.81
int RationalNumberType_encode(cJSON *parent, const char* name, RationalNumberType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddNumberToObject(object, "exponent", value->exponent);
                cJSON_AddNumberToObject(object, "value", value->value);
                return 0;
            }
        }
    }
    return -1;
}

//2.4
int AdditionalSelectedServicesType_encode(cJSON *parent, const char* name, AdditionalSelectedServicesType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "serviceName", value->serviceName);
                RationalNumberType_encode(object, "serviceFee", &value->serviceFee);
                return 0;
            }
        }
    }
    return -1;
}

//2.5
int AddressType_encode(cJSON *parent, const char* name, AddressType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "name", value->name);
                cJSON_AddStringToObject(object, "address1", value->address1);
                if (value->address2_isUsed) cJSON_AddStringToObject(object, "address2", value->address2);
                cJSON_AddStringToObject(object, "city", value->city);
                if (value->postcode_isUsed) cJSON_AddStringToObject(object, "postcode", value->postcode);
                cJSON_AddStringToObject(object, "country", value->country);
                return 0;
            }
        }
    }
    return -1;
}

//2.6
int APNType_encode(cJSON *parent, const char* name, APNType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "apn", value->apn);
                if (value->anpUserName_isUsed) cJSON_AddStringToObject(object, "anpUserName", value->anpUserName);
                if (value->anpPassword_isUsed) cJSON_AddStringToObject(object, "anpPassword", value->anpPassword);
                if (value->simPin_isUsed) cJSON_AddStringToObject(object, "simPin", value->simPin);
                if (value->preferredNetwork_isUsed) cJSON_AddStringToObject(object, "preferredNetwork", value->preferredNetwork);
                if (value->useOnlyPreferredNetwork_isUsed) cJSON_AddBoolToObject(object, "useOnlyPreferredNetwork", value->useOnlyPreferredNetwork);
                cJSON_AddStringToObject(object, "apnAuthentication", APNAuthenticationEnumType_toString(value->apnAuthentication));
                return 0;
            }
        }
    }
    return -1;
}

//-------------------------------------------------------------------------------


//-------------------------------------------------------------------------------

//2.115
int V2XSignalWattPointType_encode(cJSON *parent, const char* name, V2XSignalWattPointType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddNumberToObject(object, "signal", value->signal);
                cJSON_AddNumberToObject(object, "power", value->power);
                return 0;
            }
        }
    }
    return -1;
}

//2.116
int VariableAttributeType_encode(cJSON *parent, const char* name, VariableAttributeType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                if (value->type_isUsed) cJSON_AddStringToObject(object, "type", AttributeEnumType_toString(value->type));
                if (value->value_isUsed) cJSON_AddStringToObject(object, "value", value->value);
                if (value->mutability_isUsed) cJSON_AddStringToObject(object, "mutability", MutabilityEnumType_toString(value->mutability));
                if (value->persistent_isUsed) cJSON_AddNumberToObject(object, "persistent", value->persistent);
                if (value->constant_isUsed) cJSON_AddNumberToObject(object, "constant", value->constant);
                return 0;
            }
        }
    }
    return -1;
}

//2.117
int VariableCharacteristicsType_encode(cJSON *parent, const char* name, VariableCharacteristicsType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                if (value->unit_isUsed) cJSON_AddStringToObject(object, "unit", value->unit);
                cJSON_AddStringToObject(object, "dataType", DataEnumType_toString(value->dataType));
                if (value->minLimit_isUsed) cJSON_AddNumberToObject(object, "minLimit", value->minLimit);
                if (value->maxLimit_isUsed) cJSON_AddNumberToObject(object, "maxLimit", value->maxLimit);
                if (value->maxElements_isUsed) cJSON_AddStringToObject(object, "maxElements", value->maxElements);
                if (value->valuesList_isUsed) cJSON_AddStringToObject(object, "valuesList", value->valuesList);
                cJSON_AddNumberToObject(object, "supportsMonitoring", value->supportsMonitoring);
                return 0;
            }
        }
    }
    return -1;
}

//2.118
int VariableMonitoringType_encode(cJSON *parent, const char* name, VariableMonitoringType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddNumberToObject(object, "id", value->id);
                cJSON_AddNumberToObject(object, "transaction", value->transaction);
                cJSON_AddNumberToObject(object, "value", value->value);
                cJSON_AddStringToObject(object, "type", MonitorEnumType_toString(value->type));
                cJSON_AddNumberToObject(object, "severity", value->severity);
                cJSON_AddStringToObject(object, "eventNotificationType", EventNotificationEnumType_toString(value->eventNotificationType));
                return 0;
            }
        }
    }
    return -1;
}

//2.119
int VariableType_encode(cJSON *parent, const char* name, VariableType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "name", value->name);
                if (value->instance_isUsed) cJSON_AddStringToObject(object, "instance", value->instance);
                return 0;
            }
        }
    }
    return -1;
}

//2.120
int VoltageParamsType_encode(cJSON *parent, const char* name, VoltageParamsType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                if (value->hv10MinMeanValue_isUsed) cJSON_AddNumberToObject(object, "hv10MinMeanValue", value->hv10MinMeanValue);
                if (value->hv10MinMeanTripDelay_isUsed) cJSON_AddNumberToObject(object, "hv10MinMeanTripDelay", value->hv10MinMeanTripDelay);
                if (value->powerDuringCessation_isUsed) cJSON_AddStringToObject(object, "powerDuringCessation", PowerDuringCessationEnumType_toString(value->powerDuringCessation));
                return 0;
            }
        }
    }
    return -1;
}

//2.121
int VPNType_encode(cJSON *parent, const char* name, VPNType_t *value)
{
    if (value)
    {
        if (parent && name )
        {
            cJSON *object = cJSON_AddObjectToObject(parent, name);
            if (object)
            {
                cJSON_AddStringToObject(object, "server", value->server);
                cJSON_AddStringToObject(object, "user", value->user);
                if (value->group_isUsed) cJSON_AddStringToObject(object, "group", value->group);
                cJSON_AddStringToObject(object, "password", value->password);
                cJSON_AddStringToObject(object, "key", value->key);
                cJSON_AddStringToObject(object, "type", VPNEnumType_toString(value->type));
                return 0;
            }
        }
    }
    return -1;
}
