{%- import "cloudevents.jinja.include" as cloudEvents -%}
{%- import "amqp.jinja.include" as amqp -%}
{%- import "mqtt.jinja.include" as mqtt -%}
{%- set messagegroups = root.messagegroups %}
{%- set uses_cloudevents_message = (root | exists("envelope","CloudEvents/1.0")) %}
{%- set uses_http_endpoint = (root | exists( "protocol", "http" )) %}
{%- set uses_amqp_message = (root | exists("protocol","AMQP/1.0")) %}
{%- set uses_amqp_endpoint = (root | exists("protocol","AMQP/1.0")) %}
{%- set uses_mqtt_message = (root | exists( "protocol", "mqtt" )) %}
{%- set uses_mqtt_endpoint = (root | exists( "protocol", "mqtt" )) %}
// this file was generated by a tool 
package {{ class_name | namespace | lower }};

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.v1.CloudEventBuilder;

{%- if uses_amqp_message %}
{{ amqp.AmqpImports() }}
{%- endif %}
{%- if uses_mqtt_message %}
{{ mqtt.PahoImports() }}
{%- endif %}

{% for messagegroupid, messagegroup in messagegroups.items() -%}
{%- set groupname = messagegroupid  | pascal -%}
{%- set className = (groupname | strip_namespace)+"EventFactory" %}
public class {{ className | strip_namespace }}
{
    {% for messageid, message in messagegroup.messages.items() -%}
    {%- set messagename = messageid | pascal %}
    {%- set isCloudEvent = not message.envelope or message.envelope.lower().startswith("cloudevents") -%}
    {%- set is_amqp = not isCloudEvent and message.envelope.lower().startswith("amqp") %}
    {%- set isMqtt = not isCloudEvent and message.envelope.lower().startswith("mqtt") %}
    public {% if isCloudEvent %}CloudEvent{% elif is_amqp %}Amqp.Message{% elif isMqtt %}MqttMessageV5{% endif %} create{{ messagename | strip_namespace }}(
    {%- if message.dataschemauri -%}
    {%- set toBytes="data.toByteArray(contentType)" -%}
    {%- set type = (message.dataschemauri if message.dataschemauri else message.dataschema) | schema_type( project_name, root, message.dataschemaformat) %}
    {%- set typePackage = type | namespace | lower %}
    {%- set typeName = type | strip_namespace | pascal %}    
    {{- typePackage }}.{{ typeName }} data
    {%- else -%}
    {% set toBytes="data.toBytes()" %}
    CloudEventData data
    {%- endif -%}
    , String contentType
    {%- if isCloudEvent -%}
    {{- cloudEvents.DeclareUriTemplateArguments(message) -}}
    {%- elif is_amqp -%}
    {{- amqp.DeclareUriTemplateArguments(message) -}}
    {%- elif isMqtt -%}
    {{- mqtt.DeclareUriTemplateArguments(message) -}}
    {%- endif -%}
    ) throws Exception {
        {% if isCloudEvent %}
        {{- cloudEvents.BuildCloudEvent(message, toBytes) | indent(8) }}
        return cloudEvent;
        {%- elif is_amqp %}
        {{- amqp.DeclareAmqpNetLiteMessage("amqpMessage", message) | indent(12)}}
        amqpMessage.BodySection = new Data() { Binary = {{ toBytes }} };
        return amqpMessage;
        {%- elif isMqtt %}
        {{- mqtt.DeclareMqttNetMessage("mqttMessage", message) | indent(12)}}
        return mqttMessage;
        {%- endif %}
    }
    {% endfor %}
}
{% endfor -%}