from __future__ import print_function
from __future__ import absolute_import

from io import BytesIO

from future import standard_library
standard_library.install_aliases()
from builtins import object
import codecs
from .templates import get_template
from w20e.forms.rendering.interfaces import IControlRenderer
from zope.interface import implementer


@implementer(IControlRenderer)
class FlowGroupRenderer(object):

    def render(self, renderer, form, renderable, out, **kwargs):

        """ Render flow group that flows horizontally or vertically """

        fmtmap = renderer.createFormatMap(form, renderable, **kwargs)

        str_out = BytesIO()
        sub_out = codecs.getwriter('utf-8')(str_out)

        for sub_renderable in renderable.getRenderables():
            renderer.render(form, sub_renderable, sub_out, **kwargs)

        print(get_template('flowgroup')(
            group=renderable,
            content=str_out.getvalue().decode("utf-8"),
            fmtmap=fmtmap
            ), file=out)
