Team::Team(Stage* stage, const Team& team)
	: QObject(stage),
	color_(team.color()),
	goals_(team.goals()),
	yellowCards_(team.yellowCards()),
	redCards_(team.redCards())
{
	stage_ = stage;
	for(int i=0; i<team.size(); i++){
		Robot* r = new Robot( *(team.at(i)) );
		r->setTeam(this);
		r->setStage(stage);
		this->push_back(r);
	}
	
}
void Controller2::step() {
	CXBOXController* controller = new CXBOXController(id);
	qreal sx = 0.0, sy = 0.0, dx = 0.0, dy = 0.0, sBoost = 1.0, aBoostAng = 1.0;
	qint32 dribSign = 1;

	if(controller->IsConnected()){
		//rotation
		qreal t_dx = controller->ThumbRX();
		qreal t_dy = controller->ThumbRY();

		if(t_dx * t_dx + t_dy * t_dy < .05) {
			t_dx = 0;
			t_dy = 0;
		}
		if(controller->ButtonPressed(XINPUT_GAMEPAD_RIGHT_THUMB)) {
			aBoostAng = 5.0;
		} else {
			aBoostAng = 1.0;
		}
		move->setSpeedAngular(-t_dx * 5);
		//steer->setRate(0.06 * aBoostAng);

		//movement
		sx = controller->ThumbLX();
		sy = controller->ThumbLY();
		// dead zone
		if(sx * sx + sy * sy < .05) {
			sx = 0;
			sy = 0;
		}

		//direcionais
		if(controller->ButtonPressed(XINPUT_GAMEPAD_DPAD_DOWN)) {
			sx = 0;
			sy = -1;
		}
		if(controller->ButtonPressed(XINPUT_GAMEPAD_DPAD_UP)) {
			sx = 0;
			sy = 1;
		}
		if(controller->ButtonPressed(XINPUT_GAMEPAD_DPAD_LEFT)) {
			sx = -1;
			sy = 0;
		}
		if(controller->ButtonPressed(XINPUT_GAMEPAD_DPAD_RIGHT)) {
			sx = 1;
			sy = 0;
		}

		if(controller->ButtonPressed(XINPUT_GAMEPAD_LEFT_THUMB)) {
			sBoost = 10.0;
		} else {
			sBoost = 1.0;
		}
		//steer->setAll(sx * sBoost * speed, sy * sBoost * speed, dx, dy);
		qreal theta = robot()->orientation();
		qreal ssx = sx * cos(theta) - sy * sin(theta);
		qreal ssy = sx * sin(theta) + sy * cos(theta);
		//steer->setAll(ssx * sBoost * speed, ssy * sBoost * speed, dx, dy);

		move->setSpeeds(ssy * sBoost * speed, - ssx * sBoost * speed);

		//dribbler
		if(controller->ButtonPressed(XINPUT_GAMEPAD_LEFT_SHOULDER)) {
			dribSign = -1;
		} else {
			dribSign = 1;
		}
		robot()->dribble(dribSign * controller->TriggerL());

		//kicker
		if(controller->ButtonPressed(XINPUT_GAMEPAD_RIGHT_SHOULDER)) {
			robot()->kick(controller->TriggerR());
		} else {
			robot()->kick(0.0);
		}
		//steer->step();

		//blocker
		if(controller->ButtonPressed(XINPUT_GAMEPAD_A)) {
			bl->step();
		}
		
		else if(controller->ButtonPressed(XINPUT_GAMEPAD_B)) {
			Team* team = robot()->team();
			int id = robot()->id();
			id++;
			id%=team->size();
			this->setRobot(team->at(id));
		}

		//goalkeeper
		else if(controller->ButtonPressed(XINPUT_GAMEPAD_X)) {
			gk->step();
		}

		//zickler
		else if(controller->ButtonPressed(XINPUT_GAMEPAD_Y)) {
			zk->step();
		}
		else if(controller->ButtonPressed(XINPUT_GAMEPAD_START)) {
			kt->setPoint(*(robot()->enemyGoal()));
			kt->step();
		}
		else if(controller->ButtonPressed(XINPUT_GAMEPAD_BACK)) {
			at->step();
		}

		else 
			move->step();
	}
}
void AutoRetaliate::step(){
	Team* team = this->team_;
	Goal* myGoal = team->goal();

	if(player_[0]->robot()->isActive())
	{
		player_[0]->step();
	}
	/*else
	{
		map<qreal, Robot*> robotsCloseToGoal = stage()->getClosestPlayersToPoint(team, team->goal()->x(), team->goal()->y());
		map<qreal, Robot*>::iterator goalIter = robotsCloseToGoal.begin();
		for(int j=0; j<robotsCloseToGoal.size(); j++){
			Robot* r = goalIter.first;
		}
		for(int i=0; i<team->size(); i++)
		{
			if(team->at(i)->isActive())
			{
				player_[0]->setRobot(team->at(i));
				player_[0]->step();
				break;
			}
		}
	}*/

	if(myGoal->width()>0){
		Goal* goal = this->team()->goal();
		cover1->setX(goal->x());
		cover1->setY(goal->y() + goal->width()/3);
		cover2->setX(goal->x());
		cover2->setY(goal->y() - goal->width()/3);
		cover3->setX(goal->x());
		cover3->setY(goal->y());
		//usei team->enemyTeam()->at(0), mas tanto faz pq o enemy eh associada dinamicamente ao robo
		for(int i = 3; i < this->team()->size(); i++){
			Object* p;
			if(i==3)
				p = cover1;
			else if(i==4)
				p = cover2;
			else
				p = cover3;
			((Defender*)player_[i])->setCover(p);
		}

		init = true;
	}
	
	if(init){
		Stage* stage = this->stage_;
		Ball* ball = stage->ball();

		map<qreal, Robot*> close = stage->getClosestPlayersToBallThatCanKick(team);
		map<int, Robot*> ids;
		map<qreal, Robot*> enemys = stage->getClosestPlayersToPointThatCanKick(team->enemyTeam(), (Point*)team->goal());



		//Attacker e Blocker
		int i=0;
		map<qreal, Robot*>::iterator it1 = close.begin();
		for(int j=0; j<close.size(); j++){
			if((*it1).second->id() == player_[0]->robot()->id()){
				it1++;
				continue;
			}

			Robot* robot = (*it1).second;
			if(i == 0){
				player_[1]->setRobot(robot);
				player_[1]->step();
			}
			else if(i == 1){
				player_[2]->setRobot(robot);
				player_[2]->step();
			}
			else
				ids[robot->id()] = robot;

			i++;
			it1++;
		}

		//Defenders enemys
		map<qreal, Robot*>::iterator it3 = enemys.begin(); 

		//Defenders
		map<int, Robot*>::iterator it2 = ids.begin();
		for(int i=3; i<team->size() && it2!=ids.end(); i++){
			player_[i]->setRobot((*it2).second);

			if(it3!=enemys.end() && (*it3).second != NULL){
				((Defender*)player_[i])->setEnemy((*it3).second);
				it3++;
			}
			else{
				((Defender*)player_[i])->setEnemy(ball);
				((Defender*)player_[i])->reset();
			}

			player_[i]->step();

			it2++;
		}

		
	}
}