package de.larmic.butterfaces.component.renderkit.html_basic.ajax;

import de.larmic.butterfaces.component.html.ajax.WaitingPanel;
import de.larmic.butterfaces.component.partrenderer.RenderUtils;
import de.larmic.butterfaces.component.partrenderer.StringUtils;
import de.larmic.butterfaces.component.base.renderer.HtmlDeprecatedBasicRenderer;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import java.io.IOException;

/**
 * Created by larmic on 31.07.14.
 */
@FacesRenderer(componentFamily = WaitingPanel.COMPONENT_FAMILY, rendererType = WaitingPanel.RENDERER_TYPE)
public class WaitingPanelRenderer extends HtmlDeprecatedBasicRenderer {

    public static final int DEFAULT_WAITING_PANEL_DELAY = 500;

    @Override
    public void encodeBegin(final FacesContext context, final UIComponent component) throws IOException {
        rendererParamsNotNull(context, component);

        if (!shouldEncode(component)) {
            return;
        }

        final ResponseWriter writer = context.getResponseWriter();
        final WaitingPanel waitingPanel = (WaitingPanel) component;

        final String style = waitingPanel.getStyle();
        final String styleClass = waitingPanel.getStyleClass();

        writer.startElement(ELEMENT_DIV, component);

        this.writeIdAttribute(context, writer, component);

        if (StringUtils.isNotEmpty(style)) {
            writer.writeAttribute("style", style, null);
        }

        if (StringUtils.isNotEmpty(styleClass)) {
            writer.writeAttribute("class", "butter-component-waitingPanel " + styleClass, null);
        } else {
            writer.writeAttribute("class", "butter-component-waitingPanel", null);
        }
    }

    @Override
    public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
        rendererParamsNotNull(context, component);

        if (!shouldEncode(component)) {
            return;
        }

        final ResponseWriter writer = context.getResponseWriter();
        final WaitingPanel waitingPanel = (WaitingPanel) component;

        writer.endElement(ELEMENT_DIV); // component div

        final String pluginFunctionCall = "waitingPanel(" + createButterTreeJQueryParameter(waitingPanel) + ")";
        RenderUtils.renderJQueryPluginCall(component.getClientId(), pluginFunctionCall, writer, component);
    }

    private String createButterTreeJQueryParameter(final WaitingPanel waitingPanel) {
        final int waitingPanelDelay = getWaitingPanelDelay(waitingPanel);

        return "{waitingPanelDelay: '" + waitingPanelDelay + "', blockpage: " + waitingPanel.isBlockpage() + "}";
    }

    private int getWaitingPanelDelay(final WaitingPanel waitingPanel) {
        if (waitingPanel.getDelay() != null) {
            return waitingPanel.getDelay() > 0 ? waitingPanel.getDelay() : 0;
        }

        return DEFAULT_WAITING_PANEL_DELAY;
    }
}

@@ -61,16 +61,88 @@
 .ajax;%0A%0A
+import de.larmic.butterfaces.component.base.renderer.HtmlBasicRenderer;%0A
 import d
@@ -196,16 +196,16 @@
 gPanel;%0A
-
 import d
@@ -329,90 +329,8 @@
 ils;
-%0Aimport de.larmic.butterfaces.component.base.renderer.HtmlDeprecatedBasicRenderer;
 %0A%0Aim
@@ -722,18 +722,8 @@
 Html
-Deprecated
 Basi
@@ -931,87 +931,34 @@
     
-rendererParamsNotNull(context, component);%0A%0A        if (!shouldEncode(component
+if (!component.isRendered(
 )) %7B
@@ -1865,87 +1865,34 @@
     
-rendererParamsNotNull(context, component);%0A%0A        if (!shouldEncode(component
+if (!component.isRendered(
 )) %7B

BUT-389 Convert ActivateLibrariesRenderer.java to HtmlBasicRenderer.java