#include "ocpp21_datatype.h"

//2.1
int AbsolutePriceScheduleType_decode(cJSON *object, AbsolutePriceScheduleType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(AbsolutePriceScheduleType_t));
        
        if (object)
        {
            sprintf(value->timeAnchor, cJSON_GetStringValue(cJSON_GetObjectItem(object, "timeAnchor")));
            value->priceScheduleID = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "priceScheduleID"));

            if (cJSON_HasObjectItem(object, "priceScheduleDescription"))
            {
                value->priceScheduleDescription_isUsed = 1;
                sprintf(value->priceScheduleDescription, cJSON_GetStringValue(cJSON_GetObjectItem(object, "priceScheduleDescription")));
            }

            if (cJSON_HasObjectItem(object, "currency"))
            {
                value->currency_isUsed = 1;
                sprintf(value->currency, cJSON_GetStringValue(cJSON_GetObjectItem(object, "currency")));
            }

            if (cJSON_HasObjectItem(object, "language"))
            {
                value->language_isUsed = 1;
                sprintf(value->language, cJSON_GetStringValue(cJSON_GetObjectItem(object, "language")));
            }

            if (cJSON_HasObjectItem(object, "priceAlgorithm"))
            {
                value->priceAlgorithm_isUsed = 1;
                sprintf(value->priceAlgorithm, cJSON_GetStringValue(cJSON_GetObjectItem(object, "priceAlgorithm")));
            }

            return 0;
        }
    }
    return -1;
}

//2.2
int ACChargingParametersType_decode(cJSON *object, ACChargingParametersType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(ACChargingParametersType_t));
        
        if (object)
        {
            value->energyAmount = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "energyAmount"));
            value->evMinCurrent = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "evMinCurrent"));
            value->evMaxCurrent = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "evMaxCurrent"));
            value->evMaxVoltage = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "evMaxVoltage"));
            return 0;
        }
    }
    return -1;
}

//2.3
int AdditionalInfoType_decode(cJSON *object, AdditionalInfoType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(AdditionalInfoType_t));
        
        if (object)
        {
            sprintf(value->additionalIdToken, cJSON_GetStringValue(cJSON_GetObjectItem(object, "additionalIdToken")));
            sprintf(value->type, cJSON_GetStringValue(cJSON_GetObjectItem(object, "type")));
            return 0;
        }
    }
    return -1;
}

//2.81
int RationalNumberType_decode(cJSON *object, RationalNumberType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(RationalNumberType_t));
        
        if (object)
        {
            value->exponent = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "exponent"));
            value->value = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "value"));
            return 0;
        }
    }
    return -1;
}

//2.4
int AdditionalSelectedServicesType_decode(cJSON *object, AdditionalSelectedServicesType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(AdditionalSelectedServicesType_t));
        
        if (object)
        {
            sprintf(value->serviceName, cJSON_GetStringValue(cJSON_GetObjectItem(object, "serviceName")));
            RationalNumberType_decode(cJSON_GetObjectItem(object, "serviceFee"), &value->serviceFee);
            return 0;
        }
    }
    return -1;
}

//2.5
int AdditionalInfoType_decode(cJSON *object, AdditionalInfoType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(AdditionalInfoType_t));
        
        if (object)
        {
            sprintf(value->additionalIdToken, cJSON_GetStringValue(cJSON_GetObjectItem(object, "additionalIdToken")));
            sprintf(value->type, cJSON_GetStringValue(cJSON_GetObjectItem(object, "type")));
            return 0;
        }
    }
    return -1;
}

//2.6
int APNType_decode(cJSON *object, APNType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(APNType_t));
        
        if (object)
        {
            sprintf(value->apn, cJSON_GetStringValue(cJSON_GetObjectItem(object, "apn")));
            if (cJSON_HasObjectItem(object, "anpUserName"))
            {
                value->anpUserName_isUsed = 1;
                sprintf(value->anpUserName, cJSON_GetStringValue(cJSON_GetObjectItem(object, "anpUserName")));
            }
            if (cJSON_HasObjectItem(object, "anpPassword"))
            {
                value->anpPassword_isUsed = 1;
                sprintf(value->anpPassword, cJSON_GetStringValue(cJSON_GetObjectItem(object, "anpPassword")));
            }
            if (cJSON_HasObjectItem(object, "simPin"))
            {
                value->simPin_isUsed = 1;
                sprintf(value->simPin, cJSON_GetStringValue(cJSON_GetObjectItem(object, "simPin")));
            }
            if (cJSON_HasObjectItem(object, "preferredNetwork"))
            {
                value->preferredNetwork_isUsed = 1;
                sprintf(value->preferredNetwork, cJSON_GetStringValue(cJSON_GetObjectItem(object, "preferredNetwork")));
            }
            if (cJSON_HasObjectItem(object, "useOnlyPreferredNetwork"))
            {
                value->useOnlyPreferredNetwork_isUsed = 1;
                value->useOnlyPreferredNetwork = cJSON_IsTrue(cJSON_GetObjectItem(object, "useOnlyPreferredNetwork"));
            }
            value->apnAuthentication = APNAuthenticationEnumType_fromString(cJSON_GetObjectItem(object, "apnAuthentication"));
            return 0;
        }
    }
    return -1;
}

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


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

//2.115
int V2XSignalWattPointType_decode(cJSON *object, V2XSignalWattPointType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VariableCharacteristicsType_t));
        
        if (object)
        {
            value->signal = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "signal"));
            value->power = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "power"));
            return 0;
        }
    }
    return -1;
}

//2.116
int VariableAttributeType_decode(cJSON *object, VariableAttributeType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VariableCharacteristicsType_t));
        
        if (object)
        {
            if (cJSON_HasObjectItem(object, "type"))
            {
                value->type_isUsed = 1;
                value->type = AttributeEnumType_fromString(cJSON_GetObjectItem(object, "type"));
            }
            if (cJSON_HasObjectItem(object, "value"))
            {
                value->value_isUsed = 1;
                sprintf(value->value, cJSON_GetStringValue(cJSON_GetObjectItem(object, "value")));
            }
            if (cJSON_HasObjectItem(object, "mutability"))
            {
                value->mutability_isUsed = 1;
                value->mutability = MutabilityEnumType_fromString(cJSON_GetObjectItem(object, "mutability"));
            }
            if (cJSON_HasObjectItem(object, "persistent"))
            {
                value->persistent_isUsed = 1;
                value->persistent = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "persistent"));
            }
            if (cJSON_HasObjectItem(object, "constant"))
            {
                value->constant_isUsed = 1;
                value->constant = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "persistent"));
            }
            return 0;
        }
    }
    return -1;
}

//2.117
int VariableCharacteristicsType_decode(cJSON *object, VariableCharacteristicsType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VariableCharacteristicsType_t));
        
        if (object)
        {
            if (cJSON_HasObjectItem(object, "unit"))
            {
                value->unit_isUsed = 1;
                sprintf(value->unit, cJSON_GetStringValue(cJSON_GetObjectItem(object, "unit")));
            }
            value->dataType =  DataEnumType_fromString(cJSON_GetObjectItem(object, "dataType"));
            if (cJSON_HasObjectItem(object, "minLimit"))
            {
                value->minLimit_isUsed = 1;
                value->minLimit = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "minLimit"));
            }
            if (cJSON_HasObjectItem(object, "maxLimit"))
            {
                value->maxLimit_isUsed = 1;
                value->maxLimit = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "maxLimit"));
            }
            if (cJSON_HasObjectItem(object, "maxElements"))
            {
                value->maxElements_isUsed = 1;
                value->maxElements = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "maxElements"));
            }
            if (cJSON_HasObjectItem(object, "valuesList"))
            {
                value->valuesList_isUsed = 1;
                sprintf(value->valuesList, cJSON_GetStringValue(cJSON_GetObjectItem(object, "valuesList")));
            }
            value->supportsMonitoring = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "supportsMonitoring"));
            
            return 0;
        }
    }
    return -1;
}

//2.118
int VariableMonitoringType_decode(cJSON *object, VariableMonitoringType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VariableMonitoringType_t));
        
        if (object)
        {
            value->id = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "id"));
            value->transaction = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "transaction"));
            value->value = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "value"));
            value->type = MonitorEnumType_fromString(cJSON_GetObjectItem(object, "type"));
            value->severity = (int)cJSON_GetNumberValue(cJSON_GetObjectItem(object, "severity"));
            value->eventNotificationType = EventNotificationEnumTyp_fromString(cJSON_GetObjectItem(object, "eventNotificationType"));
            return 0;
        }
    }
    return -1;
}

//2.119
int VariableType_decode(cJSON *object, VariableType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VariableType_t));
        
        if (object)
        {
            sprintf(value->name, cJSON_GetStringValue(cJSON_GetObjectItem(object, "name")));
            if (cJSON_HasObjectItem(object, "instance"))
            {
                value->instance_isUsed = 1;
                sprintf(value->instance, cJSON_GetStringValue(cJSON_GetObjectItem(object, "instance")));
            }
            return 0;
        }
    }
    return -1;
}

//2.120
int VoltageParamsType_decode(cJSON *object, VoltageParamsType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VoltageParamsType_t));
        
        if (object)
        {
            if (cJSON_HasObjectItem(object, "hv10MinMeanValue"))
            {
                value->hv10MinMeanValue_isUsed = 1;
                value->hv10MinMeanValue = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "hv10MinMeanValue"));
            }
            if (cJSON_HasObjectItem(object, "hv10MinMeanTripDelay"))
            {
                value->hv10MinMeanTripDelay_isUsed = 1;
                value->hv10MinMeanTripDelay = cJSON_GetNumberValue(cJSON_GetObjectItem(object, "hv10MinMeanTripDelay"));
            }
            if (cJSON_HasObjectItem(object, "powerDuringCessation"))
            {
                value->powerDuringCessation_isUsed = 1;
                value->powerDuringCessation = VPNEnumType_fromString(cJSON_GetObjectItem(object, "powerDuringCessation"));
            }
            return 0;
        }
    }
    return -1;
}

//2.121
int VPNType_decode(cJSON *object, VPNType_t *value)
{
    if (value)
    {
        memset(value, 0, sizeof(VPNType_t));
        
        if (object)
        {
            sprintf(value->server, cJSON_GetStringValue(cJSON_GetObjectItem(object, "server")));
            sprintf(value->user, cJSON_GetStringValue(cJSON_GetObjectItem(object, "user")));
            if (cJSON_HasObjectItem(object, "group"))
            {
                value->group_isUsed = 1;
                sprintf(value->group, cJSON_GetStringValue(cJSON_GetObjectItem(object, "group")));
            }
            sprintf(value->password, cJSON_GetStringValue(cJSON_GetObjectItem(object, "password")));
            sprintf(value->key, cJSON_GetStringValue(cJSON_GetObjectItem(object, "key")));
            value->type = VPNEnumType_fromString(cJSON_GetObjectItem(object, "type"));
            return 0;
        }
    }
    return -1;
}


