package hotelmania.group4.behaviours;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import hotelmania.group4.HotelManiaAgent;
import hotelmania.group4.utils.Utils;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * This is just better base class for simple behaviour because it behaves correctly and blocks for the message.
 *
 * @author Marek Lewandowski <marek.lewandowski@icompass.pl>
 * @since 20/04/14
 */
public abstract class EmseSimpleBehaviour extends SimpleBehaviour {

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

    private HotelManiaAgent agent;

    public EmseSimpleBehaviour (HotelManiaAgent agent) {
        super(agent);
        Preconditions.checkNotNull(agent);
        this.agent = agent;
    }

    @Override
    public void action () {
        final List<MessageTemplate> messageTemplates = getMessageTemplates();
        final ACLMessage receive = getAgent().receive(Utils.messageTemplateConjunction(messageTemplates));
        logger.debug("Received message {}", receive.toString());
        final Optional<ACLMessage> aclMessageOptional = Optional.fromNullable(receive);
        if (aclMessageOptional.isPresent()) {
            final ACLMessage message = aclMessageOptional.get();
            final MessageStatus messageStatus = processMessage(message);
            if (messageStatus.equals(MessageStatus.NOT_PROCESSED)) {
                logger.debug("Message has not been processed. Putting it back to message queue for other behaviours");
                getAgent().putBack(message);
            }
        } else {
            block();
        }
    }

    protected abstract List<MessageTemplate> getMessageTemplates ();

    protected abstract MessageStatus processMessage (ACLMessage message);

    protected HotelManiaAgent getHotelManiaAgent () {
        return agent;
    }

    protected void sendMessage (ACLMessage message) {
        getHotelManiaAgent().sendMessage(message);
    }
}

@@ -1185,73 +1185,8 @@
 ));%0A
-        logger.debug(%22Received message %7B%7D%22, receive.toString());%0A
     
@@ -1311,24 +1311,93 @@
 resent()) %7B%0A
+            logger.debug(%22Received message %7B%7D%22, receive.toString());%0A
             

Fix - log it when there is actual message because it could be null