package au.org.ala.ecodata.forms

/**
 * Created by baird on 18/10/13.
 */
class PrintModelWidgetRenderer implements ModelWidgetRenderer {

    @Override
    void renderLiteral(WidgetRenderContext context) {
        context.writer << "<span ${context.attributes.toString()}>${context.model.source}</span>"
    }

    @Override
    void renderText(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderNumber(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderBoolean(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderTextArea(WidgetRenderContext context) {
        context.writer << "<span class=\"span12 printed-form-field textarea\"></span>"
//        context.databindAttrs.add 'value', context.source
//        context.writer << "<textarea ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}'></textarea>"
    }

    @Override
    void renderSimpleDate(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderSelectOne(WidgetRenderContext context) {
        renderCheckboxes(context)
    }

    @Override
    void renderSelectMany(WidgetRenderContext context) {
        renderCheckboxes(context)
    }

    @Override
    void renderImage(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderImageDialog(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderEmbeddedImage(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderEmbeddedImages(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderAutocomplete(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderPhotoPoint(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderLink(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderDate(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderDocument(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderSpeciesSelect(WidgetRenderContext context) {
        defaultRender(context)
    }

    @Override
    void renderSelect2(WidgetRenderContext context) {
        defaultRender(context)
    }

    private void defaultRender(WidgetRenderContext context) {


        context.writer << "<span class=\"span12 printed-form-field\"></span>"
        // context.databindAttrs.add 'value', context.source
//        context.writer << "<input ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}' ${context.validationAttr} type='text' class='input-small'/>"
    }

    private void renderCheckboxes(WidgetRenderContext context) {
        context.labelAttributes.addClass 'checkbox-list-label '
        def constraints = context.source + '.constraints'
        context.writer << """
            <ul class="checkbox-list" data-bind="foreach: ${constraints}">
                <li>
                    <span class="printed-checkbox">&nbsp;</span>&nbsp;<span data-bind="text:\$data"></span>
                </li>
            </ul>
        """
    }

    @Override
    void renderCurrency(WidgetRenderContext context) {
       defaultRender(context)
    }

    @Override
    void renderSelect2Many(WidgetRenderContext context) {
        renderCheckboxes(context)
    }

    @Override
    void renderMultiInput(WidgetRenderContext context) {
        renderTextArea(context)
    }
}
