/*
    Copyright (C) 2004  Erik Hjortsberg

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "EmberEntity.h"
#include "EmberPhysicalEntity.h"
// #include "PersonEmberEntity.h"
#include "framework/ConsoleBackend.h"
#include "Avatar.h"
#include "GUIManager.h"
#include "model/Model.h"
#include "AvatarEmberEntity.h"
#include "EmberOgre.h"

#include <Eris/Entity.h>
#include <Eris/Avatar.h>

namespace EmberOgre {


AvatarEmberEntity::AvatarEmberEntity(const std::string& id, Eris::TypeInfo* type, Eris::View* vw, Ogre::SceneManager* sceneManager, Eris::Avatar* erisAvatar) : 
mAvatar(0), mErisAvatar(erisAvatar)
,EmberPhysicalEntity(id, type, vw, sceneManager)
{
}

AvatarEmberEntity::~AvatarEmberEntity()
{}

void AvatarEmberEntity::init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp)
{
	EmberPhysicalEntity::init(ge, fromCreateOp);
	mModel->setQueryFlags(EmberEntity::CM_AVATAR);
	

}

void AvatarEmberEntity::onMoved()
{
	//EmberPhysicalEntity::onMoved();
	
	if (getAvatar()) {
		getAvatar()->movedInWorld();
	}
	Eris::Entity::onMoved();
}

/*
void AvatarEmberEntity::handleTalk(const std::string &msg)
{
	std::string message = "<";
	message.append(getName());
	message.append("> ");
	message.append(msg);
	GUIManager::getSingleton().AppendIGChatLine.emit(message);
	std::cout << "TRACE - AVATAR SAYS: [" << message << "]\n" << std::endl;
	Ember::ConsoleBackend::getMainConsole()->pushMessage(message);
}
*/
/*
void AvatarEmberEntity::setVisible(bool vis)
{
	//TODO
	//mOgreEntity->setVisible(true);	
}
*/

//void AvatarEmberEntity::addMember(Entity *e) 
void AvatarEmberEntity::onChildAdded(Entity *e) 
{
	//mAvatar->EventAddedEntityToInventory.emit(static_cast<EmberEntity*>(e));
	EmberOgre::getSingleton().getAvatar()->mEntitiesToBeAddedToInventory.insert(e);
	//PersonEmberEntity::addMember(e);
	EmberPhysicalEntity::onChildAdded(e);
	
}


/*void AvatarEmberEntity::rmvMember(Entity *e)*/
void AvatarEmberEntity::onChildRemoved(Entity *e)
{
	EmberOgre::getSingleton().getAvatar()->EventRemovedEntityFromInventory.emit(static_cast<EmberEntity*>(e));
	EmberPhysicalEntity::onChildRemoved(e);
//	mAvatar->mEntitiesToBeRemovedFromInventory.insert(e);
//	PersonEmberEntity::rmvMember(e);	
}



// void AvatarEmberEntity::onLocationChanged(Eris::Entity *oldLocation, Eris::Entity *newLocation)
// {
// 	return EmberEntity::onLocationChanged(oldLocation, newLocation);
// 	
// 	
// 	
// 	Ogre::Vector3 oldWorldPosition = getSceneNode()->getWorldPosition();
// 	EmberEntity* EmberEntity = dynamic_cast<EmberEntity*>(newLocation);
// 	Ogre::SceneNode* newOgreParentNode = EmberEntity->getSceneNode();
// 	
// /*	if (EmberEntity)
// 	{
// 		newOgreParentNode = EmberEntity->getSceneNode();
// 	} else {
// 		newOgreParentNode = EmberOgre::getSingleton().getSceneManager()->getSceneNode(newLocation->getId());
// 	}*/
// 		
// 	if (getSceneNode()->getParent()) {
// 		//detach from our current object and add to the new entity
// 		getSceneNode()->getParent()->removeChild(getSceneNode()->getName());
// 	}
// 	newOgreParentNode->addChild(getSceneNode());
// 	
// 
// //	Entity::setContainer(pr);
// 	Eris::Entity::onLocationChanged(oldLocation, newLocation);
// 	
// 	//we adjust the entity so it retains it's former position in the world
// 	Ogre::Vector3 newWorldPosition = getSceneNode()->getWorldPosition();
// 	getSceneNode()->translate(oldWorldPosition - newWorldPosition);
// }

Ogre::SceneNode* AvatarEmberEntity::getAvatarSceneNode()
{
	return getScaleNode();	
}




}

