#include "SquareArc.h"
#include "GroundMap.h"

#include "MemoryManager/debugNew.h"


using namespace ground;

SquareArc::SquareArc(void){
}

SquareArc::SquareArc(SquareNode * first,SquareNode * second){
	this->first = first;
	this->second = second;
}

SquareArc::~SquareArc(void){
}

void SquareArc::setFirt(SquareNode * node){
	first = node;
}

void SquareArc::setSecond(SquareNode * node){
	second = node;
}

GraphNode & SquareArc::getFirtNode(){
	return *first;
}

GraphNode & SquareArc::getSecondNode(){
	return *second;
}

void SquareArc::computeFactors(){
	float firstSize = (first->getSize()+1)/2.0f;
	float secondSize = (second->getSize()+1)/2.0f;
	float deltaX = first->getPosition().getX() - second->getPosition().getX();
	float deltaY = first->getPosition().getY() - second->getPosition().getY();
	length = sqrt(deltaX*deltaX + deltaY*deltaY);
	firstFactor = (firstSize/(secondSize+firstSize))*length;
	secondFactor = (secondSize/(secondSize+firstSize))*length;
	if (firstFactor == 0.0f){
		printf("erreur : firstFactor a 0 : %f %f %f\n",firstSize,secondSize,length);
	}
	if (secondFactor == 0.0f){
		printf("erreur : secondFactor a 0 : %f %f %f\n",firstSize,secondSize,length);
	}
	if ((firstFactor/length) + (secondFactor/length) < 0.999f ||
		(firstFactor/length) + (secondFactor/length) > 1.001f){
		printf("erreure : somme des facteurs non egale a 1 : %f + %f = %f\n",(firstFactor/length),(secondFactor/length),(firstFactor/length)+(secondFactor/length));
	}
}

void SquareArc::detachExtremity(){
	if (first != NULL){
		first->removeArc(this);
	}
	if (second != NULL){
		second->removeArc(this);
	}
}


float SquareArc::getCostFrom(GraphNode& origin){
	SquareNode * from = static_cast<SquareNode*>(&origin);
	if (first != from && second != from){
		printf("l'origine passé à la methode SquareArc::getCostFrom(origin) n'est pas une des extremité de l'arc...\n");
		return 99999.0f;
	}
	float firstCost;
	float secondCost;
	switch(first->getGroundType()){
		case MEADOW:
			firstCost = 20.0f;
			break;
		case FOREST:
			firstCost = 40.0f;
			break;
		case OREE:
			firstCost = 40.0f;
			break;
		case ROAD:
			firstCost = 15.0f;
			break;
		case WATER:
			printf("terrain de type ground::WATER trouvé dans le graph...\n");
			firstCost = 9999.0f;
			break;
	}
	switch(second->getGroundType()){
		case MEADOW:
			secondCost = 20.0f;
			break;
		case FOREST:
			secondCost = 40.0f;
			break;
		case OREE:
			secondCost = 40.0f;
			break;
		case ROAD:
			secondCost = 15.0f;
			break;
		case WATER:
			printf("terrain de type ground::WATER trouvé dans le graph...\n");
			secondCost = 9999.0f;
			break;
	}
	return (firstFactor*firstCost + secondFactor * secondCost)*length;
}

GraphNode* SquareArc::getOtherSide(GraphNode* extremity){
	if (extremity == first){
		return second;
	}else if(extremity == second){
		return first;
	}else{
		return NULL;
	}
}

float SquareArc::getFirstFactor()const{
	return firstFactor;
}

float SquareArc::getSecondFactor()const{
	return secondFactor;
}


