package adf.agent.communication;

import adf.agent.communication.standard.bundle.StandardMessageBundle;
import adf.component.communication.CommunicationMessage;
import adf.component.communication.MessageBundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MessageManager
{
    private int standardMessageClassCount;
    private int customMessageClassCount;
    private HashMap<Integer, Class<? extends CommunicationMessage>> messageClassMap;
    private HashMap<Class<? extends CommunicationMessage>, Integer> messageClassIDMap;
    private List<CommunicationMessage> sendMessageList;
    private List<CommunicationMessage> receivedMessageList;
    private int heardAgentHelpCount;

    public MessageManager()
    {
        standardMessageClassCount = 1;   // 00001
        customMessageClassCount = 16;    // 10000
        messageClassMap = new HashMap<>(32);
        messageClassIDMap = new HashMap<>(32);
        sendMessageList = new ArrayList<>();
        this.receivedMessageList = new ArrayList<>();
        heardAgentHelpCount = 0;
    }

    public boolean registerMessageClass(int index, Class<? extends CommunicationMessage> messageClass)
    {
        if (index > 31)
        {
            throw new IllegalArgumentException("index maximum is 31");
        }
        if (messageClassMap.containsKey(index))
        {
            //throw new IllegalArgumentException("index(" + index + ") is already registrated");
            System.out.println("index(" + index + ") is already registrated/"+ messageClass.getName() +" is ignored");
            return false;
        }

        messageClassMap.put(index, messageClass);
        messageClassIDMap.put(messageClass, index);

        return true;
    }

    public void registerMessageBundle(MessageBundle messageBundle)
    {
        for (Class<? extends CommunicationMessage> messageClass : messageBundle.getMessageClassList())
        {
            this.registerMessageClass(
                    (messageBundle.getClass().equals(StandardMessageBundle.class) ? standardMessageClassCount++ : customMessageClassCount++),
                    messageClass);
        }
    }

    public Class<? extends CommunicationMessage> getMessageClass(int index)
    {
        if (!messageClassMap.containsKey(index))
        {
            return null;
        }

        return messageClassMap.get(index);
    }

    public int getMessageClassIndex(CommunicationMessage message)
    {
        if (!messageClassMap.containsValue(message.getClass()))
        {
            throw new IllegalArgumentException(message.getClass().getName() + " isnot registorated to manager");
        }

        return messageClassIDMap.get(message.getClass());
    }

    public void addMessage(CommunicationMessage message)
    {
        sendMessageList.add(message);
    }

    public List<CommunicationMessage> getSendMessageList()
    {
        return this.sendMessageList;
    }

    public void addReceivedMessage(CommunicationMessage message)
    {
        receivedMessageList.add(message);
    }

    public List<CommunicationMessage> getReceivedMessageList()
    {
        return this.receivedMessageList;
    }

    public void addHeardAgentHelpCount()
    {
        this.heardAgentHelpCount++;
    }

    public int getHeardAgentHelpCount()
    {
        return this.heardAgentHelpCount;
    }

    public void refresh()
    {
        this.sendMessageList.clear();
        this.receivedMessageList.clear();
        this.heardAgentHelpCount = 0;
    }
}

@@ -233,16 +233,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -3206,32 +3206,492 @@
 ageList;%0A    %7D%0A%0A
+    public List%3CCommunicationMessage%3E getReceivedMessageList(Class%3C? extends CommunicationMessage%3E... messageClasses)%0A    %7B%0A        List%3CCommunicationMessage%3E resultList = new ArrayList%3C%3E();%0A        for (CommunicationMessage message : this.receivedMessageList)%0A        %7B%0A            if (Arrays.asList(messageClasses).contains(message.getClass()))%0A            %7B%0A                resultList.add(message);%0A            %7D%0A        %7D%0A        return resultList;%0A    %7D%0A%0A
     public void 

add method to MessageManager