/*
 * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution for a full
 * listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.zanata.action;

import java.io.Serializable;

import lombok.extern.slf4j.Slf4j;

import javax.enterprise.event.Observes;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.scope.WindowScoped;
import org.zanata.events.AlreadyLoggedInEvent;
import org.zanata.events.LoginFailedEvent;
import org.zanata.events.LoginSuccessfulEvent;
import org.zanata.events.NotLoggedInEvent;
import org.zanata.events.UserCreatedEvent;
import org.zanata.ui.faces.FacesMessages;

import static javax.enterprise.event.Reception.IF_EXISTS;

/**
 * Some of the event observers are migrated from org.jboss.seam.security.FacesSecurityEvents
 */
@WindowScoped
@Slf4j
public class AuthenticationEvents implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private FacesMessages facesMessages;

    public void createSuccessful(@Observes(notifyObserver = IF_EXISTS)
    UserCreatedEvent userCreatedEvent) {
        log.info("Account {} created",
                userCreatedEvent.getUser().getUsername());
    }

    public void loginInSuccessful(
            @Observes(notifyObserver = IF_EXISTS) LoginSuccessfulEvent event) {
        log.debug("Account logged in successfully");
        facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
                "org.jboss.seam.loginSuccessful", event.getName());
    }

    public void loginFailed(
            @Observes(notifyObserver = IF_EXISTS) LoginFailedEvent event) {
        log.debug("login failed", event.getException());
        facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,
                "org.jboss.seam.loginFailed");
    }

    public void notLoggedIn(
            @Observes(notifyObserver = IF_EXISTS) NotLoggedInEvent event) {
        facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
                "org.jboss.seam.NotLoggedIn");
    }

    public void alreadyLoggedIn(
            @Observes(notifyObserver = IF_EXISTS) AlreadyLoggedInEvent event) {
        facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_WARN,
                "org.jboss.seam.AlreadyLoggedIn");
    }
}

@@ -1064,16 +1064,59 @@
 Slf4j;%0A%0A
+import javax.enterprise.context.Dependent;%0A
 import j
@@ -1279,16 +1279,69 @@
 Scoped;%0A
+import org.apache.deltaspike.core.util.ContextUtils;%0A
 import o
@@ -1602,67 +1602,8 @@
 s;%0A%0A
-import static javax.enterprise.event.Reception.IF_EXISTS;%0A%0A
 /**%0A
@@ -1700,28 +1700,25 @@
 ts%0A */%0A@
-WindowScoped
+Dependent
 %0A@Slf4j%0A
@@ -1931,40 +1931,8 @@
 rves
-(notifyObserver = IF_EXISTS)%0A   
  Use
@@ -2108,58 +2108,17 @@
 ful(
-%0A            @Observes(notifyObserver = IF_EXISTS)
+@Observes
  Log
@@ -2193,24 +2193,92 @@
 essfully%22);%0A
+        if (ContextUtils.isContextActive(WindowScoped.class)) %7B%0A    
         face
@@ -2345,32 +2345,36 @@
                 
+    
 %22org.jboss.seam.
@@ -2401,32 +2401,42 @@
 ent.getName());%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2458,58 +2458,17 @@
 led(
-%0A            @Observes(notifyObserver = IF_EXISTS)
+@Observes
  Log
@@ -2551,37 +2551,126 @@
 ));%0A        
-facesMessages
+if (ContextUtils.isContextActive(WindowScoped.class)) %7B%0A            facesMessages%0A                    
 .addFromReso
@@ -2717,32 +2717,44 @@
                 
+  
+          
 %22org.jboss.seam.
@@ -2764,24 +2764,34 @@
 inFailed%22);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2817,106 +2817,154 @@
 dIn(
-%0A            @Observes(notifyObserver = IF_EXISTS) NotLoggedInEvent event) %7B%0A        facesMessages
+@Observes NotLoggedInEvent event) %7B%0A        if (ContextUtils.isContextActive(WindowScoped.class)) %7B%0A            facesMessages%0A                    
 .add
@@ -3018,32 +3018,44 @@
                 
+   
+         
 %22org.jboss.seam.
@@ -3065,24 +3065,34 @@
 LoggedIn%22);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3122,110 +3122,158 @@
 dIn(
-%0A            @Observes(notifyObserver = IF_EXISTS) AlreadyLoggedInEvent event) %7B%0A        facesMessages
+@Observes AlreadyLoggedInEvent event) %7B%0A        if (ContextUtils.isContextActive(WindowScoped.class)) %7B%0A            facesMessages%0A                    
 .add
@@ -3327,32 +3327,44 @@
                 
+   
+         
 %22org.jboss.seam.
@@ -3378,20 +3378,30 @@
 LoggedIn%22);%0A
+        %7D%0A
     %7D%0A%7D%0A

fix AuthenticationEvents