#include "ModelStore.h"

#include <Renderer/Model.h>
#include <Physics/Model.h>
#include <Utility/FilePath.h>
#include <Utility/AC3DFileReader.h>

namespace
{
	const std::string ROOT_DIR = "Models/";
}

ModelStore::ModelStore()
{
}

ModelStore::~ModelStore()
{
}

Renderer::ModelPtr ModelStore::GetVisibleModel( AAssetManager& Manager, const std::string& File )
{
	auto iter = m_VisibleModels.find( File );
	if (iter != m_VisibleModels.end()) {
		return iter->second;
	}
	Utility::FilePath FilePath( ROOT_DIR + File );
	Utility::AC3DFilePtr pAC3DFile = Utility::AC3DFileReader::ReadFile( Manager, FilePath );
	Renderer::ModelPtr pModel( new Renderer::Model( *pAC3DFile, false ) );
	m_VisibleModels[File] = pModel;
	return pModel;
}

Renderer::ModelPtr ModelStore::GetShadowModel( AAssetManager& Manager, const std::string& File )
{
	auto iter = m_ShadowModels.find( File );
	if (iter != m_ShadowModels.end()) {
		return iter->second;
	}
	Utility::FilePath FilePath( ROOT_DIR + File );
	Utility::AC3DFilePtr pAC3DFile = Utility::AC3DFileReader::ReadFile( Manager, FilePath );
	Renderer::ModelPtr pModel( new Renderer::Model( *pAC3DFile, true ) );
	m_ShadowModels[File] = pModel;
	return pModel;
}

Physics::ModelPtr ModelStore::GetPhysicsModel( AAssetManager& Manager,  const std::string& File )
{
	auto iter = m_PhysicsModels.find( File );
	if (iter != m_PhysicsModels.end()) {
		return iter->second;
	}
	Utility::FilePath FilePath( ROOT_DIR + File );
	Utility::AC3DFilePtr pAC3DFile = Utility::AC3DFileReader::ReadFile( Manager, FilePath );
	Physics::ModelPtr pModel( new Physics::Model( *pAC3DFile ) );
	return pModel;
}

Renderer::ModelPtr ModelStore::GetVisibleModel( const Math::CubeVector& Size )
{
	std::ostringstream str;
	str << Size;
	auto iter = m_VisibleModels.find( str.str() );
	if (iter != m_VisibleModels.end()) {
		return iter->second;
	}
	Renderer::ModelPtr pModel( new Renderer::Model( Size, true ) );
	m_VisibleModels[str.str()] = pModel;
	return pModel;
}

Renderer::ModelPtr ModelStore::GetShadowModel( const Math::CubeVector& Size )
{
	std::ostringstream str;
	str << Size;
	auto iter = m_ShadowModels.find( str.str() );
	if (iter != m_ShadowModels.end()) {
		return iter->second;
	}
	Renderer::ModelPtr pModel( new Renderer::Model( Size, true ) );
	m_ShadowModels[str.str()] = pModel;
	return pModel;
}

Physics::ModelPtr ModelStore::GetPhysicsModel( const Math::CubeVector& Size )
{
	std::ostringstream str;
	str << Size;
	auto iter = m_PhysicsModels.find( str.str() );
	if (iter != m_PhysicsModels.end()) {
		return iter->second;
	}
	Physics::ModelPtr pModel( new Physics::Model( Size ) );
	m_PhysicsModels[str.str()] = pModel;
	return pModel;
}

