package br.net.mirante.singular.form.wicket.mapper.richtext;

import br.net.mirante.singular.commons.util.Loggable;
import br.net.mirante.singular.form.SingularFormException;
import br.net.mirante.singular.form.wicket.WicketBuildContext;
import br.net.mirante.singular.form.wicket.model.SInstanceValueModel;
import br.net.mirante.singular.util.wicket.util.JavaScriptUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.template.PackageTextTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static br.net.mirante.singular.util.wicket.jquery.JQuery.$;
import static br.net.mirante.singular.util.wicket.util.WicketUtils.$b;
import static org.apache.commons.lang3.StringUtils.EMPTY;

public class PortletRichTextPanel extends Panel implements Loggable {

    private static String HTML_NEW_TAB;

    static {
        HTML_NEW_TAB = Optional.ofNullable(PortletRichTextPanel.class.getResourceAsStream("PortletRichTextNewTab.html"))
                .map(in -> {
                    try {
                        return JavaScriptUtils.javaScriptEscape(IOUtils.toString(in, "UTF-8"));
                    } catch (IOException e) {
                        throw new SingularFormException("Não foi possivel extrair o conteudo html", e);
                    }
                }).orElse(StringUtils.EMPTY);
    }

    private HiddenField hiddenInput;
    private Label       htmlContent;
    private Label       label;
    private String      hash;

    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        try (PackageTextTemplate packageTextTemplate = new PackageTextTemplate(getClass(), "PortletRichTextPanel.js")) {
            final Map<String, String> params = new HashMap<>();
            params.put("label", (String) label.getDefaultModel().getObject());
            params.put("htmlContainer", htmlContent.getMarkupId());
            params.put("hiddenInput", hiddenInput.getMarkupId());
            params.put("hash", hash);
            params.put("html", HTML_NEW_TAB);
            packageTextTemplate.interpolate(params);
            response.render(JavaScriptHeaderItem.forScript(packageTextTemplate.getString(), hash));
        } catch (IOException e) {
            getLogger().error(e.getMessage(), e);
        }
    }

    public PortletRichTextPanel(String id, WicketBuildContext ctx) {
        super(id);
        hash = String.valueOf(Math.abs(ctx.getCurrentInstance().getPathFull().hashCode()));
        build(ctx);
        addBehaviours();
    }

    private void addBehaviours() {
        add($b.onReadyScript(c -> $(htmlContent).append(".html(").append($(hiddenInput).append(".val())"))));
    }

    private void build(WicketBuildContext ctx) {
        add(label = new Label("label", Model.of(Optional.ofNullable(ctx.getCurrentInstance().asAtr().getLabel()).orElse(EMPTY))));
        add(htmlContent = new Label("htmlContent", new SInstanceValueModel<>(ctx.getModel())));
        add(hiddenInput = new HiddenField<>("hiddenInput", new SInstanceValueModel<>(ctx.getModel())));
        add(new Button("button") {
            @Override
            protected String getOnClickScript() {
                return "openNewTabWithCKEditor" + hash + "();";
            }
        });
        htmlContent.setEscapeModelStrings(false);
    }

}
@@ -54,32 +54,39 @@
 chtext;%0A%0Aimport 
+static 
 br.net.mirante.s
@@ -97,88 +97,35 @@
 lar.
-commons.util.Loggable;%0Aimport br.net.mirante.singular.form.SingularFormException
+util.wicket.jquery.JQuery.$
 ;%0Aim
@@ -121,32 +121,39 @@
 Query.$;%0Aimport 
+static 
 br.net.mirante.s
@@ -160,28 +160,28 @@
 ingular.
-form
+util
 .wicket.
 WicketBu
@@ -176,163 +176,192 @@
 ket.
+util.
 Wicket
-BuildContext;%0Aimport br.net.mirante.singular.form.wicket.model.SInstanceValueModel;%0Aimport br.net.mirante.singular.util.wicket.util.JavaScriptUtils
+Utils.$b;%0Aimport static org.apache.commons.lang3.StringUtils.EMPTY;%0A%0Aimport java.io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Optional
 ;%0A
+%0A
 impo
@@ -867,112 +867,121 @@
 ort 
-java.io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.O
+br.net.mirante.singular.commons.util.Loggable;%0Aimport br.net.mirante.singular.form.SingularFormExce
 ption
-al;%0A
+;
 %0Aimport 
 stat
@@ -968,39 +968,32 @@
 ception;%0Aimport 
-static 
 br.net.mirante.s
@@ -996,36 +996,36 @@
 te.singular.
-util
+form
 .wicket.
 jquery.JQuer
@@ -1008,39 +1008,42 @@
 form.wicket.
-jquery.JQuery.$
+WicketBuildContext
 ;%0Aimport sta
@@ -1031,39 +1031,32 @@
 Context;%0Aimport 
-static 
 br.net.mirante.s
@@ -1059,36 +1059,36 @@
 te.singular.
-util
+form
 .wicket.
 util.WicketU
@@ -1079,85 +1079,98 @@
 ket.
-util.WicketUtils.$b;%0Aimport static org.apache.commons.lang3.StringUtils.EMPTY
+model.SInstanceValueModel;%0Aimport br.net.mirante.singular.util.wicket.util.JavaScriptUtils
 ;%0A%0Ap
@@ -2894,11 +2894,21 @@
 ath.
-abs
+sqrt(Math.pow
 (ctx
@@ -2953,16 +2953,20 @@
 shCode()
+, 2)
 ));%0A    

ajuste para o sonar