#include "imodule.h"
#include "moduletype.h"
#include "../exceptions/exceptions.h"

Ptr<IModule> IModule::nullRef;

const char* IModule::type="IModule";

const char* IModule::getType() const
{
	return type;
}

void IModule::registerModule(ModuleType* parent,const string& name)
{
	if (this->parent!=0) throw new Exception("IModule","Module already registred !");
	this->parent=parent;
	this->manager=parent->getManager();
	this->name=name;
	Ptr<VarDef>* variables=parent->getVariables();
	if (variables!=0)
	{
		int i=0;
		while(!variables[i].isNull())
		{
			VarInst* varInst=variables[i]->instantiate(this);
			vars[varInst->getVarDef()->getName()]=varInst;
			i++;
		}
	}
}

void IModule::printVariables(ostream& o) const
{
	//o<<"Name Type Value Meaning"<<endl;
	o<<"Variables : "<<endl;
	const int space_size=10;
	char spaces[space_size];
	for(t_vars::const_iterator it=vars.begin();it!=vars.end();it++)
	{
		const VarDef* def=it->second->getVarDef();

		memset(spaces,'\32',space_size);
		sprintf(spaces,"(%s)",def->getITypeName());
		o<<spaces<<" ";
		o<<def->getName()<<" = ";
		if (it->second.isNull()) 
			o<<"NOT SET"<<endl;
		else
			o<<"'"<<(it->second)->toString()<<"'"<<endl;
		o<<"Description :"<<endl;
		o<<it->second->getVarDef()->getDescr()<<endl;
		o<<"----------------------------------------"<<endl;
	}
	o.flush();
}
