<#--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
${helper.getSourceDirectory()?replace(".", "/")}/${helper.camelCaseToSnakeCase(type.name)}.c
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.c.CLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition" -->
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <string.h>
#include <plc4c/driver_${protocolName}_static.h>

#include "${helper.camelCaseToSnakeCase(type.name)}.h"

// Code generated by code-generation. DO NOT EDIT.

<#--
    Add any import statements for partent-types, complex types used in properties or parser arguments.
-->
<#if helper.getTypeNamesForImportStatements()?has_content>
    <#list helper.getTypeNamesForImportStatements() as typeReference>
#include "${helper.camelCaseToSnakeCase(typeReference)}.h"
    </#list>
</#if>

// Create an empty NULL-struct
static const ${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_null_const;

${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_null() {
  return ${helper.getCTypeName(type.name)}_null_const;
}

// Parse function.
plc4c_return_code ${helper.getCTypeName(type.name)}_parse(plc4c_spi_read_buffer* readBuffer, ${helper.getCTypeName(type.name)}* _message) {
    plc4c_return_code _res = OK;

    ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow())} value;
    _res = ${helper.getReadBufferReadMethodCall(type.type.orElseThrow().asSimpleTypeReference().orElseThrow(), "&value", null)?no_esc};
    *_message = ${helper.getCTypeName(type.name)}_for_value(value);

    return _res;
}

plc4c_return_code ${helper.getCTypeName(type.name)}_serialize(plc4c_spi_write_buffer* writeBuffer, ${helper.getCTypeName(type.name)}* _message) {
    plc4c_return_code _res = OK;

    _res = ${helper.getWriteBufferWriteMethodCall(type.type.orElseThrow().asSimpleTypeReference().orElseThrow(), "*_message", null)?no_esc};

    return _res;
}

${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_for_value(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow())} value) {
    for(int i = 0; i < ${helper.getCTypeName(type.name)}_num_values(); i++) {
        if(${helper.getCTypeName(type.name)}_value_for_index(i) == value) {
            return ${helper.getCTypeName(type.name)}_value_for_index(i);
        }
    }
    return -1;
}

${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_value_of(char* value_string) {
<#list type.enumValues as enumValue>
    if(strcmp(value_string, "${enumValue.name}") == 0) {
        return ${helper.getCTypeName(type.name)}_${enumValue.name};
    }
</#list>
    return -1;
}

int ${helper.getCTypeName(type.name)}_num_values() {
  return ${type.enumValues?size};
}

${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_value_for_index(int index) {
    switch(index) {
    <#list type.enumValues as enumValue>
      case ${enumValue?index}: {
        return ${helper.getCTypeName(type.name)}_${enumValue.name};
      }
    </#list>
      default: {
        return -1;
      }
    }
}
    <#--
        If this enum also defines constant values, output one function for each.
        Each of these functions consist of a simple switch statement using the enum as switch-value
        returning the constant for a given enum value.
    -->
<#if type.constantNames?has_content>
    <#list type.constantNames as constantName>
        <#assign constantType = type.getConstantType(constantName)>

${helper.getLanguageTypeNameForTypeReference(constantType)} ${helper.getCTypeName(type.name)}_get_${helper.camelCaseToSnakeCase(constantName)}(${helper.getCTypeName(type.name)} value) {
        <#assign uniqueEnumValues=helper.getUniqueEnumValues(type.enumValues)/>
        <#if helper.getCTypeName(type.name) == "bool">
  if (!value) {
            <#assign enumValue=uniqueEnumValues[0]>
    return <#if helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow()) == '-1'>-1<#elseif constantType.isNonSimpleTypeReference()><#if constantType.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(constantType)}_${helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow())}<#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if><#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if>;
  } else {
            <#if uniqueEnumValues?size &gt; 1>
            <#assign enumValue=uniqueEnumValues[1]>
    return <#if helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow()) == '-1'>-1<#elseif constantType.isNonSimpleTypeReference()><#if constantType.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(constantType)}_${helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow())}<#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if><#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if>;
            <#else>
    return 0;
            </#if>
  }
        <#else>
  switch(value) {
        <#list uniqueEnumValues as enumValue>
    case ${helper.getCTypeName(type.name)}_${enumValue.name}: { /* '${enumValue.value}' */
      return <#if helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow()) == '-1'>-1<#elseif constantType.isNonSimpleTypeReference()><#if constantType.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(constantType)}_${helper.escapeValue(constantType, enumValue.getConstant(constantName).orElseThrow())}<#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if><#else>${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}</#if>;
    }<#sep>
    </#sep></#list>

    default: {
      return 0;
    }
  }
        </#if>
}

${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_get_first_enum_for_field_${helper.camelCaseToSnakeCase(constantName)}(${helper.getLanguageTypeNameForTypeReference(constantType)} value) {
    <#assign uniqueEnumValues=helper.getEnumValuesForUniqueConstantValues(type.enumValues, constantName)>
    <#if constantType.isStringTypeReference() || constantType.isVstringTypeReference()>
        <#list helper.getEnumValuesForUniqueConstantValues(type.enumValues, constantName) as enumValue>
    if (strcmp(value, ${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}) == 0) {
        return ${helper.getCTypeName(type.name)}_${enumValue.name};
    }
        </#list>
    <#elseif constantType.isBooleanTypeReference()>
        if (!value) {
        <#assign enumValue=uniqueEnumValues[0]>
            return ${helper.getCTypeName(type.name)}_${enumValue.name};
        } else {
        <#if uniqueEnumValues?size &gt; 1>
        <#assign enumValue=uniqueEnumValues[1]>
            return ${helper.getCTypeName(type.name)}_${enumValue.name};
        <#else>
            return -1;
        </#if>
        }
    <#else>
    switch(value) {
        <#list uniqueEnumValues as enumValue>
            <#if constantType.isSimpleTypeReference()>
        case ${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}: {
            <#elseif constantType.isEnumTypeReference()>
        case ${helper.getCTypeName(type.name)}_${enumValue.name}: /* '${enumValue.value}' */{
            </#if>
            return ${helper.getCTypeName(type.name)}_${enumValue.name};
        }
        </#list>
        default: {
            return -1;
        }
    }
    </#if>
}
    </#list>
</#if>

uint16_t ${helper.getCTypeName(type.name)}_length_in_bytes(${helper.getCTypeName(type.name)}* _message) {
    return ${helper.getCTypeName(type.name)}_length_in_bits(_message) / 8;
}

uint16_t ${helper.getCTypeName(type.name)}_length_in_bits(${helper.getCTypeName(type.name)}* _message) {
    return ${type.type.orElseThrow().asSimpleTypeReference().orElseThrow().sizeInBits};
}
