package waiter;

import jason.asSemantics.*;
import jason.asSyntax.Trigger;
import java.util.Iterator;

public class WaiterAgent extends Agent 
{
    public Event selectEvent(java.util.Queue<Event> events) 
	{
		// Comportamiento por defecto
		//return super.selectEvent(events); 
		
		// Priorizar los mensajes de comunicacion
		Event aux, e = null;
		Iterator<Event> it = events.iterator();
		while (it.hasNext())
		{
			aux = it.next();
			if (aux.getTrigger().getLiteral().getFunctor().equals("auction"))
			{
				e = aux;
				break;
			}
		}
		if (e == null)
			e = events.poll();
		else
			events.remove(e);
		//System.out.println("Event selected: " + e);
		return e;
	}

	public Intention selectIntention(java.util.Queue<Intention> intentions) 
	{
		//Intention i = intentions.poll(); 
		//System.out.println("Intention selected: " + i);
		//return i;

		// Priorizar las acciones sociales
		Unifier un = new Unifier();

		/*Literal l = Literal.parseLiteral("chatting");
		Literal res = getTS().getAg().findBel(l, un);
		if (res != null)
			return Intention.EmptyInt;*/

		Intention aux, i = null;
		Iterator<Intention> it = intentions.iterator();
		while (it.hasNext())
		{
			aux = it.next();
			if ( (aux.getIM(Trigger.parseTrigger("+!chat(_)"), un) != null) || 
				 (aux.getIM(Trigger.parseTrigger("+!chat(_,_)"), un) != null) )
			{
				i = aux;
				//System.out.println("-->Intention of chat: " + i);
				break;
			}
		}
		if (i == null)
			i = intentions.poll();
		else
			intentions.remove(i);
		//System.out.println("Intention selected: " + i);
		return i;
	}
}
