#include "aliens.h"
#include <cmath>

Aliens::Aliens(Game_Data *gd, QObject *parent) :
		QObject(parent)
{
	gameData = gd;
	nextId = 1;
}


void Aliens::AddAlien(int srcId)
{
	if (srcId < 0 || srcId >= gameData->srcAliens.size())
	{
		qCritical("ERROR out of aliens range");
		return;
	}
	AlienType al;// = gameData->srcAliens.at(srcId);
	al.Id = nextId;
	al.src = srcId;
	al.finish = gameData->map->Finishs().at(qrand()%gameData->map->Finishs().size());
	al.pos = QPointF(gameData->map->Starts().at(qrand()%gameData->map->Starts().size()));
	al.path = gameData->map->CreatePath(al.pos.toPoint(),al.finish);
	al.pathIndex = 1;
	if (al.path.isEmpty()) qFatal("ERROR create path");
	qDebug() << al.path;
	al.health = gameData->srcAliens.at(srcId).health;
	al.speed = gameData->srcAliens.at(srcId).speed;
	al.imgType = gameData->srcAliens.at(srcId).imgType;
	//gameData->map->printMap();
	gameData->curAliens.insert(al.Id,al);
	nextId++;
}


void Aliens::retrace(bool * OK)
{
	qDebug("re-trace!");
	gameData->map->removeAliensPath();
	for (int i=0; i<gameData->curAliens.size(); i++)
	{
		gameData->curAliens.values()[i].path = gameData->map->CreatePath(gameData->curAliens.values()[i].pos.toPoint(),gameData->curAliens.values()[i].finish);
		gameData->curAliens.values()[i].pathIndex = 1;
		if (gameData->curAliens.values()[i].path.isEmpty()) *OK = false;
	}
}


void Aliens::DelAlien(int Id)
{
	gameData->curAliens.remove(Id);
}


void Aliens::update()
{
	QList <int> missIndex;
	for (int i=0; i<gameData->curAliens.size(); i++)
	{
		AlienType al = gameData->curAliens.values().at(i);
		//qDebug() << i;
		//if (al.health < 0) emit AlienKill();
		float tmpdx,tmpdy,angl,arctg = 0;
		tmpdx = al.pos.x() - al.path.at(al.pathIndex).x();
		tmpdy = al.pos.y() - al.path.at(al.pathIndex).y();
		while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*al.speed)
		{
			al.pathIndex++;
			if (al.pathIndex >= al.path.size())
			{
				qDebug("Missing alien!");
				missIndex.push_back(al.Id);
				break;
			}
			/*{
				PathIndex = 0;
				position = game->start*game->cellsize;
			}*/
			tmpdx = al.pos.x() - al.path.at(al.pathIndex).x();
			tmpdy = al.pos.y() - al.path.at(al.pathIndex).y();
			//qDebug() << "next";
		}
		arctg = std::atan(tmpdx/tmpdy);
		if (tmpdy < 0) arctg=arctg+M_PI;
		angl = 180.0f*(-arctg)/M_PI;
		/*if (PathIndex > 1)
		{
			if ((Position.angle-angl < -5 || Position.angle-angl > 5) && angl < 175 && angl > -175)
			{
				if (angl > Position.angle) Position.angle += 5;
				else Position.angle -= 5;
			}
			else Position.angle = angl;
		}
		else*/
		al.angle = angl;
		//qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt;
		al.pos.setX(al.pos.x()
					  -al.speed*std::sin(arctg));
		al.pos.setY(al.pos.y()
					  -al.speed*std::cos(arctg));
		//return true;
		gameData->curAliens.insert(al.Id,al);
		qDebug() <<"alien"<< i << "  path index" << al.pathIndex << "  pos" << al.pos;
	}
	for (int j=0; j<missIndex.size(); j++)
	{
		gameData->missingAliens++;
		DelAlien(missIndex.at(j));
	}
}
