/* -----------------------------------------------------------------------------
*
*  npassimp.c
*
*  ANTz - realtime 3D data visualization tools for the real-world, based on NPE.
*
*  ANTz is hosted at http://openantz.com and NPE at http://neuralphysics.org
*
*  Written in 2010-2016 by Shane Saxon - saxon@openantz.com
*
*  Please see main.c for a complete list of additional code contributors.
*
*  To the extent possible under law, the author(s) have dedicated all copyright 
*  and related and neighboring rights to this software to the public domain
*  worldwide. This software is distributed without any warranty.
*
*  Released under the CC0 license, which is GPL compatible.
*
*  You should have received a copy of the CC0 Public Domain Dedication along
*  with this software (license file named LICENSE.txt). If not, see
*  http://creativecommons.org/publicdomain/zero/1.0/
*
* --------------------------------------------------------------------------- */
#include "npassimp.h"

#include "../gl/npgeometry.h"
#include "../gl/nptexmap.h"
#include "../npfile.h"

// ----------------------------------------------------------------------------
/** Initialize Assimp (Model Library).
	@param dataRef is a global map reference instance.
*/
void npAssimpInit(void* dataRef)
{
	pData data = (pData) dataRef;
	pNPassimp assimp = data->io.assimp;
	int index = 0;
	
	assimp = (pNPassimp)malloc(sizeof(NPassimp));
	if(assimp == NULL)
	{
		printf("\nmalloc failed (npAssimpInit)");
		assimp->running = false;
		return;
	}

	data->io.assimp = assimp;
	assimp->numOfScenes = 0;

	for(index = 0; index <= 49; index++)
	{
		assimp->scene[index] = NULL;
	}

	assimp->running = true;
	return;
}

/** Gets the texture path for the first model in the scene
	@param sceneIndex references an assimp scene
	@param dataRef is a global map reference instance.
	@returns An aiString containing the texture path	
*/
struct aiString npAssimpGetTexturePath(int sceneIndex, void* dataRef)
{
	pData data = (pData) dataRef;
	pNPassimp assimp = (pNPassimp)data->io.assimp;
	struct aiScene* scene = NULL;
	struct aiMaterial* material = NULL;
    static const int types[] = {
          aiTextureType_NONE,
          aiTextureType_DIFFUSE,
          aiTextureType_SPECULAR,
          aiTextureType_AMBIENT,
          aiTextureType_EMISSIVE,
          aiTextureType_HEIGHT,
          aiTextureType_NORMALS,
          aiTextureType_SHININESS,
          aiTextureType_OPACITY,
          aiTextureType_DISPLACEMENT,
          aiTextureType_LIGHTMAP,
          aiTextureType_REFLECTION,
          aiTextureType_UNKNOWN
    };
	unsigned int type = 0;
	unsigned int idx = 0;
	unsigned int total = 0;

	assimp->path.data[0] = '\0';

//	if(sceneIndex < 1 || sceneIndex > 
	/*
	if(sceneIndex < 1 || sceneIndex > assimp->numOfScenes)
	{
		printf("\nInvalid Scene Index");
		return assimp->path;
	}
	*/
	if( assimp->scene[sceneIndex] == NULL)
	{
		printf("\nScene not loaded");
		return assimp->path;
	}

	scene = assimp->scene[sceneIndex]; /// assimp->scene[0] is dummy value (NULL) 
	material = scene->mMaterials[0];
	
	for(type = 0; type < sizeof(types)/sizeof(types[0]); ++type)
	{
	//	printf("\ntype : %d", type);
		for(idx = 0; AI_SUCCESS==aiGetMaterialString(scene->mMaterials[0],
			AI_MATKEY_TEXTURE(types[type],idx),&assimp->path); idx++)
		{
				printf("%s\n    \'%s\'",(total++?"":"\nTexture Refs:" ),assimp->path.data);
			total++;
		}
	}

	return assimp->path;
}



