/*
 * Copyright 2014 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.sarl.core;

import io.sarl.lang.core.Agent;

import java.lang.ref.WeakReference;
import java.util.TimerTask;

import org.eclipse.xtext.xbase.lib.Functions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Procedures;

/**
 * Description of a task an agent has schedule to be performed at a later time.
 * @author $Author: srodriguez$
 * @version $FullVersion$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 */
public class AgentTask {

	private String name;
	private Functions.Function1<Agent, Boolean> guard;
	private Procedures.Procedure1<Agent> procedure;

	private TimerTask timerTask = null;
	public AgentTask() {

	}


	public Procedures.Procedure1<Agent> getProcedure() {
		return procedure;
	}


	public void setProcedure(Procedures.Procedure1<Agent> procedure) {
		this.procedure = procedure;
	}


	public Functions.Function1<Agent, Boolean> getGuard() {
		return guard;
	}

	public void setGuard(Functions.Function1<Agent, Boolean> guard) {
		this.guard = guard;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AgentTask cancel() {
		this.timerTask.cancel();
		return this;
	}

	public AgentTask unless(Functions.Function1<Agent, Boolean> predicate) {
		this.guard = new NegateFunction(predicate);
		return this;
	}

	public AgentTask ifTrue(Functions.Function1<Agent, Boolean> predicate) {
		this.guard = predicate;
		return this;
	}

	public TimerTask getTimerTask(Agent agent) {
		this.timerTask =new AgentTimerTask(this, agent);
		return this.timerTask;
	}

	private class AgentTimerTask extends TimerTask {
		private WeakReference<AgentTask> agentTaskRef;
		private WeakReference<Agent> agentRef;

		public AgentTimerTask(AgentTask task, Agent agent) {
			this.agentTaskRef = new WeakReference<AgentTask>(task);
			this.agentRef = new WeakReference<Agent>(agent);
		}

		@Override
		public void run() {
			if (agentTaskRef.get() == null) {
				System.out.println("Agent Task is null");
			} else {
				AgentTask task = agentTaskRef.get();
				if (task.guard.apply(this.agentRef.get())) {					
					task.procedure.apply(this.agentRef.get());
				} 
			}

		}

	}

	private class NegateFunction implements Function1<Agent, Boolean> {

		private Function1<Agent, Boolean> unlessPredicate;

		public NegateFunction(Function1<Agent, Boolean> predicate) {
			this.unlessPredicate = predicate;
		}

		public Boolean apply(Agent p) {
			return !this.unlessPredicate.apply(p);
		}

	}
}

@@ -1259,45 +1259,9 @@
 re;%0A
-%0A%09private TimerTask timerTask = null;
+%09
 %0A%09pu
@@ -1747,83 +1747,8 @@
 %09%7D%0A%0A
-%09public AgentTask cancel() %7B%0A%09%09this.timerTask.cancel();%0A%09%09return this;%0A%09%7D%0A%0A
 %09pub
@@ -1982,16 +1982,16 @@
 dicate;%0A
+
 %09%09return
@@ -2005,740 +2005,8 @@
 %09%7D%0A%0A
-%09public TimerTask getTimerTask(Agent agent) %7B%0A%09%09this.timerTask =new AgentTimerTask(this, agent);%0A%09%09return this.timerTask;%0A%09%7D%0A%0A%09private class AgentTimerTask extends TimerTask %7B%0A%09%09private WeakReference%3CAgentTask%3E agentTaskRef;%0A%09%09private WeakReference%3CAgent%3E agentRef;%0A%0A%09%09public AgentTimerTask(AgentTask task, Agent agent) %7B%0A%09%09%09this.agentTaskRef = new WeakReference%3CAgentTask%3E(task);%0A%09%09%09this.agentRef = new WeakReference%3CAgent%3E(agent);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void run() %7B%0A%09%09%09if (agentTaskRef.get() == null) %7B%0A%09%09%09%09System.out.println(%22Agent Task is null%22);%0A%09%09%09%7D else %7B%0A%09%09%09%09AgentTask task = agentTaskRef.get();%0A%09%09%09%09if (task.guard.apply(this.agentRef.get())) %7B%09%09%09%09%09%0A%09%09%09%09%09task.procedure.apply(this.agentRef.get());%0A%09%09%09%09%7D %0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %09pri

Remove TimerTask related code.