package com.bloatit.web.pages.master;

import static com.bloatit.framework.webprocessor.context.Context.tr;

import com.bloatit.framework.FrameworkConfiguration;
import com.bloatit.framework.exceptions.lowlevel.RedirectException;
import com.bloatit.framework.utils.Image;
import com.bloatit.framework.webprocessor.PageNotFoundException;
import com.bloatit.framework.webprocessor.components.HtmlDiv;
import com.bloatit.framework.webprocessor.components.HtmlGenericElement;
import com.bloatit.framework.webprocessor.components.HtmlImage;
import com.bloatit.framework.webprocessor.components.HtmlSpan;
import com.bloatit.framework.webprocessor.components.PlaceHolderElement;
import com.bloatit.framework.webprocessor.components.advanced.HtmlClearer;
import com.bloatit.framework.webprocessor.components.meta.HtmlBranch;
import com.bloatit.framework.webprocessor.components.meta.HtmlElement;
import com.bloatit.framework.webprocessor.context.Context;
import com.bloatit.framework.webprocessor.masters.Page;
import com.bloatit.framework.webprocessor.url.Url;
import com.bloatit.web.WebConfiguration;
import com.bloatit.web.url.IndexPageUrl;

public abstract class MasterPage extends Page {

    private HtmlBranch notifications;
    private final HtmlDiv notificationBlock;

    public MasterPage(final Url url) {
        super(url);
        notifications = null;
        notificationBlock = new HtmlDiv("notifications");
    }

    // -----------------------------------------------------------------------
    // Template method pattern: Abstract methods
    // -----------------------------------------------------------------------
    protected abstract HtmlElement createBodyContent() throws RedirectException;

    protected abstract String createPageTitle();

    protected abstract Breadcrumb createBreadcrumb();

    // There is a default behavior here. You can overload it.
    protected HtmlElement createBodyContentOnParameterError() throws RedirectException {
        throw new PageNotFoundException();
    }

    // -----------------------------------------------------------------------
    // Template method pattern: Implementation
    // -----------------------------------------------------------------------
    @Override
    protected final HtmlElement createBody() throws RedirectException {
        final HtmlGenericElement body = createBodyElement();
        final HtmlBranch header = new HtmlDiv("header").setId("header");
        body.add(header);
        final HtmlBranch headerContent = new HtmlDiv("header_content").setId("header_content");
        header.add(headerContent);
        header.add(new HtmlClearer());

        headerContent.add(new SessionBar());
        headerContent.add(generateLogo());

        body.add(new Menu());

        final HtmlBranch page = new HtmlDiv("page").setId("page");
        body.add(page);

        final HtmlBranch content = new HtmlDiv().setId("content");
        page.add(content);

        final PlaceHolderElement breacrumbPlaceHolder = new PlaceHolderElement();
        content.add(breacrumbPlaceHolder);
        content.add(notificationBlock);

        // Template method pattern.
        content.add(createBodyContent());

        body.add(new Footer());

        breacrumbPlaceHolder.add(createBreadcrumb().toHtmlElement());
        return body;
    }

    @Override
    protected final HtmlElement createBodyOnParameterError() throws RedirectException {
        final HtmlGenericElement body = createBodyElement();
        body.add(createBodyContentOnParameterError());
        return body;

    }

    private HtmlGenericElement createBodyElement() {
        final HtmlGenericElement body = new HtmlGenericElement("body");
        body.addCustomJs(FrameworkConfiguration.getJsJquery());
        body.addCustomJs(FrameworkConfiguration.getJsJquery());
        body.addCustomJs(FrameworkConfiguration.getJsSelectivizr());
        body.addCustomJs(FrameworkConfiguration.getJsFlexie());
        return body;
    }

    @Override
    protected final String getTitle() {
        return "Elveos – " + createPageTitle();
    }

    @Override
    protected final void addNotification(final HtmlNotification note) {
        if (notifications == null) {
            notifications = new HtmlDiv().setId("notifications");
            notificationBlock.add(notifications);
        }
        notifications.add(note);
    }

    private HtmlElement generateLogo() {
        Context.getSession();

        final HtmlDiv logoDiv = new HtmlDiv("logo", "logo");

        final HtmlImage logoImage = new HtmlImage(new Image(WebConfiguration.getImgLogo()), tr("elveos.org logo"));
        logoImage.setCssClass("logo_elveos");

        logoDiv.add(new IndexPageUrl().getHtmlLink(logoImage));

        final HtmlSpan logoTextDiv = new HtmlSpan("logo_text", "logo_text");
        logoTextDiv.addText(tr("the cooperative platform for free software funding"));

        logoDiv.add(logoTextDiv);

        return logoDiv;
    }
}

@@ -2315,60 +2315,660 @@
     
-final HtmlGenericElement body = createBodyElement();
+return doCreateBody(createBodyContent());%0A    %7D%0A%0A    @Override%0A    protected final HtmlElement createBodyOnParameterError() throws RedirectException %7B%0A        return doCreateBody(createBodyContentOnParameterError());%0A    %7D%0A%0A    private HtmlElement doCreateBody(final HtmlElement bodyContent) throws RedirectException %7B%0A        final HtmlGenericElement body = new HtmlGenericElement(%22body%22);%0A        body.addCustomJs(FrameworkConfiguration.getJsJquery());%0A        body.addCustomJs(FrameworkConfiguration.getJsJquery());%0A        body.addCustomJs(FrameworkConfiguration.getJsSelectivizr());%0A        body.addCustomJs(FrameworkConfiguration.getJsFlexie());%0A
 %0A   
@@ -3759,31 +3759,25 @@
 content.add(
-createB
+b
 odyContent()
@@ -3774,18 +3774,16 @@
 yContent
-()
 );%0A%0A    
@@ -3913,669 +3913,8 @@
  %7D%0A%0A
-    @Override%0A    protected final HtmlElement createBodyOnParameterError() throws RedirectException %7B%0A        final HtmlGenericElement body = createBodyElement();%0A        body.add(createBodyContentOnParameterError());%0A        return body;%0A%0A    %7D%0A%0A    private HtmlGenericElement createBodyElement() %7B%0A        final HtmlGenericElement body = new HtmlGenericElement(%22body%22);%0A        body.addCustomJs(FrameworkConfiguration.getJsJquery());%0A        body.addCustomJs(FrameworkConfiguration.getJsJquery());%0A        body.addCustomJs(FrameworkConfiguration.getJsSelectivizr());%0A        body.addCustomJs(FrameworkConfiguration.getJsFlexie());%0A        return body;%0A    %7D%0A%0A
     

make sure all the page is computed on redefined createBodyContentOnParameterError()