#include <Ogre.h>

using namespace Ogre;
#include "OgreDefaultHardwareBufferManager.h"

DefaultHardwareBufferManager* bufferManager = 0;

std::ofstream outfile;
int index = 0;

class MyReGroupListener:public ResourceGroupListener
{
public:
void resourceGroupScriptingStarted(const String& groupName, size_t scriptCount){}
void scriptParseStarted(const String& scriptName, bool& skipThisScript)
{
// std::cout<<scriptName<<std::endl;
// if (Ogre::StringUtil::endsWith(scriptName, ".cg"))
// {
// skipThisScript = true;
// }
}
void scriptParseEnded(const String& scriptName, bool skipped){}
void resourceGroupScriptingEnded(const String& groupName){}
void resourceGroupLoadStarted(const String& groupName, size_t resourceCount){}
void resourceLoadStarted(const ResourcePtr& resource){}
void resourceLoadEnded(void){}
void worldGeometryStageStarted(const String& description){}
void worldGeometryStageEnded(void){}
void resourceGroupLoadEnded(const String& groupName){}
};


class MyResourceListener:public ResourceLoadingListener
{
public:


DataStreamPtr resourceLoading(const String &name, const String &group, Resource *resource){return DataStreamPtr();}
void resourceStreamOpened(const String &name, const String &group, Resource *resource, DataStreamPtr& dataStream)
{
m_scriptName = name;
}
bool resourceCollision(Resource *resource, ResourceManager *resourceManager)
{
index++;
char num[20];
itoa(index, num, 10);
std::string msg;
//msg =std::string("[") + std::string(num)+ "]script: ["+m_scriptName + "] [" + resource->getName() + "]" + "\n";

if (!HasPrintOrigin(resource->getName()))
{
MaterialPtr m = MaterialManager::getSingleton().getByName(resource->getName());

msg = m->getOrigin() + "|" + resource->getName() + "\n";
outfile<<msg;
m_hasPrint.push_back(resource->getName());
}


msg =m_scriptName + "|" + resource->getName() + "\n";

outfile<<msg;
std::cout<<"script:"<<m_scriptName<<" "<<resource->getName()<<" Collision"<<std::endl;
return false;
}

bool HasPrintOrigin(String name)
{
std::vector<String>::iterator itr;

for (itr = m_hasPrint.begin(); itr != m_hasPrint.end(); itr++)
{
if ((*itr).compare(name) == 0)
return true;
}

return false;
}

String m_scriptName;
std::vector<String> m_hasPrint;

};

void SetupResource(String path)
{
ConfigFile cf;

cf.load(path);

// Go through all sections & settings in the file
ConfigFile::SectionIterator seci = cf.getSectionIterator();

String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap* settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;

for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;

ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName, true);

}
}
}

void Print_Material()
{
ResourceManager::ResourceMapIterator itr = MaterialManager::getSingleton().getResourceIterator();

String matInfo = "";
while (itr.hasMoreElements())
{
MaterialPtr m = itr.getNext();

String matName = m->getName();
matInfo=matName;

//std::cout<<matName<<std::endl;

for (int i = 0; i < m->getNumTechniques(); i++)
{
Technique* tech = m->getTechnique(i);

for (int j = 0; j < tech->getNumPasses(); j++)
{
Pass* pass = tech->getPass(j);

for (int k = 0; k < pass->getNumTextureUnitStates(); k++)
{
TextureUnitState* texU = pass->getTextureUnitState(k);
matInfo +=" "+texU->getTextureName();
}
}
}

std::cout<<matInfo<<std::endl;
}
}

int main()
{
outfile.open("collisions.txt");
if (outfile.fail())
{
std::cout<<"can't open txt"<<std::endl;
return 1;
}

Root* pOgreRoot = new Root();
if (!pOgreRoot->restoreConfig())
pOgreRoot->showConfigDialog();





//bufferManager = new DefaultHardwareBufferManager();
pOgreRoot->initialise(true);

SetupResource("detectResource.cfg");
MyResourceListener* myListener = new MyResourceListener();
MyReGroupListener* myGroupLis = new MyReGroupListener();
ResourceGroupManager::getSingleton().setLoadingListener(myListener);
ResourceGroupManager::getSingleton().addResourceGroupListener(myGroupLis);

ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

//Print_Material();

// FileInfoListPtr fileRes = ResourceGroupManager::getSingleton().findResourceFileInfo(
// ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "*.mesh", false);
//
// FileInfoList::iterator fileItr;
//
// MeshPtr pMesh;
//
// for (fileItr = fileRes->begin(); fileItr != fileRes->end(); fileItr++)
// {
// String mesh_path = fileItr->archive->getName()+"/"+fileItr->filename;
// std::cout<<mesh_path<<std::endl;
//
// pMesh = MeshManager::getSingleton().load(fileItr->filename, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
//
// for (int i = 0; i < pMesh->getNumSubMeshes(); i++)
// {
// SubMesh* pSubMesh = pMesh->getSubMesh(i);
//
// String name = pSubMesh->getMaterialName();
//
// std::cout<<name<<std::endl;
// }
// }

outfile.close();

int c;
std::cin>>c;

return 0;

}