//feeder.js

var lblBest, lblWorst, lblGeneration, lblAverage, lblBestData;
var control;
var canvasWorld;
var TheHeight = 40;
var TheWidth = 60;
var TheSize = 10;
var TheStatus = "Stopped";
var _world;
var POPULATIONSIZE = 12;
var FOODCOUNT = 250;
var ShowInfo = true;

function Loaded(sender,args)
{
	control = sender.getHost();
	lblBest = sender.findName("lblBest");
	lblWorst = sender.findName("lblWorst");
	lblAverage = sender.findName("lblAverage");
	lblGeneration = sender.findName("lblGeneration");
	lblBestData = sender.findName("lblBestData");
	
	canvasWorld = sender.findName("canvasWorld");
	_world = new World(TheWidth, TheHeight);
	_world.Clear();

	UpdateInfo();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function CheckBox_MouseLeftButtonDown(sender, args) 
{
	var buttonname = sender.Name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 0.7;
}

function CheckBox_MouseLeftButtonUp(sender, args) 
{
	var buttonname = sender.Name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 1;
	var pth = sender.findName(buttonname + "Path");
	if (pth.Visibility=="Visible")
	{
		pth.Visibility = "Collapsed";
		DoUnCheck(buttonname);
	}
	else
	{
		pth.Visibility = "Visible";
		DoCheck(buttonname);
	}
}

function CheckBox_MouseEnter(sender,args)
{
	var buttonname = sender.Name;
	sender.findName(buttonname + "Rect").Stroke = "Black";
}

function CheckBox_MouseLeave(sender,args)
{
	var buttonname = sender.Name;
	sender.findName(buttonname + "Rect").Stroke = "Gray";
	sender.findName(buttonname + "Rect").Opacity = 1;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function Button_MouseLeftButtonDown(sender, args) 
{
	var buttonname = sender.Name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 0.7;
}

function Button_MouseLeftButtonUp(sender, args) 
{
	var buttonname = sender.Name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 1;
	DoClick(buttonname);
}

function Button_MouseEnter(sender,args)
{
	var buttonname = sender.Name;
	sender.findName(buttonname + "Rect").Stroke = "Black";
}

function Button_MouseLeave(sender,args)
{
	var buttonname = sender.Name;
	sender.findName(buttonname + "Rect").Stroke = "Gray";
	sender.findName(buttonname + "Rect").Opacity = 1;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function DoCheck(buttonname)
{
	if (buttonname == "chkInfo")
	{
		ShowInfo=true;
	}
}
function DoUnCheck(buttonname)
{
	if (buttonname == "chkInfo")
	{
		ShowInfo=false;
	}
}

function DoClick(buttonname)
{
	if (buttonname == "btnReset")
	{
		RemoveFood();
		RemovePopulation();
		_world.Clear();
	}
	else if (buttonname == "btnStep")
	{
		MovePopulation1();
	}
	else if (buttonname == "btnStart")
	{
		if (TheStatus=="Stopped")
		{
			TheStatus = "Running";
			MustMove = 1000;
			MustStop = false;
			setTimeout("OnTimer()",500);
		}
	}
	else if (buttonname == "btnStop")
	{
		MustStop = true;
		TheStatus = "Stopped";
	}
	else if (buttonname == "btnPopulate")
	{
		_world.AddPopulation(POPULATIONSIZE);
		MakePopulationUI();
		TheGeneration = 1;
	}
	else if (buttonname == "btnAddFood")
	{
		_world.AddFood(FOODCOUNT);
		MakeFoodUI();
	}
	else if (buttonname == "btnEvolve")
	{
		TheGeneration++;
		Evolve();
	}
	UpdateInfo();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

var TheWorst = 0;
var TheBest = 0;
var TheAverage = 0;
var TheGeneration = 0;

var LastWorst = -1;
var LastBest = -1;
var LastAverage = -1;
var LastGeneration = -1;

function UpdateInfo()
{
	if (TheBest != LastBest)
	{
		lblBest.Text = TheBest + '';
		LastBest = TheBest;
	}
	if (TheWorst != LastWorst)
	{
		lblWorst.Text = TheWorst + '';
		LastWorst = TheWorst;
	}
	if (TheGeneration != LastGeneration)
	{
		lblGeneration.Text = TheGeneration + '';
		LastGeneration = TheGeneration;
	}
	if (TheAverage != LastAverage)
	{
		lblAverage.Text = TheAverage + '';
		LastAverage = TheAverage;
	}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function MakePopulationUI()
{
	var i,l,c;
	var colors = new Array(5);
	colors[0] = "DarkBlue";
	colors[1] = "Blue";
	colors[2] = "LightBlue";
	colors[3] = "White";
	colors[4] = "White";
	for (i=0;i<_world.PopulationSize;i++)
	{
		c = _world.Population[i];
		for (l=c.Length-1;l>=0;l--)
		{
			x=c.PosX[l];
			y=c.PosY[l];
			xmlpop = "<Rectangle Height='"+TheSize+"' Width='"+TheSize+"' Canvas.Left='"+x*TheSize+"' Canvas.Top='"+y*TheSize+"' Fill='"+colors[l]+"' Name='pop_"+i+"_"+l+"' Stroke='Transparent'/>"
			c.PartsUI[l] = control.content.createFromXaml(xmlpop);
			canvasWorld.children.add(c.PartsUI[l]);
		}
	}
}

function RemovePopulation()
{
	var i,l,c;
	for (i=0;i<_world.PopulationSize;i++)
	{
		c = _world.Population[i];
		for (l=c.Length-1;l>=0;l--)
		{
			canvasWorld.children.remove(c.PartsUI[l]);
			c.PartsUI[l]=null;
		}
	}
}

function MakeFoodUI()
{
	for (x=0; x<_world.Width; x++)
		for (y=0; y<_world.Height; y++)
		{
			if (_world.Data[y][x] == 1 && _world.DataUI[y][x] == null)
			{
				xmlfood = "<Ellipse Height='"+TheSize+"' Width='"+TheSize+"' Canvas.Left='"+x*TheSize+"' Canvas.Top='"+y*TheSize+"' Fill='Red' Name='food_"+x+"_"+y+"' />"
				_world.DataUI[y][x] = control.content.createFromXaml(xmlfood);
				canvasWorld.children.add(_world.DataUI[y][x]);
			}
		}
}

function UpdateFoodUI()
{
	var x,y;
	for (x=0; x<_world.Width; x++)
		for (y=0; y<_world.Height; y++)
		{
			if (_world.Data[y][x] == 1 && _world.DataUI[y][x] != null)
			{
				_world.DataUI[y][x]["Canvas.Top"] = y*TheSize;
				_world.DataUI[y][x]["Canvas.Left"] = x*TheSize;
			}
		}
}

function RemoveFood()
{
	for (x=0; x<_world.Width; x++)
		for (y=0; y<_world.Height; y++)
		{
			if (_world.DataUI[y][x] != null)
			{
				canvasWorld.children.remove(_world.DataUI[y][x]);
				_world.DataUI[y][x] = null;
				_world.Data[y][x] = 0;
			}
		}
}

function MovePopulation1()
{
	var foundfood=false;
	var i,l,c;
	for (i=0;i<_world.PopulationSize;i++)
	{
		c = _world.Population[i];
		c.Step1();
		if (_world.CheckIfFoundFood(c))
		{
			foundfood=true;
			c.Score++;
		}
		for (l=c.Length-1;l>=0;l--)
		{
			x=c.PosX[l];
			y=c.PosY[l];
			c.PartsUI[l]["Canvas.Top"] = y*TheSize;
			c.PartsUI[l]["Canvas.Left"] = x*TheSize;
		}		
	}
	if (foundfood)
	{
		UpdateFoodUI();
		UpdateScore();
	}
}

function UpdateScore()
{
	var b=0;
	var w=999999;
	var i,c;
	var tot = 0;
	var bestix=-1;
	for (i=0;i<_world.PopulationSize;i++)
	{
		c = _world.Population[i];
		c.PartsUI[0].Stroke=null;
		tot = tot + c.Score;
		if (c.Score>b)
		{
			b=c.Score;
			bestix=i;
		}
		if (c.Score<w)
			w=c.Score;
	}
	if (bestix!=-1)
	{
		c = _world.Population[bestix];
		c.PartsUI[0].Stroke="Yellow";
		lblBestData.Text=c.GetData();
	}
	tot = tot / _world.PopulationSize;
	tot = Math.floor(tot * 100);
	tot = tot / 100;
	TheBest = b;
	TheWorst = w;
	TheAverage = tot;
	UpdateInfo();
}

function Evolve()
{
	var i,l,c;
	_world.EvolvePopulation(ShowInfo);
	for (i=0;i<_world.PopulationSize;i++)
	{
		c = _world.Population[i];
		c.Step = 0;
		c.Score = 0;
		//c.MoveToRandomSpot();
		for (l=c.Length-1;l>=0;l--)
		{
			x=c.PosX[l];
			y=c.PosY[l];
			c.PartsUI[l]["Canvas.Top"] = y*TheSize;
			c.PartsUI[l]["Canvas.Left"] = x*TheSize;
		}		
	}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var MustStop = false;
var MustMove;
function OnTimer()
{
	if (MustStop)
	{
		TheStatus = "Stopped";
	}
	else
	{
		MustMove--;
		MovePopulation1();
		if (MustMove==0)
		{
			TheGeneration++;
			Evolve();
			MustMove=1000;
		}
		setTimeout("OnTimer()",50);
	}
	UpdateInfo();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

