// Copyright 2011 The Apache Software Foundation
//
// Licensed 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.tapestry5.corelib.components;

import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.alerts.Alert;
import org.apache.tapestry5.alerts.AlertStorage;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.RequestParameter;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.InitializationPriority;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

/**
 * Renders out an empty {@code <div>} element and provides JavaScript initialization to make the element
 * the container for alerts. After rendering markup (and initialization JavaScript), it
 * {@linkplain org.apache.tapestry5.alerts.AlertStorage#dismissNonPersistent() removes all non-persistent alerts).
 *
 * @tapestrydoc
 * @since 5.3
 */
public class Alerts implements ClientElement
{
    @Inject
    private ComponentResources resources;

    @Environmental
    private JavaScriptSupport javaScriptSupport;

    @SessionState(create = false)
    private AlertStorage storage;

    private String clientId;

    public String getClientId()
    {
        return clientId;
    }

    boolean beginRender(MarkupWriter writer)
    {
        clientId = javaScriptSupport.allocateClientId(resources);

        writer.element("div", "id", clientId);
        writer.end();

        JSONObject spec = new JSONObject("id", clientId,
                "dismissURL", resources.createEventLink("dismiss").toURI());

        javaScriptSupport.addInitializerCall(InitializationPriority.EARLY, "alertManager", spec);

        if (storage != null)
        {
            for (Alert alert : storage.getAlerts())
            {
                javaScriptSupport.addInitializerCall("addAlert", alert.toJSON());
            }

            storage.dismissNonPersistent();
        }


        return false;
    }

    Object onDismiss(@RequestParameter(value = "id", allowBlank = true) Long alertId)
    {
        if (alertId != null)
        {
            storage.dismiss(alertId);
        } else
        {
            storage.dismissAll();
        }

        return new JSONObject();
    }
}

@@ -2761,32 +2761,252 @@
  alertId)%0A    %7B%0A
+        // If the alert was created inside an Ajax request and AlertStorage did not previously%0A        // exist, it can be null when the dismiss event comes up from the client.%0A        if (storage != null)%0A        %7B%0A    
         if (aler
@@ -3018,34 +3018,42 @@
 = null)%0A        
+   
+ 
 %7B%0A
+    
             stor
@@ -3082,16 +3082,20 @@
         
+    
 %7D else%0A 
@@ -3093,34 +3093,41 @@
  %7D else%0A        
-%7B%0A
+    %7B%0A   
             stor
@@ -3114,32 +3114,33 @@
 %0A               
+ 
 storage.dismissA
@@ -3141,24 +3141,38 @@
 smissAll();%0A
+            %7D%0A
         %7D%0A%0A 

Handle the case where all alerts are dismissed but no AlertStorage object yet exists