{# Macro for Function Inputs #}
{%- macro paramvalue(param) -%}{% if param.input_type in ["select", "multiselect"] %}self.get_select_param(kwargs.get("{{param.name}}")){% elif param.input_type in ["textarea"] %}self.get_textarea_param(kwargs.get("{{param.name}}")){% else %}kwargs.get("{{param.name}}"){% endif %}{%- endmacro -%}

{# Macro for Function Input Comment #}
{%- macro paramcomment(param) -%}{{param.input_type}}{%if param.input_type in ["select", "multiselect"]%}, values: "{{param["values"]|list|map(attribute="label")|join('", "')}}"{%endif%}{%- endmacro -%}

# -*- coding: utf-8 -*-
# pragma pylint: disable=unused-argument, no-self-use
"""Function implementation"""

import logging
from resilient_circuits import ResilientComponent, function, handler, StatusMessage, FunctionResult, FunctionError

PACKAGE_NAME = "{{package_name}}"


class FunctionComponent(ResilientComponent):
    """Component that implements Resilient function '{{name}}''"""

    def __init__(self, opts):
        """constructor provides access to the configuration options"""
        super(FunctionComponent, self).__init__(opts)
        self.options = opts.get(PACKAGE_NAME, {})

    @handler("reload")
    def _reload(self, event, opts):
        """Configuration options have changed, save new values"""
        self.options = opts.get(PACKAGE_NAME, {})

    @function("{{name}}")
    def _{{name}}_function(self, event, *args, **kwargs):
        """Function: {{description.content}}"""
        try:

            # Get the wf_instance_id of the workflow this Function was called in
            wf_instance_id = event.message["workflow_instance"]["workflow_instance_id"]

            yield StatusMessage("Starting '{{name}}' running in workflow '{0}'".format(wf_instance_id))

            # Get the function parameters:
            {% for fn_input in inputs %}
            {{fn_input.name}} = {{paramvalue(fn_input)}}  # {{paramcomment(fn_input)}}
            {% endfor %}

            log = logging.getLogger(__name__)
            {% for fn_input in inputs %}
            log.info("{{fn_input.name}}: %s", {{fn_input.name}})
            {% endfor %}

            ##############################################
            # PUT YOUR FUNCTION IMPLEMENTATION CODE HERE #
            ##############################################

            yield StatusMessage("Finished '{{name}}' that was running in workflow '{0}'".format(wf_instance_id))

            results = {
                "content": "xyz"
            }

            # Produce a FunctionResult with the results
            yield FunctionResult(results)
        except Exception:
            yield FunctionError()
