from __future__ import print_function
from __future__ import absolute_import
from builtins import object
from .templates import get_template
from w20e.forms.rendering.interfaces import IControlRenderer
from zope.interface import implementer


@implementer(IControlRenderer)
class RichTextRenderer(object):

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

        """ render Input to HTML """

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

        fmtmap['value'] = form.data[renderable.bind] or ""
        fmtmap['richconfig'] = renderer.opts.get('richconfig', '')
        fmtmap['richclass'] = 'wysiwyg'
        fmtmap['cols'] = renderable.cols or 80
        fmtmap['rows'] = renderable.rows or 3

        # added to guarantee backwards compatibilty (WGH)
        if not 'cols' in fmtmap:
            fmtmap['cols'] = '30'

        print(get_template('richtext')(
            control=renderable,
            fmtmap=fmtmap), file=out)
