/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.core.persistence;

import com.wegas.core.ejb.RequestManager;
import com.wegas.core.persistence.game.Game;
import com.wegas.core.persistence.game.GameModel;
import com.wegas.core.persistence.game.Player;
import com.wegas.core.persistence.game.Team;
import com.wegas.core.persistence.variable.VariableDescriptor;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PreRemove;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Maxence Laurent (maxence.laurent at gmail.com)
 */
public class EntityListener {

    private static final Logger logger = LoggerFactory.getLogger(EntityListener.class);

    @Inject
    private RequestManager requestManager;

    @PostPersist
    void onPostPersist(Object o) {
        logger.error("Post Persist: " + o);
        if (o instanceof Broadcastable) {
            Broadcastable b = (Broadcastable) o;
            Map<String, List<AbstractEntity>> entities = b.getEntities();
            if (b instanceof Team || b instanceof Player){
                requestManager.addUpdatedEntities(entities);
            } else {
                logger.warn("Unhandled new broadcastable entity: " + b);
            }
        }
    }

    @PostUpdate
    void onPostUpdate(Object o) {
        logger.error("POST UPDATE: " + o);
        if (o instanceof Broadcastable) {
            Broadcastable b = (Broadcastable) o;
            if (b instanceof GameModel) {
                /* Since a serialized gameModel differs according to whom request it...
                 it's not possible to broadcast the new version -> Outdate it */
                requestManager.addOutofdateEntities(b.getEntities());
            } else if (b instanceof AbstractEntity) {
                logger.error("Propagate: " + b);
                Map<String, List<AbstractEntity>> entities = b.getEntities();
                requestManager.addUpdatedEntities(entities);
            }
        }
    }

    @PreRemove
    void onPreRemove(Object o) {
        logger.error("Pre Remove: " + o);
        if (o instanceof Broadcastable) {
            Broadcastable b = (Broadcastable) o;
            Map<String, List<AbstractEntity>> entities = b.getEntities();
            if (b instanceof VariableDescriptor || b instanceof Game){
                //logger.error(("Entities: " + entities.size()));
                requestManager.addDestroyedEntities(entities);
            } else if (b instanceof Team || b instanceof Player){
                requestManager.addUpdatedEntities(entities);
            } else {
                logger.warn("Unhandled destroyed broadcastable entity: " + b);
            }
        }
    }
}

@@ -1037,21 +1037,21 @@
  logger.
-error
+trace
 (%22Post P
@@ -1387,36 +1387,37 @@
          logger.
-warn
+debug
 (%22Unhandled new 
@@ -1542,21 +1542,21 @@
  logger.
-error
+trace
 (%22POST U
@@ -2015,29 +2015,29 @@
      logger.
-error
+debug
 (%22Propagate:
@@ -2278,21 +2278,21 @@
  logger.
-error
+trace
 (%22Pre Re
@@ -2561,25 +2561,24 @@
     
-//
 logger.
-error
+debug
 ((%22
+#
 Enti
@@ -2727,32 +2727,97 @@
 anceof Player)%7B%0A
+                logger.debug((%22#Entities: %22 + entities.size()));%0A
                 
@@ -2909,12 +2909,13 @@
 ger.
-warn
+debug
 (%22Un

Fix CI