//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//

#include "ExtendedBaseWorldUtility.h"
#include "ConnectionManager.h"
#include "FindModule.h"

Define_Module(ExtendedBaseWorldUtility);

void ExtendedBaseWorldUtility::initialize(int stage)
{
	BaseWorldUtility::initialize(stage);

	if(stage == 0)
	{
//		int numNodes = par("numNodes");
//		//nodePosition.reserve(numNodes);
//		nodePosition.reserve(numNodes);

		//--------------------------------------------

		checkTime = par("checkTime");
		aliveNodes = numNodes = par("numNodes");
		checkNodes = new cMessage("checkAliveNodes", 0);
		scheduleAt(simTime() + checkTime, checkNodes);

		aliveNodesVector.setName("Alive nodes");
		relativeAliveNodesVector.setName("Percentage of alive nodes");

	}

//	if(stage == 1) {
//		range = FindModule<ConnectionManager*>::findSubModule(simulation.getSystemModule())->ca;
//		WATCH(range);
//	}

}

void ExtendedBaseWorldUtility::handleMessage(cMessage *msg) {
	if (msg->isSelfMessage()) {
		//record absolute and relative alive nodes
		aliveNodesVector.record(aliveNodes);
		relativeAliveNodesVector.record(aliveNodes/(double)numNodes);

		if(aliveNodes == 0)
			cancelAndDelete(checkNodes);

		else
			scheduleAt(simTime() + checkTime, checkNodes);
	}
}

void ExtendedBaseWorldUtility::decreaseAliveNodes()
{
	Enter_Method_Silent();
	aliveNodes--;
}


//void ExtendedBaseWorldUtility::setBsPosition(Coord bsCoord)
//{
//	bsPosition = bsCoord;
//	EV << "By world utility: BS position set to: " << bsPosition.info() << endl;
//}
//
//Coord ExtendedBaseWorldUtility::getBsPosition()
//{
//	return bsPosition;
//}
//
//void ExtendedBaseWorldUtility::setNodePosition(int index, Coord nodeCoord)
//{
//	nodePosition[index] = nodeCoord;
//	EV << "By world utility: node["<< index <<"] position set to: " << nodeCoord.info() << endl;
//}
//
//Coord ExtendedBaseWorldUtility::getNodePosition(int index)
//{
//	return nodePosition[index];
//}





