/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.isis.viewer.wicket.ui.errors;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.request.resource.JavaScriptResourceReference;

import org.apache.isis.applib.RecoverableException;
import org.apache.isis.core.commons.internal.base._Strings;
import org.apache.isis.core.security.authentication.MessageBroker;
import org.apache.isis.core.webapp.context.IsisWebAppCommonContext;

/**
 * Attach to any Ajax button that might trigger a notification (ie calls
 * {@link MessageBroker#addMessage(String)}, {@link MessageBroker#addWarning(String)},
 * {@link MessageBroker#setApplicationError(String)} or throws an {@link RecoverableException}).
 *
 * <p>
 * Attach using the standard Wicket code:
 * <pre>
 * Button editButton = new AjaxButton(ID_EDIT_BUTTON, Model.of("Edit")) { ... }
 * editButton.add(new JGrowlBehaviour());
 * </pre>
 */
public class JGrowlBehaviour extends AbstractDefaultAjaxBehavior {

    private static final long serialVersionUID = 1L;
    private final transient IsisWebAppCommonContext commonContext;
    
    public JGrowlBehaviour(IsisWebAppCommonContext commonContext) {
        this.commonContext = commonContext;
    }

    @Override
    protected void respond(AjaxRequestTarget target) {
        String feedbackMsg = JGrowlUtil.asJGrowlCalls(getMessageBroker());
        if(!_Strings.isNullOrEmpty(feedbackMsg)) {
            target.appendJavaScript(feedbackMsg);
        }
    }

    @Override
    public void renderHead(Component component, IHeaderResponse response) {
        super.renderHead(component, response);

        renderFeedbackMessages(response);
    }

    public void renderFeedbackMessages(IHeaderResponse response) {
        response.render(JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(JGrowlBehaviour.class, "js/bootstrap-growl.js")));

        String feedbackMsg = JGrowlUtil.asJGrowlCalls(getMessageBroker());
        if(!_Strings.isNullOrEmpty(feedbackMsg)) {
            response.render(OnDomReadyHeaderItem.forScript(feedbackMsg));
        }
    }


    protected MessageBroker getMessageBroker() {
        return commonContext.getAuthenticationSession().getMessageBroker();
    }

    

}

@@ -869,16 +869,44 @@
 rrors;%0A%0A
+import java.util.Optional;%0A%0A
 import o
@@ -1397,16 +1397,91 @@
 trings;%0A
+import org.apache.isis.core.security.authentication.AuthenticationSession;%0A
 import o
@@ -2447,32 +2447,99 @@
 rget) %7B%0A        
+%0A        getMessageBroker().ifPresent(messageBroker-%3E%7B%0A            
 String feedbackM
@@ -2560,36 +2560,33 @@
 l.asJGrowlCalls(
-getM
+m
 essageBroker());
@@ -2573,34 +2573,32 @@
 ls(messageBroker
-()
 );%0A        if(!_
@@ -2584,32 +2584,36 @@
 roker);%0A        
+  
+  
 if(!_Strings.isN
@@ -2651,16 +2651,20 @@
         
+    
 target.a
@@ -2693,16 +2693,20 @@
 ckMsg);%0A
+    
         
@@ -2703,24 +2703,36 @@
           %7D%0A
+        %7D);%0A
     %7D%0A%0A    @
@@ -3004,16 +3004,33 @@
 .render(
+%0A                
 JavaScri
@@ -3037,24 +3037,41 @@
 ptHeaderItem
+%0A                
 .forReferenc
@@ -3152,24 +3152,91 @@
 wl.js%22)));%0A%0A
+        getMessageBroker().ifPresent(messageBroker-%3E%7B%0A        %0A    
         Stri
@@ -3277,20 +3277,17 @@
 wlCalls(
-getM
+m
 essageBr
@@ -3290,18 +3290,16 @@
 geBroker
-()
 );%0A     
@@ -3293,36 +3293,39 @@
 roker);%0A        
+   
+ 
 if(
-!
 _Strings.isNullO
@@ -3312,37 +3312,34 @@
  if(_Strings.isN
-ullOr
+ot
 Empty(feedbackMs
@@ -3336,32 +3336,36 @@
 feedbackMsg)) %7B%0A
+    
             resp
@@ -3434,16 +3434,53 @@
     
+    
 %7D%0A    
+        %0A        %7D);%0A        %0A    
 %7D
-%0A
 %0A%0A  
@@ -3491,16 +3491,25 @@
 otected 
+Optional%3C
 MessageB
@@ -3513,16 +3513,17 @@
 geBroker
+%3E
  getMess
@@ -3551,16 +3551,36 @@
  return 
+Optional.ofNullable(
 commonCo
@@ -3611,17 +3611,62 @@
 ession()
-.
+)%0A                .map(AuthenticationSession::
 getMessa
@@ -3673,17 +3673,16 @@
 geBroker
-(
 );%0A    %7D
@@ -3687,12 +3687,6 @@
  %7D%0A%0A
-    %0A%0A
 %7D%0A

fix possible NPE with JGrowlBehaviour