using System;
using Gtk;
using System.Timers;
using System.Threading;
using System.Collections.Generic;


public partial class MainWindow: Gtk.Window
{	
	
	#region Private Members
	
	private const uint worldWidth = 15;
	private const uint worldHeight = 15;
	
	private const uint numElements = 9;
	private const int timerInterval = 1000;
	
	private Gdk.Pixbuf[] iconList;	
	
	enum Elements {Empty, Dirt, Fire, Wall, Window, Door, Family, Thief, Robot };
		
	private Gtk.Image[,] worldImagesMatrix;	
	private int[,] worldMatrix;
	private int[,] pastWorldMatrix;
	private int[] sensorInfo;
	
	private System.Drawing.Point agentPosi;
	private System.Drawing.Point windowPosi;
	private System.Drawing.Point doorPosi;
	private System.Drawing.Point personPosi;
	
	private int numTicksDirt;
	private int numTicksFire;
	private int numTotalTicks;	
	private int numTicksFamily;
	private int numTickRand;
	private bool isFamily;
	private bool isThief;
	private bool isAgent;
	private bool isAlarmOn;
	private bool ranAux;
		
	private Random r;
	private System.Timers.Timer timer;
	
	// Agente
	private FixedRulesGUI.Agente Andy;
	
	#endregion
		
	#region Private Methods
	
	/// <summary>
	/// Starts the world.
	/// </summary>
	private void StartWorld()
	{
		Gdk.Color c = new Gdk.Color(73,73,73);
		
		eventbox1.ModifyBg(StateType.Normal, c);
		eventbox1.ModifyFg(StateType.Normal, c);
		
		iconList = new Gdk.Pixbuf[numElements];		
			
		// Initialize all the private members
		iconList[(int)Elements.Empty] = new Gdk.Pixbuf("Images/empty.jpg", 30, 30);
		iconList[(int)Elements.Dirt] = new Gdk.Pixbuf("Images/dirt.jpg", 30, 30);
		iconList[(int)Elements.Fire] = new Gdk.Pixbuf("Images/fire.jpg", 30, 30);
		iconList[(int)Elements.Wall] = new Gdk.Pixbuf("Images/wall.jpg", 30, 30);
		iconList[(int)Elements.Window] = new Gdk.Pixbuf("Images/window.jpg", 30, 30);
		iconList[(int)Elements.Door] = new Gdk.Pixbuf("Images/door.jpg", 30, 30);
		iconList[(int)Elements.Family] = new Gdk.Pixbuf("Images/family.jpg", 30, 30);
		iconList[(int)Elements.Thief] = new Gdk.Pixbuf("Images/thief.jpg", 30, 30);
		iconList[(int)Elements.Robot] = new Gdk.Pixbuf("Images/robot.jpg", 30, 30);
		
		worldImagesMatrix = new Gtk.Image[worldWidth,worldHeight];
		worldMatrix = new int[worldWidth,worldHeight];
		pastWorldMatrix = new int[worldWidth,worldHeight];
		sensorInfo = new int[8]{0,0,0,0,0,0,0,0};
		
		agentPosi = new System.Drawing.Point(-1,-1);
		windowPosi= new System.Drawing.Point(-1,-1);
		doorPosi= new System.Drawing.Point(-1,-1);
		personPosi= new System.Drawing.Point(-1,-1);
		Andy = new FixedRulesGUI.Agente();
		numTicksDirt = 0;
		numTicksFire = 0;
		numTotalTicks = 0;
		numTicksFamily = 0;	
		numTickRand = 0;
		isFamily = false;
		isThief = false;
		isAgent = false;
		isAlarmOn = false;
		ranAux = false;
		
		imgAlarm.Hide();
		lblAlarm.Hide();		
				
		for(uint i = 0; i < worldWidth; i++)
		{
			for(uint j = 0; j < worldHeight; j++)
			{
				worldMatrix[i,j] = 0;
				pastWorldMatrix[i,j] = 0;
				worldImagesMatrix[i,j] = new Gtk.Image();
				worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Empty];
				table1.Attach(worldImagesMatrix[i,j],i,(i+1),j,(j+1));	
				worldImagesMatrix[i,j].Show();
			}			
		}
			
		timer = new System.Timers.Timer(timerInterval);
		timer.Elapsed += new ElapsedEventHandler(onTimerTick);
		timer.Stop();
		
		r = new Random(DateTime.Now.Millisecond);	
		
		ResetWorld();
	}
	
	/// <summary>
	/// Fills the world.
	/// </summary>
	private void ResetWorld()
	{
		
		// All the worldMatrix values to 0
		numTicksDirt = 0;
		numTotalTicks = 0;
		numTicksFamily = 0;		
		isFamily = false;
		isThief = false;
				
		for(int i = 0; i < worldWidth; i++)
		{
			for(int j = 0; j < worldHeight; j++)
			{
				worldMatrix[i,j] = 0;
			}			
		}
		
		// Draw the exterior Walls
		for(int j = 0; j < 15; j++)
		{
				worldMatrix[0,j] = 3;
				worldMatrix[14,j] = 3;
				worldMatrix[j,0] = 3;
				worldMatrix[j,14] = 3;
		}
		
		// Draw the Door (down and left)
		int val = r.Next(0,15);		
		
		if(r.Next(0,2) == 0) // down or left?
			doorPosi = new System.Drawing.Point(0,val);
		else
			doorPosi = new System.Drawing.Point(val,14);
		
		worldMatrix[doorPosi.X, doorPosi.Y] = (int)Elements.Door;
		
		// Draw the Widow (up and right)
		val = r.Next(1,14);
		
		if((int)r.Next(0,2) == 0) // up and right?
			windowPosi = new System.Drawing.Point(14,val);
		else		
			windowPosi = new System.Drawing.Point(val,0);
		
		worldMatrix[windowPosi.X, windowPosi.Y] = (int)Elements.Window;
		
		// Draw the interiors walls
		worldMatrix[9,4] = (int)Elements.Wall;
		worldMatrix[9,5] = (int)Elements.Wall;
		worldMatrix[9,6] = (int)Elements.Wall;
		worldMatrix[9,7] = (int)Elements.Wall;
		worldMatrix[9,8] = (int)Elements.Wall;
		worldMatrix[9,9] = (int)Elements.Wall;
		
		worldMatrix[4,5] = (int)Elements.Wall;
		worldMatrix[5,5] = (int)Elements.Wall;
		
		// Draw 3 Fires and 3 dirt aleatori
		int val1 = 0;
		int val2 = 0;
		
		for (int i = 0; i < 3; i++)
		{
			do {
				val1 = r.Next(1,14);
				val2 = r.Next(1,14);
			} while (worldMatrix[val1, val2] != (int)Elements.Empty);
			
			worldMatrix[val1, val2] = (int)Elements.Fire;
			
			do {
				val1 = r.Next(1,14);
				val2 = r.Next(1,14);
			} while (worldMatrix[val1, val2] != (int)Elements.Empty);
			
			worldMatrix[val1, val2] = (int)Elements.Dirt;			
		}		
		
		// Draw Agent
		val1 = 0;
		val2 = 0;
		
		do {
			val1 = r.Next(1,14);
			val2 = r.Next(1,14);
		} while (worldMatrix[val1, val2] != (int)Elements.Empty);
		
		agentPosi.X = val1;
		agentPosi.Y = val2;
		
		worldMatrix[agentPosi.X, agentPosi.Y] = (int)Elements.Robot;		
		isAgent = true;
		
		DrawWorld();
	}
	
	/// <summary>
	/// Draws the world.
	/// </summary>
	private void DrawWorld()
	{
		Gtk.Application.Invoke(delegate {	
			
			for(int i = 0; i < worldWidth; i++)
			{
				for(int j = 0; j < worldHeight; j++)
				{
					
					if(pastWorldMatrix[i,j] != worldMatrix[i,j])
					{
						switch (worldMatrix[i,j]) 
						{
						
						case 0:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Empty];
							break;
							
						case 1:	
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Dirt];
												
							break;
							
						case 2:					
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Fire];
							break;
							
						case 3:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Wall];
							break;
							
						case 4:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Window];
							break;
							
						case 5:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Door];
							break;
							
						case 6:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Family];
							break;
							
						case 7:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Thief];
							break;
							
						case 8:
							worldImagesMatrix[i,j].Pixbuf = iconList[(int)Elements.Robot];
							break;
							
						default:
							break;
						}
					}
				}
			}
			
			for(int i = 0; i < worldWidth; i++)
			{
				for(int j = 0; j < worldHeight; j++)
				{
					pastWorldMatrix[i,j] = worldMatrix[i,j];
				}
			}
		});
	}
	
	/// <summary>
	/// Deletes a person.
	/// </summary>
	private void DeletePerson()
	{		
		worldMatrix[personPosi.X, personPosi.Y] = (int)Elements.Empty;
		isFamily = false;
		isThief = false;
	}
	
	/// <summary>
	/// Inserts a person.
	/// </summary>
	private void InsertPerson()
	{
		int val1 = r.Next(0,2);
		//int val1 = 0;		
		
		if(val1 == 0) // Familiar
		{
			isFamily = true;
			isThief = false;
			List<System.Drawing.Point> destList = PossibleDest(doorPosi);
			
			if(destList.Count > 0)
			{
				val1 = r.Next(0,destList.Count);
				personPosi = destList[val1];
				worldMatrix[personPosi.X, personPosi.Y] = (int)Elements.Family;
			}
			
		}
		else // Thief
		{
			isFamily = false;
			isThief = true;
			List<System.Drawing.Point> destList = PossibleDest(windowPosi);
			
			if(destList.Count > 0)
			{
				val1 = r.Next(0,destList.Count);
				personPosi = destList[val1];
				worldMatrix[personPosi.X, personPosi.Y] = (int)Elements.Thief;
			}
			
		}
	}
	
	/// <summary>
	/// Possible destination.
	/// </summary>
	/// <returns>
	/// The possibles destination.
	/// </returns>
	/// <param name='actualPoss'>
	/// Actual possition.
	/// </param>
	private List<System.Drawing.Point> PossibleDest(System.Drawing.Point actualPoss)
	{
		List <System.Drawing.Point> pointsList = new List<System.Drawing.Point>();
		int aPosX = actualPoss.X;
		int aPosY = actualPoss.Y;
		
		int nPosX = 0;
		int nPosY = 0;
		
		for(int i = -1; i< 2; i++)
		{
			for(int j = -1; j< 2; j++)
			{
				nPosX = aPosX + j;
			    nPosY =	aPosY + i;
				
				if(nPosX >= 0 && nPosY >= 0 && nPosX < worldWidth  && nPosY < worldHeight)
				{
					if(worldMatrix[nPosX,nPosY] == (int)Elements.Empty)
						pointsList.Add(new System.Drawing.Point(nPosX,nPosY));
				}
			}
		}
		
		return pointsList;
	}
	
	/// <summary>
	/// Moves the element.
	/// </summary>
	/// <param name='origin'>
	/// Point with the origin of the element.
	/// </param>
	/// <param name='dest'>
	/// Destination Point
	/// </param>	
	private void MoveElement(System.Drawing.Point origin, System.Drawing.Point dest)
	{
		if(dest.X > 0 && dest.X < (worldWidth-1) && dest.Y > 0 && dest.Y < (worldHeight-1))
		{
			if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
			{
				int elem = worldMatrix[origin.X, origin.Y];
				worldMatrix[origin.X, origin.Y] = (int)Elements.Empty;
				worldMatrix[dest.X, dest.Y] = elem;
			}
		}
	}
	
	/// <summary>
	/// Gets the sensor information for the agent.
	/// </summary>
	private void GetSensorInformation()
	{
		sensorInfo[0] = worldMatrix[agentPosi.X-1, agentPosi.Y-1];
		sensorInfo[1] = worldMatrix[agentPosi.X, agentPosi.Y-1];
		sensorInfo[2] = worldMatrix[agentPosi.X+1, agentPosi.Y-1];
		sensorInfo[3] = worldMatrix[agentPosi.X+1, agentPosi.Y];
		sensorInfo[4] = worldMatrix[agentPosi.X+1, agentPosi.Y+1];
		sensorInfo[5] = worldMatrix[agentPosi.X, agentPosi.Y+1];
		sensorInfo[6] = worldMatrix[agentPosi.X-1, agentPosi.Y+1];
		sensorInfo[7] = worldMatrix[agentPosi.X-1, agentPosi.Y];		
	}
	
	/// <summary>
	/// Gets the absolute position.
	/// </summary>
	/// <returns>
	/// The absolute position.
	/// </returns>
	/// <param name='relative_position'>
	/// The absolute position. [0-9]
	/// </param>
	/// <param name='current_point'>
	/// Current_point.
	/// </param>
	private System.Drawing.Point GetAbsolutePosition(int relative_position, System.Drawing.Point current_point)
	{
		System.Drawing.Point absPoint = new System.Drawing.Point(0,0);
		
		switch (relative_position) {
		case 0:
			absPoint.X = current_point.X -1;
			absPoint.Y = current_point.Y -1;
			break;
		case 1:
			absPoint.X = current_point.X;
			absPoint.Y = current_point.Y -1;
			break;
		case 2:
			absPoint.X = current_point.X +1;
			absPoint.Y = current_point.Y -1;
			break;
		case 3:
			absPoint.X = current_point.X +1;
			absPoint.Y = current_point.Y;
			break;
		case 4:
			absPoint.X = current_point.X +1;
			absPoint.Y = current_point.Y +1;
			break;
		case 5:
			absPoint.X = current_point.X;
			absPoint.Y = current_point.Y+1;
			break;
		case 6:
			absPoint.X = current_point.X -1;
			absPoint.Y = current_point.Y +1;
			break;
		case 7:
			absPoint.X = current_point.X -1;
			absPoint.Y = current_point.Y;
			break;					
		default:
			break;
		}
		
		return absPoint;
	}
	
	/// <summary>
	/// Executes the action choosen for the agent.
	/// </summary>
	/// <param name='action'>
	/// Action.
	/// </param>
	private void ExecuteAction(string action)
	{
		int posi = 0;
		int val1 = 0;
		System.Drawing.Point dest;
		
		switch (action) {
			
		case "moveTo":
			
			if(Andy.possibleDest.Count > 0)
			{
				
				if(Andy.possibleDest.Contains(1) && 
				   Andy.possibleDest.Contains(3) &&
				   Andy.possibleDest.Contains(5) &&
				   Andy.possibleDest.Contains(7) )
				{
					numTickRand++;
					
					if(numTickRand > 10)
					{
						numTickRand = 0;
						ranAux = !ranAux;
						Console.WriteLine("Shift random algorithm");
					}
					
					val1 = r.Next(0, 10);
					
					if(val1 < 7)
					{
						dest = new System.Drawing.Point(0,0);
						
						if(ranAux)
						{
							dest = GetAbsolutePosition(1, agentPosi);
							Andy.pastAgentPos = 5;			
							Console.WriteLine("Move To 1");
						}
						else
						{
							dest = GetAbsolutePosition(5, agentPosi);
							Andy.pastAgentPos = 1;	
							Console.WriteLine("Move To 5");
						}
						
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						break;
					}
					if(val1 < 8)
					{
						dest = new System.Drawing.Point(0,0);
						
						if(ranAux)
						{
							dest = GetAbsolutePosition(5, agentPosi);
							Andy.pastAgentPos = 1;		
							Console.WriteLine("Move To 5");
						}
						else
						{
							dest = GetAbsolutePosition(1, agentPosi);
							Andy.pastAgentPos = 5;
							Console.WriteLine("Move To 1");
						}
						
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						break;
					}
					if(val1 == 8)
					{
						dest = new System.Drawing.Point(0,0);
						
						if(ranAux)
						{
							dest = GetAbsolutePosition(3, agentPosi);
							Andy.pastAgentPos = 7;	
							Console.WriteLine("Move To 3");
						}
						else
						{
							dest = GetAbsolutePosition(7, agentPosi);
							Andy.pastAgentPos = 3;	
							Console.WriteLine("Move To 7");
						}
												
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						break;
					}
					if(val1 == 9)
					{
						dest = new System.Drawing.Point(0,0);
						
						if(ranAux)
						{
							dest = GetAbsolutePosition(7, agentPosi);
							Andy.pastAgentPos = 3;	
							Console.WriteLine("Move To 7");
						}
						else
						{
							dest = GetAbsolutePosition(3, agentPosi);
							Andy.pastAgentPos = 7;	
							Console.WriteLine("Move To 3");
						}
						
						
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						break;
					}
				}
				else
				{
					val1 = r.Next(0, Andy.possibleDest.Count);
					posi = Andy.possibleDest[val1];
					dest = new System.Drawing.Point(0,0);
				
					dest = GetAbsolutePosition(posi, agentPosi);
					
					Console.WriteLine("Move To " + posi.ToString());
					
					if(posi < 4)
						Andy.pastAgentPos = (posi+4);
					else
						Andy.pastAgentPos = (posi-4);
				
					MoveElement(agentPosi,dest);
					agentPosi = dest;
				}
			}
			break;
			
		case "clean":
				posi = Andy.desPoss;
				dest = GetAbsolutePosition(posi, agentPosi);
			
				// Delete Dirt
				worldMatrix[dest.X, dest.Y] = (int)Elements.Empty;
				// Move Andy
				MoveElement(agentPosi,dest);
				Andy.pastAgentPos = posi;
				agentPosi = dest;
			
				Console.WriteLine("Clean at "+ posi.ToString());
			
			break;
			
		case "pourWater":
				posi = Andy.desPoss;
				dest = GetAbsolutePosition(posi, agentPosi);
			
				// Pour Water 
				worldMatrix[dest.X, dest.Y] = (int)Elements.Empty;
				// Move Andy
				MoveElement(agentPosi,dest);
				Andy.pastAgentPos = posi;
				agentPosi = dest;
				Console.WriteLine("Pour Water in "+posi.ToString());
			break;
			
		case "goAway":
			posi = Andy.desPoss;
			
			Console.WriteLine("Family was detected");
			
			if(isAlarmOn)
			{
				Console.WriteLine("Alarme: Turn Off");
				isAlarmOn = false;	
			}			
			
			if((posi == 0) || (posi == 1) || (posi == 2))
			{
				dest = GetAbsolutePosition(5, agentPosi);
				if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
				{	// Move Andy
					MoveElement(agentPosi,dest);
					agentPosi = dest;
					Andy.pastAgentPos = 5;					
				}
				else 
				{
					dest = GetAbsolutePosition(6, agentPosi);
					if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
					{	
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						Andy.pastAgentPos = 6;
					}
					else
					{
						dest = GetAbsolutePosition(4, agentPosi);
						if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
						{	
							MoveElement(agentPosi,dest);
							agentPosi = dest;
							Andy.pastAgentPos = 4;							
						}
					}
					
				}
			}
			
			if((posi == 3) || (posi == 4))
			{
				dest = GetAbsolutePosition(7, agentPosi);
				if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
				{	// Move Andy
					MoveElement(agentPosi,dest);
					agentPosi = dest;
					Andy.pastAgentPos = 7;					
				}
				else 
				{
					dest = GetAbsolutePosition(6, agentPosi);
					if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
					{	
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						Andy.pastAgentPos = 6;
					}
					else
					{
						dest = GetAbsolutePosition(0, agentPosi);
						if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
						{	
							MoveElement(agentPosi,dest);
							agentPosi = dest;
							Andy.pastAgentPos = 0;
						}
					}
					
				}
			}
			
			if((posi == 5) || (posi == 6))
			{
				dest = GetAbsolutePosition(1, agentPosi);
				if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
				{	// Move Andy
					MoveElement(agentPosi,dest);
					agentPosi = dest;
					Andy.pastAgentPos = 1;
				}
				else 
				{
					dest = GetAbsolutePosition(2, agentPosi);
					if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
					{	
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						Andy.pastAgentPos = 2;
					}
					else
					{
						dest = GetAbsolutePosition(0, agentPosi);
						if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
						{	
							MoveElement(agentPosi,dest);
							agentPosi = dest;
							Andy.pastAgentPos = 0;
						}
					}
					
				}
			}
			
			if(posi == 7)
			{
				dest = GetAbsolutePosition(3, agentPosi);
				if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
				{	// Move Andy
					MoveElement(agentPosi,dest);
					agentPosi = dest;
					Andy.pastAgentPos = 3;
				}
				else 
				{
					dest = GetAbsolutePosition(4, agentPosi);
					if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
					{	
						MoveElement(agentPosi,dest);
						agentPosi = dest;
						Andy.pastAgentPos = 4;
					}
					else
					{
						dest = GetAbsolutePosition(2, agentPosi);
						if(worldMatrix[dest.X, dest.Y] == (int)Elements.Empty)
						{	
							MoveElement(agentPosi,dest);
							agentPosi = dest;
							Andy.pastAgentPos = 2;							
						}
					}					
				}
			}			
			break;
		case "turnOnAlarm":			
			Console.WriteLine("Thief was detected,  Alarm: Turn ON");
			isAlarmOn = true;
			break;
		default:
			break;
		}
	}
	
	/// <summary>
	/// Ons the timer tick.
	/// </summary>
	/// <param name='source'>
	/// Source.
	/// </param>
	/// <param name='e'>
	/// E.
	/// </param>
	private void onTimerTick(object source, ElapsedEventArgs e) 
	{   
		numTotalTicks++;		
		numTicksDirt++;
		numTicksFire++;
		numTicksFamily++;		
		
		lblTicks.LabelProp = "Num Tick = " + numTotalTicks.ToString();
		
		if(isAlarmOn){ imgAlarm.Show(); lblAlarm.Show(); }
		else{ imgAlarm.Hide(); lblAlarm.Hide(); }
		
		// Add Dirt
		if(numTicksDirt == 2)
		{			
			int val1 = 0;
			int val2 = 0;
			
			do {
				val1 = r.Next(1,14);
				val2 = r.Next(1,14);
			} while (worldMatrix[val1, val2] != (int)Elements.Empty);
			
			worldMatrix[val1, val2] = (int)Elements.Dirt;
			numTicksDirt = 0;
		}
		
		// Add Fire
		if(numTicksFire == 4)
		{			
			int val1 = 0;
			int val2 = 0;
			
			do {
				val1 = r.Next(1,14);
				val2 = r.Next(1,14);
			} while (worldMatrix[val1, val2] != (int)Elements.Empty);
			
			worldMatrix[val1, val2] = (int)Elements.Fire;
			
			numTicksFire = 0;
		}
		
		// Move Person
		if(isFamily || isThief)
		{
			if(isFamily)
				worldMatrix[personPosi.X, personPosi.Y] = (int)Elements.Family;
			else
				worldMatrix[personPosi.X, personPosi.Y] = (int)Elements.Thief;
			
			List<System.Drawing.Point> destList = PossibleDest(personPosi);
			
			if(destList.Count > 0)
			{
				int val1 = r.Next(0, destList.Count);				
				MoveElement(personPosi,destList[val1]);
				personPosi = destList[val1];
			}
		}
		
		// Adds person
		if(numTicksFamily == 10)		
			if(!isFamily && !isThief)
				InsertPerson();
		
		if(numTicksFamily == 30)
		{
			numTicksFamily = 0;
			
			if(isFamily || isThief)
				DeletePerson();
		}
		
				
		// Start Cognitive Cicle
		// Gets the vector with the sensor information
		if(isAgent)
		{
			worldMatrix[agentPosi.X, agentPosi.Y] = (int)Elements.Robot;
			GetSensorInformation();
			string action = Andy.NewCognitiveCicle(sensorInfo);
			ExecuteAction(action);
		}
		
		// Update World
		DrawWorld();		
	}
	
	#endregion
			
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();		
		StartWorld();
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		if(isAgent)
			Andy.EndAgent();
		
		Application.Quit ();
		a.RetVal = true;
	}

	protected void OnBtnResetClicked (object sender, System.EventArgs e)
	{
		ResetWorld();		
	}

	protected void OnBtnStartClicked (object sender, System.EventArgs e)
	{
		timer.Start();
	}

	protected void OnBtnStopClicked (object sender, System.EventArgs e)
	{
		timer.Stop();
	}
}
