{# 
# TITLE
#}
{% macro printTitle (title, level, iconUrl) -%}
{% for i in [1..level] %}#{% endfor %} {{(not empty(iconUrl))?'![]('~iconUrl~') ':''}}{{- trim(title) }}
{%- endmacro %}

{# 
# MAVEN COORDINATE
#}
{% macro printMavenCoordinate (mavenCoordinate, prefix) -%}
```xml
{{ prefix }}<dependency>
{{ prefix }}    <groupId>{{ trim(mavenCoordinate.groupId) }}</groupId>
{{ prefix }}    <artifactId>{{ trim(mavenCoordinate.artifactId) }}</artifactId>
{{ prefix }}    <version>{{ trim(mavenCoordinate.version) }}</version>{{'\n'}}
{%- if not empty(mavenCoordinate.classifier) %}{{ prefix }}    <classifier>{{ trim(mavenCoordinate.classifier) }}</classifier>{{'\n'}}{%- endif -%}
{{ prefix }}</dependency>
{{ prefix }}```
{%- endmacro %}

{# 
# SLING FEATURE COORDINATE
#}
{% macro printFeatureCoordinate (mavenCoordinate, checksum) -%}
```
{{ prefix }}"bundles": [
{{ prefix }}   {
{{ prefix }}    "id": "{{ trim(mavenCoordinate.groupId) }}:{{ trim(mavenCoordinate.artifactId) }}{%- if not empty(mavenCoordinate.classifier) %}{{ prefix }}:jar:{{ trim(mavenCoordinate.classifier) }}{%- endif -%}:{{ trim(mavenCoordinate.version) }}"
{% if not empty(checksum.sha1) %}{{ prefix }}    "hash": "{{ trim(checksum.sha1) }}"{{'\n'}}{%- endif -%}
{{ prefix }}   }
{{ prefix }}]
{{ prefix }}```
{%- endmacro %}


{# 
# OSGI COORDINATE
#}
{% macro printOsgiCoordinate (symbolicName, version, prefix) -%}
{% import _self as this -%}
```
{{ prefix }}Bundle Symbolic Name: {{ trim(symbolicName) }}
{{ prefix }}Version             : {{ this.printOsgiVersion(version) }}
{{ prefix }}```
{%- endmacro %}

{# 
# CHECKSUM
#}
{% macro printChecksum (checksum, prefix) -%}
{% import _self as this -%}
```{{'\n'}}
{%- if not empty(checksum.md5) %}{{ prefix }}md5:    {{ checksum.md5 }}{{'\n'}}{%- endif -%}
{%- if not empty(checksum.sha1) %}{{ prefix }}sha1:   {{ checksum.sha1 }}{{'\n'}}{%- endif -%}
{%- if not empty(checksum.sha256) %}{{ prefix }}sha256: {{ checksum.sha256 }}{{'\n'}}{%- endif -%}
{%- if not empty(checksum.sha256) %}{{ prefix }}sha512: {{ checksum.sha512 }}{{'\n'}}{%- endif -%}

{{ prefix }}```
{%- endmacro %}

{# 
# OSGI VERSION
#}
{% macro printOsgiVersion (osgiVersion) -%}
{{ osgiVersion.major }}{{ '.'~osgiVersion.minor }}{{ '.'~osgiVersion.micro }}
{%- if not empty(osgiVersion.qualifier) %}
{{- '.'~osgiVersion.qualifier }}
{%- endif %}
{%- endmacro %}

{# 
# TABLE HEADER
#}
{% macro printTableHeaders (headers, layout, prefix) -%}
{% for h in headers %}{{ (loop.first) ? '|'~trim(h)~' |' : trim(h)~' |' }}{% endfor %}
{% if empty (layout) -%}
{{ prefix }}{% for h in headers %}{{ (loop.first) ? '|--- |' : '--- |' }}{% endfor %}
{%- else %}
{%- set layoutTab = split(layout, '') -%}
{{ prefix }}{% for l in layoutTab %}
{%- if l == 'r' -%}
{{ (loop.first) ? '|---: |' : '---: |' }}
{%- elseif l == 'c' -%}
{{ (loop.first) ? '|:---: |' : ':---: |' }}
{%- else -%}
{{ (loop.first) ? '|--- |' : '--- |' }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endmacro %}

{# 
# PROPERTY TYPE
#}
{% macro printPropertyType (type, multiValue) -%}
{{ type }}{{ (multiValue) ? '[]' : ''}}
{%- endmacro %}

{# 
# PROPERTY VALUES
#}
{% macro printPropertyValues (type, values, multiValue) -%}
{%- if multiValue -%}
{{ '[' }}{% for v in values %}{{ (loop.first) ? '' : ', ' }}{{ (type == 'String' ) ? '"'~v~'"' : v}}{% endfor %}{{ ']' }}
{%- else -%}
{{ (type == 'String' ) ? '"'~(values[0])~'"' : values[0] }}
{%- endif -%}
{%- endmacro %}

{# 
# OPTION
#}
{% macro printOptions (option) -%}
{% for o in option %}{{ (loop.first) ? '' : ', ' }}{{ '"'~o.value~'"' }}{% endfor %}
{%- endmacro %}

{# 
# COMPONENTS
#}
{% macro printComponents (components, metatypes) -%}
{%- import _self as this -%}
{%- for c in components -%}
{%- if not (loop.first) -%}{{ '\n\n---\n\n' }}{%- endif -%}
{{ this._printComponentTitle(c) }}

{{ this._printComponentServiceDescription(c) }}

{{ this._printComponentServices(c) }}

{{ this._printComponentProperties(c) }}

{{ this._printComponentConfiguration(c, metatypes) }}

{{ this._printComponentReferences(c) }}

{{ this._printComponentJsonConfiguratorSnippet(c, metatypes) }}
{%- endfor -%}
{%- endmacro %}

{# 
# COMPONENT TITLE
#}
{% macro _printComponentTitle (c) -%}
{%- import _self as this -%}
{{ this.printTitle(c.name,3) }} - *state = {{ (c.defaultEnabled) ? 'enabled' : 'not enabled' }}, activation = {{ (c.immediate) ? 'immediate' : 'delayed' }}*
{%- endmacro %}

{# 
# DESCRIPTION
#}
{% macro _printComponentServiceDescription (c) -%}
{%- import _self as this -%}
{{ this.printTitle('Description',4) }}
{%- for key, value in c.properties -%}
{%- if key == 'service.description' -%}
{{ '\n\n' }}{{ value.values[0] }}
{%- endif -%}
{%- endfor -%}
{%- endmacro %}

{# 
# COMPONENT SERVICES
#}
{% macro _printComponentServices (c) -%}
{%- import _self as this -%}
{%- if not empty(c.serviceInterfaces) -%}
{{ this.printTitle('Services - *scope = '~c.scope~'*',4) }}

{{ this.printTableHeaders(['Interface name']) }}
{%- for s in c.serviceInterfaces -%}
{{ '\n' }}|{{ s }} |
{%- endfor -%}
{%- else -%}
{{ this.printTitle('Services',4) }}

No services.
{%- endif -%}
{%- endmacro %}

{# 
# COMPONENT PROPERTIES
#}
{% macro _printComponentProperties (c) -%}
{%- import _self as this -%}
{{ this.printTitle('Properties',4) }}{{ '\n\n' }}
{%- if not empty(c.properties) > 0 -%}
{{ this.printTableHeaders(['Name','Type','Value']) }}
{%- for key, value in c.properties -%}
{{ '\n' }}|{{key}} |{{ this.printPropertyType(value.type,value.multiValue)}} |{{ this.printPropertyValues(value.type,value.values,value.multiValue) }} |
{%- endfor -%}
{%- else -%}
No properties.
{%- endif -%}
{%- endmacro %}

{# 
# COMPONENT CONFIGURATION
#}
{% macro _printComponentConfiguration (c, metatypes) -%}
{%- import _self as this -%}
{%- if not (c.factory is null) -%}
{{ this.printTitle('Configuration - *factory*',4) }}

Factory name: {{ '`'~c.factory~'`'}}
{%- elseif c.configurationPolicy == 'ignore' -%}
{{ this.printTitle('Configuration',4) }}

No configuration.
{%- else -%}
{{ this.printTitle('Configuration - *policy = '~c.configurationPolicy~'*',4) }}{{ '\n\n' }}
{%- for pid in c.configurationPid -%}
{%- if not (loop.first) -%}{{ '\n\n' }}{%- endif -%}
{%- set pidOcd = "notset" -%}
{%- set factoryPidOcd = "notset" -%}
{%- for ocd in metatypes -%}
{%- if pid in ocd.pids -%}
{%- set pidOcd = ocd -%}
{%- elseif pid in ocd.factoryPids -%}
{%- set factoryPidOcd = ocd -%}
{%- endif -%}
{%- endfor -%}
{%- set o = "notset" -%}
{%- if pidOcd != 'notset' -%}
{{ this.printTitle('Pid: `'~pid~'`',5) }}{{ '\n\n' }}
{%- set o = pidOcd -%}
{%- elseif factoryPidOcd != 'notset' -%}
{{ this.printTitle('Factory Pid: `'~pid~'`',5) }}{{ '\n\n' }}
{%- set o = factoryPidOcd -%}
{%- else -%}
{{ this.printTitle('Pid: `'~pid~'`',5) }}

No information available.
{%- endif -%}
{%- if o != 'notset' -%}
{%- for a in o.attributes -%}
{%- if not (loop.first) -%}{{ '\n\n' }}{%- endif -%}
{{ this.printTableHeaders(['Attribute','Value']) }}
|Id |`{{a.id}}` |
|Required |**{{a.required}}** |
|Type |**{{ this.printPropertyType(a.type,(a.cardinality != 0)) }}** |
{%- if length(a.description) > 0 -%}
{{ '\n' }}|Description |{{ a.description }} |
{%- endif -%}
{%- if length(a.values) > 0 -%}
{{ '\n' }}|Default |{{ this.printPropertyValues(a.type,a.values,(a.cardinality != 0)) }} |
{%- endif -%}
{%- if (not empty(a.max)) or (not empty(a.min)) -%}
{{ '\n' }}|Value range |
{%- if (not empty(a.max)) and (not empty(a.min)) -%}
`min = {{a.min}}` / `max = {{a.max}}` |
{%- elseif not empty(a.max) -%}
`max = {{a.max}}` |
{%- else -%}
`min = {{a.min}}` |
{%- endif -%}
{%- elseif length(a.options) > 0 -%}
{{ '\n' }}|Value range |{{ this.printOptions(a.options) }} |
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro %}


{# 
# COMPONENT REFERENCES
#}
{% macro _printComponentReferences (c) -%}
{%- import _self as this -%}
{{ this.printTitle('Reference bindings',4) }}{{ '\n\n' }}

{%- if c.references is empty -%}
No bindings.
{%- else -%}
{%- for r in c.references %}
{{- this.printTableHeaders(['Attribute','Value']) }}
|name |{{r.name}} |
|interfaceName |{{r.interfaceName}} |
|target |{{r.target}} |
|cardinality |{{r.cardinality}} |
|policy |{{r.policy}} |
|policyOption |{{r.policyOption}} |
|scope |{{r.scope}} |
{%- endfor -%}
{%- endif -%}
{%- endmacro %}


{# 
# COMPONENT JSON-CONFIGURATOR-SNIPPET
#}
{% macro _printComponentJsonConfiguratorSnippet (c, metatypes) -%}
{%- import _self as this -%}

{{ this.printTitle('OSGi-Configurator',4) }}

{% for pid in c.configurationPid %}
```
/*
 * Component: {{ c.name }}
 * policy:    {{ c.configurationPolicy }}
 */
{% set pidOcd = "notset" -%}
{%- set factoryPidOcd = "notset" -%}
{%- for ocd in metatypes -%}
{%- if pid in ocd.pids -%}
{%- set pidOcd = ocd -%}
{%- elseif pid in ocd.factoryPids -%}
{%- set factoryPidOcd = ocd -%}
{%- endif -%}
{%- endfor -%}
{%- set o = "notset" -%}
{%- if pidOcd != 'notset' -%}
    "{{ pid }}":{
{%- set o = pidOcd -%}
{%- elseif factoryPidOcd != 'notset' -%}
    "{{ pid }}~FactoryNameChangeIt":{
{%- set o = factoryPidOcd -%}
{%- else -%}
    "{{ pid }}":{

{%- endif %}
        //# Component properties    
{%- if c.properties is empty %}
        // none
{% else %}
{%- for key, a in c.properties %}
        /*
         * Type = {{ this.printPropertyType(a.type,a.multiValue) }}
{%- if length(a.values) > 0 %}
         * Default = {{ this.printPropertyValues(a.type,a.values,a.multiValue) }}
{%- endif %}
         */
         // "{{ key }}": null,{% if loop.last %}{{ '\n' }}{% else%}{% endif %}{{ '\n' }}
{%- endfor %}
{%- endif %}
        //# Reference bindings    
{%- if c.references is empty %}
        // none
{%- else -%}
{%- for r in c.references %}
        // "{{r.name}}.target": {% if r.target=='' %}"(component.pid=*)"{% else %}"{{r.target}}"{% endif -%}
{%- if loop.last -%}{%- if loop.parent.o == 'notset' %}{% else %},{% endif %}{{ '\n' }}{% else %},{%- endif -%}
{%- endfor %}
{%- endif %}

        //# ObjectClassDefinition - Attributes
{%- if o == 'notset' %}
        // (No PidOcd available.)
{% else %}
{%- for a in o.attributes %}
        /*
         * Required = {{a.required}}
         * Type = {{ this.printPropertyType(a.type,(a.cardinality != 0)) }}
{%- if not empty(a.description) %}
         * Description = {{ a.description }}
{%- endif %}
{%- if length(a.values) > 0 %}
         * Default = {{ this.printPropertyValues(a.type,a.values,(a.cardinality != 0)) }}
{%- endif %}
{%- if (not empty(a.max)) or (not empty(a.min)) %}
         * Value restriction = `min = {{a.min}}` / `max = {{a.max}}`
{%- elseif length(a.options) > 0 %}
         * Value restriction = {{ this.printOptions(a.options) }}
{%- endif %}
         */
         {% if a.required!='true' %}// {% endif -%}"{{ a.id }}": null{% if loop.last %}{% else%},{% endif %}{{ '\n' }}
{%- endfor %}
{%- endif -%}
}
```
{%- endfor %}
{%- endmacro %}

{# 
# DEVELOPERS
#}
{% macro printDevelopers (developers) -%}
{%- for d in developers -%}
{%- if not (loop.first) -%}{{ '\n' }}{%- endif -%}
{%- set email=d.email -%}
{%- set id=d.identifier -%}
{%- if empty(email) and id.contains('@') -%}
{%- set email=id -%}
{%- set id=null -%}
{%- endif -%}
{%- if empty(id) -%}
{%- if empty(d.name) -%}
* [{{ trim(email) }}](mailto:{{ trim(email) }})
{%- else -%}
* **{{ trim(d.name) }}** / [{{ trim(email) }}](mailto:{{ trim(email) }})
{%- endif -%}
{%- else -%}
{%- if empty(d.name) -%}
{%- if empty(email) -%}
* {{ trim(id) }}
{%- else -%}
* {{ trim(id) }} / [{{ trim(email) }}](mailto:{{ trim(email) }})
{%- endif -%}
{%- else -%}
{%- if empty(email) -%}
* **{{ trim(d.name) }}** ({{ trim(id) }})
{%- else -%}
* **{{ trim(d.name) }}** ({{ trim(id) }}) / [{{ trim(email) }}](mailto:{{ trim(email) }})
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if not empty(d.organization) and not empty(d.organizationUrl) -%}
{{ ' @ ' }}[{{ trim(d.organization) }}]({{ trim(d.organizationUrl) }})
{%- elseif not empty(d.organization) and empty(d.organizationUrl) -%}
{{ ' @ ' }}{{ trim(d.organization) }}
 {%- elseif empty(d.organization) and not empty(d.organizationUrl) -%}
{{ ' @ ' }}[{{ trim(d.organizationUrl) }}]({{ trim(d.organizationUrl) }})
{%- endif -%}
{%- if length(d.roles) > 0 -%}
{{ ' - ' }}{%- for r in d.roles -%}*{{ trim(r) }}*{{ (loop.last) ? '' : ', '}}{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endmacro %}

{# 
# LICENCES
#}
{% macro printLicenses (licenses) -%}
{%- for l in licenses -%}
{%- if not (loop.first) -%}{{ '\n\n' }}{%- endif -%}
**{{ trim(l.name) }}**
{%- if not empty(l.description) -%}
{{ '\n  ' }}> {{ trim(l.description) }}
{%- endif -%}
{%- if not empty(l.link) -%}
{{ '\n  ' }}>
{{ '  ' }}> For more information see [{{ trim(l.link) }}]({{ trim(l.link) }}).
{%- endif -%}
{%- endfor -%}
{%- endmacro %}

{# 
# ARTIFACTS
#}
{% macro printArtifacts (report) -%}
{%- import _self as this -%}
{{- this._printArtifactsRecursive(report,'', '') -}}
{%- endmacro %}

{# 
# ARTIFACTS RECURSIVE
#}
{% macro _printArtifactsRecursive (report, prefix, path) -%}
{% import _self as this -%}
{%- if not empty(report.projects) -%}
{%- for p in report.projects -%}
{%- if not (loop.first) -%}{{ '\n' }}{%- endif -%}
{%- set bundle = null -%}
{%- if not empty(p.bundles) -%}
{%- set bundle = p.bundles[0] -%}
{%- endif -%}
{%- set title = default(bundle.manifest.bundleName,p.commonInfo.name) -%}
{%- set title = default(title,bundle.manifest.bundleSymbolicName.symbolicName) -%}
{%- set title = trim(default(title,p.fileName)) -%}
{%- set description = trim(default(bundle.manifest.bundleDescription,p.commonInfo.description)) -%}
{%- if length(p.bundles) > 1 -%}
{%- set title = trim(default(p.commonInfo.name,p.fileName)) -%}
{%- set description = trim(p.commonInfo.description) -%}
{%- endif -%}
{{prefix}}* [**{{title}}**]({{ path~p.fileName }}){{ (length(description) > 0) ? ': '~description : ''}}
{%- if not empty(p.projects) or length(p.bundles) > 1 -%}
{{ '\n' }}{{ this._printArtifactsRecursive(p, prefix~'  ', path~p.fileName~'/') }}
{%- endif -%}
{%- endfor -%}
{%- elseif length(report.bundles) > 1 -%}
{%- for b in report.bundles -%}
{%- if not (loop.first) -%}{{ '\n' }}{%- endif -%}
{%- set title = trim(default(b.manifest.bundleName,b.manifest.bundleSymbolicName.symbolicName)) -%}
{%- set description = trim(b.manifest.bundleDescription) -%}
{%- if b.manifest.bundleSymbolicName.symbolicName.startsWith(report.fileName) -%}
{%- set readmeFile = 'readme'~b.manifest.bundleSymbolicName.symbolicName.substring(length(report.fileName))~'.md' -%}
{%- else -%}
{%- set readmeFile = 'readme.'~b.manifest.bundleSymbolicName.symbolicName~'.md' -%}
{%- endif -%}
{{prefix}}* [**{{title}}**]({{ path }}{{ readmeFile }}){{ (length(description) > 0) ? ': '~description : ''}}
{%- endfor -%}
{%- endif -%}
{%- endmacro %}

{# 
# CODE SNIPPETS
#}
{% macro printCodeSnippets (codeSnippets, level) -%}
{% import _self as this -%}
{%- set topLevel= default(level,3) -%}
{%- for cs in codeSnippets -%}
{%- if not (loop.first) -%}{{ '\n\n' }}{%- endif -%}
{%- if not empty(cs.title) -%}
{{ this.printTitle(trim(cs.title), topLevel)~'\n\n' }}
{%- endif -%}
{%- if not empty(cs.description) -%}
{{ trim(cs.description)~'\n\n' }}
{%- endif -%}
{%- if empty(cs.steps) -%}
```{{ trim(cs.programmingLanguage) }}
{{ trim(cs.codeSnippet) }}
```
{%- else -%}
{%- for s in cs.steps -%}
{%- if not (loop.first) -%}{{ '\n\n' }}{%- endif -%}
{%- if not empty(s.title) -%}
{{ this.printTitle(trim(s.title), topLevel+1)~'\n\n' }}
{%- endif -%}
{%- if not empty(s.description) -%}
{{ trim(s.description)~'\n\n' }}
{%- endif -%}
```{{ trim(s.programmingLanguage) }}
{{ trim(s.codeSnippet) }}
```
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endmacro %}

{# 
# VENDOR
#}
{% macro printVendor (vendor, address) -%}
{%- import _self as this -%}
{%- if length(vendor) == 0 -%}
{%- if address.type == 'postal' -%}
{{ address.address }}
{%- else -%}
[{{ address.address }}]({{ address.address }})
{%- endif -%}
{%- elseif empty(address) -%}
{{ vendor }}
{%- else -%}
{{ vendor }} -
{%- if address.type == 'url' -%}
{{ ' ' }}[{{ address.address }}]({{ address.address }})
{%- elseif address.type == 'email' -%}
{{ ' ' }}[{{ address.address }}](mailto:{{ address.address }})
{%- else -%}
{{ ' ' }}{{ address.address -}}
{%- endif -%}
{%- endif -%}
{%- endmacro %}

{# 
# GOGO COMMANDs
#}
{% macro printGogoCommands (commandScopes) -%}
{%- import _self as this -%}
{%- for scope in commandScopes -%}
{%- for function in scope.functions -%}
{%- if not empty(scope.name) -%}
{{ this.printTitle(scope.name~':'~function.name,3) }}
{%- else -%}
{{ this.printTitle(function.name,3) }}
{%- endif -%}
{%- for method in function.methods -%}
{{- '\n\n' -}}**Synopsis**{{- '\n\n' -}}
{{- (scope.name is empty)? '`' : '`'~scope.name~':' -}}
{{- function.name -}}
{{- (method.options is empty)? '' : ' [OPTIONS]' -}}
{%- if not empty(method.arguments) -%}
{%- for argument in method.arguments -%}
{{- ' '~argument.name|upper -}}{{- (argument.multiValue)? '...' : '' -}}
{%- endfor -%}
{%- endif -%}
{{- '`' -}}
{%- if not (method.description is empty) -%}
{{- '\n\n' -}}**Description**{{- '\n\n' -}}
{{-  method.description }}
{%- endif -%}
{%- if not (method.arguments is empty) -%}
{{ '\n\n' }}**Arguments**{{ '\n' -}}
{%- for argument in method.arguments -%}
{{ '\n' }}{{- '* `'~argument.name|upper~'`  ' }}{{ (argument.description is empty)? '' : argument.description -}}
{% endfor -%}
{%- endif -%}
{%- if not (method.options is empty) -%}
{{ '\n\n' }}**Options**{{ '\n' }}
{%- for option in method.options -%}
{{ '\n* `' }}{%- for name in option.names %}{{ name }}{{ (option.isFlag)? '': ' VALUE'}}{{ (option.isFlag and option.multiValue)? '...': ''}}{{ (loop.last )? '' : ', '}}{%- endfor -%}
{{ '`  ' }}{{(option.description is empty)? '' : option.description -}}
{%- endfor -%}
{%- endif -%}
{%- if not (loop.last) -%}
{{-  '\n\n---' -}}
{%- endif -%}
{%- endfor -%}
{%- if not (loop.last) -%}
{{-  '\n\n' -}}
{%- endif -%}
{%- endfor -%}
{%- if not (loop.last) -%}
{{-  '\n\n' -}}
{%- endif -%}
{%- endfor -%}
{%- endmacro %}
