/**
 * 
 */
package jadeinteraction;

import alice.mas.lab1.StaticUtils;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.StringTerm;
import jason.asSyntax.Term;
import jason.infra.jade.JadeRuntimeServices;

/**
 * @author danysk
 * 
 */
public class getservices extends DefaultInternalAction {

	@Override
	public Object execute(TransitionSystem ts, Unifier un, Term[] args)
			throws Exception {
		StaticUtils.searchServiceProviders(a, service)
		
		try {
			// get a reference to the JADE agent that represents this Jason
			// agent
			JadeRuntimeServices runtime = (JadeRuntimeServices) ts
					.getUserAgArch().getArchInfraTier().getRuntimeServices();
			Agent infra = runtime.getJadeAgent();

			// 0. get arguments from the AgentSpeak code (type and name of the
			// new service)
			StringTerm type = (StringTerm) args[0];
			StringTerm name = (StringTerm) args[1];

			// 1. get current services
			DFAgentDescription dfd = new DFAgentDescription();
			dfd.setName(infra.getAID());

			DFAgentDescription list[] = DFService.search(infra, dfd);

			// 2. deregister
			if (list.length > 0) {
				DFService.deregister(infra);
				dfd = list[0]; // the first result
			}

			// 3. add a new services
			ServiceDescription sd = new ServiceDescription();
			sd.setType(type.getString());
			sd.setName(name.getString());
			dfd.addServices(sd);

			// 4. register again
			DFService.register(infra, dfd);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
