// This is a template example to list the name of the classes
{% if package_name %}
package {{ package_name }};
{% endif %}
{% import "java_fields.py.j2" as java_fields %}
{% set attribs = [] %}
{% for attr in class_obj.attributes %}
    {% do attribs.append(java_fields.get_field(attr.type.name)) %}
    {% do attribs.append(attr.name) %}
{% endfor %}
{% set generals = namespace(names=[]) %}
{% set parent_attribs = [] %}
{% for parent in class_obj.parents() %}
    {% for attr in parent.attributes %}
        {% do parent_attribs.append(java_fields.get_field(attr.type.name)) %}
        {% do parent_attribs.append(attr.name) %}
    {% endfor %}
    {% set _ = generals.names.append(parent.name) %}
{% endfor %}
{% if "LocalDate" in attribs or  "LocalDate" in parent_attribs%}
import java.time.LocalDate;
{% endif %}
{% if "LocalDateTime" in attribs or "LocalDateTime" in parent_attribs %}
import java.time.LocalDateTime;
{% endif %}
{% if "LocalTime" in attribs or "LocalTime" in parent_attribs %}
import java.time.LocalTime;
{% endif %}
{% if "UUID" in attribs or "UUID" in parent_attribs %}
import java.time.UUID;
{% endif %}
import java.util.List;
import java.util.ArrayList;

{% set assocs = [] %}
{% set assocs_card_over_1 = [] %}
{% set inheritance = generals.names | join(', ') %}
{% if inheritance|length > 1 %}
    {% set extends = "extends " + inheritance %}
{% else %}
    {% set extends = "" %}
{% endif %}
public class {{ class_obj.name }} {{ extends }}{
{% for i in range(0, attribs|length, 2) %}
    private {{ attribs[i] }} {{ attribs[i+1] }};
{% endfor %}
{% for association in class_obj.associations %}
    {% if association.ends|length == 2 and association.name not in processed_associations %}
        {% set ns = namespace(end1=None, end2=None) %}
        {% for end in association.ends %}
            {% set ns.end1 = end if end.type.name == class_obj.name else ns.end1 %}
            {% set ns.end2 = end if end.type.name != class_obj.name else ns.end2 %}
        {% endfor %}
        {% set class1_name = ns.end1.type.name %}
        {% set class2_name = ns.end2.type.name %}
        {% if ns.end1.multiplicity.max > 1 and ns.end2.multiplicity.max > 1%}
            {% if ns.end1.multiplicity.min != 1 and ns.end2.multiplicity.min == 1 or
                ns.end1.multiplicity.min == 1 and ns.end2.multiplicity.min == 1 or
                ns.end1.multiplicity.min != 1 and ns.end2.multiplicity.min != 1 %}
    private List<{{ class2_name }}> {{ class2_name.lower() }}s;
                {% do assocs.append('List<'+class2_name+'>') %}
                {% do assocs.append(class2_name.lower()) %}
                {% do assocs_card_over_1.append(class2_name) %}              
                {% do processed_associations.append(association.name) %}
            {% endif %}
        {% elif ns.end1.multiplicity.max == 1 and ns.end2.multiplicity.max > 1 %}
    private List<{{ class2_name }}> {{ class2_name.lower() }}s;
            {% do assocs.append('List<'+class2_name+'>') %}
            {% do assocs.append(class2_name.lower()) %}
            {% do assocs_card_over_1.append(class2_name) %}  
            {% do processed_associations.append(association.name) %}
        {% elif ns.end1.multiplicity.max == 1 and ns.end2.multiplicity.max == 1 %}
    private {{ class2_name }} {{ class2_name.lower() }};
            {% do assocs.append(class2_name) %}
            {% do assocs.append(class2_name.lower()) %}
            {% do processed_associations.append(association.name) %}
        {% endif %}
    {% endif %}
{% endfor %}

    public {{ class_obj.name }} (
{%- for i in range(attribs|length) -%}
    {%- if i == attribs|length - 1 -%}
    {{ attribs[i]}}
    {%- else -%}
        {%- if i % 2 == 0 -%}
    {{ attribs[i]}} {% else -%}
    {{ attribs[i] }}, {% endif -%}
    {%- endif -%}
{%- endfor -%}
{%- if inheritance|length > 1 -%}
    {%- for i in range(parent_attribs|length) -%}
        {%- if i == parent_attribs|length - 1 -%}
    {{ parent_attribs[i]}}
        {%- else -%}
            {%- if i % 2 == 0 -%}
    , {{ parent_attribs[i]}} {% else -%}
    {{ parent_attribs[i] }}{% endif -%}
        {%- endif -%}
    {%- endfor -%}
{% endif %}) {
{% if inheritance|length > 1 %}
        super(
    {%- for i in range(parent_attribs|length) %}
        {%- if i == parent_attribs|length - 1 -%}
    {{ parent_attribs[i]}});
        {% else -%}
            {%- if i % 2 == 1 -%}
    {{ parent_attribs[i]}}, {% endif %}
        {% endif %}
    {% endfor %}
{% endif %}
{% for i in range(attribs|length) %}
    {% if i % 2 == 1 %}
        this.{{ attribs[i] }} = {{ attribs[i] }};
    {% endif %}
{% endfor %}
{% for i in range(1, assocs|length) %}
    {% if i % 2 == 1 and assocs[i-1][:4] == "List" %}
        this.{{ assocs[i] }}s = new ArrayList<>();
    {% endif %}
{% endfor %}
    }

{% if assocs_card_over_1 != [] %}
    public {{ class_obj.name }} (
{%- for i in range(attribs|length) -%}
    {%- if i == attribs|length - 1 -%}
    {{ attribs[i]}}
    {%- else -%}
        {%- if i % 2 == 0 -%}
    {{ attribs[i]}} {% else -%}
    {{ attribs[i] }}, {% endif -%}
    {%- endif -%}
{%- endfor -%}
{%- if inheritance|length > 1 -%}
    {%- for i in range(parent_attribs|length) -%}
        {%- if i == parent_attribs|length - 1 -%}
    {{ parent_attribs[i]}}
        {%- else -%}
            {%- if i % 2 == 0 -%}
    , {{ parent_attribs[i]}} {% else -%}
    {{ parent_attribs[i] }}{% endif -%}
        {%- endif -%}
    {%- endfor -%}
{% endif %}
{%- for i in range(0, assocs_card_over_1|length) -%}
    {%- if i < assocs_card_over_1 |length -%}
    ,{{" "}}
    {%- endif -%}
    ArrayList<{{ assocs_card_over_1[i] }}> {{ assocs_card_over_1[i]| lower }}s

{%- endfor -%}
) {
{% if inheritance|length > 1 %}
        super(
    {%- for i in range(parent_attribs|length) %}
        {%- if i == parent_attribs|length - 1 -%}
    {{ parent_attribs[i]}});
        {% else -%}
            {%- if i % 2 == 1 -%}
    {{ parent_attribs[i]}}, {% endif %}
        {% endif %}
    {% endfor %}
{% endif %}
{% for i in range(attribs|length) %}
    {% if i % 2 == 1 %}
        this.{{ attribs[i] }} = {{ attribs[i] }};
    {% endif %}
{% endfor %}
{% for i in range(1, assocs|length) %}
    {% if i % 2 == 1 and assocs[i-1][:4] == "List" %}
        this.{{ assocs[i] }}s = {{ assocs[i] }}s;
    {% endif %}
{% endfor %}
    }
{%- endif %}

{% for i in range(0, attribs|length, 2) %}

    public {{ attribs[i] }} get{{ attribs[i+1].capitalize() }}() {
        return this.{{ attribs[i+1] }};
    }

    public void set{{ attribs[i+1].capitalize() }}({{ attribs[i] }} {{ attribs[i+1] }}) {
        this.{{ attribs[i+1] }} = {{ attribs[i+1] }};
    }
{% endfor %}
{% for i in range(0, assocs|length, 2) %}

    {% if assocs[i][:4] == "List" %}
    public {{ assocs[i] }} get{{ assocs[i+1].capitalize() }}s() {
        return this.{{ assocs[i+1] }}s;
    }

    public void add{{ assocs[i+1].capitalize() }}({{ assocs[i+1].capitalize() }} {{ assocs[i+1] }}) {
        {{ assocs[i+1] }}s.add({{ assocs[i+1] }});
    }
    {% else %}
    public {{ assocs[i] }} get{{ assocs[i+1].capitalize() }}() {
        return this.{{ assocs[i+1] }};
    }

    public void set{{ assocs[i+1].capitalize() }}({{ assocs[i+1].capitalize() }} {{ assocs[i+1] }}) {
        this.{{ assocs[i+1] }} = {{ assocs[i+1] }};
    }
    {% endif %}
{% endfor %}
}
