#pragma once
#include "Material.h"
#include <sstream>
#include "Util.h"

#define TOUNICODE(t) L ## t

void Material::setName(string _name)
{
	name = _name;
}

string Material::getName()
{
	return name;
}

void Material::setAmbientColor(float* ambient)
{
	material.ambientColor = D3DXVECTOR4(ambient);
}

void Material::setDiffuseColor(float* diffuse)
{
	material.diffuseColor = D3DXVECTOR4(diffuse);
}

void Material::setSpecularColor(float* specular, float exp)
{
	material.specularColor = D3DXVECTOR4(specular);
	material.exponent = exp;
}

void Material::setEmissiveColor(float* emissive)
{
	material.emissiveColor = D3DXVECTOR4(emissive);
}

void Material::setHasEmissive(bool hasemissive)
{
	material.hasEmissive = hasemissive;
}

void Material::setHasSpecular(bool hasSpec)
{
	
}

void Material::setNumTextures(int numTextures)
{
	nrOfTextures = numTextures;
	if(nrOfTextures == 0)
	{
		material.hasNormalMap = false;
		material.hasTexture = false;
	}
}

void Material::setTextureFilename(int level, string textureFilename)
{
	textureLevel = level;
	
	textureFileName = textureFilename;
	textureResource= NULL;
	normalMapResource = NULL;
	
	if(nrOfTextures !=0)
	{
		size_t indexOfLastSlash = textureFilename.find_last_of("\\/");
		string textureDirectory = (indexOfLastSlash != string::npos ? textureFilename.substr(0,indexOfLastSlash+1) : string("./"));
		string imageFilename = (indexOfLastSlash != string::npos ? textureFilename.substr(indexOfLastSlash+1) : textureFilename);
		material.hasTexture = true;
		size_t indexOfLastDot = imageFilename.rfind(".");
		string imageFilenameWithoutExtension = (indexOfLastDot != string::npos ? imageFilename.substr(0,indexOfLastDot) : imageFilename);
		string imageFileExtension = (indexOfLastDot != string::npos ? imageFilename.substr(indexOfLastDot+1) : string(""));

		normalMapFileName = textureDirectory + string("normalmaps/") + imageFilenameWithoutExtension + string("_NRM.") + imageFileExtension;
		material.hasNormalMap = Util::fileExists(normalMapFileName);
	}
	
}

void Material::loadTextures()
{
	if(material.hasTexture && !textureResource)
		{
			D3DX10CreateShaderResourceViewFromFile( DXUTGetD3D10Device(), Util::stringToUnicode(textureFileName).c_str(), NULL, NULL, &textureResource, NULL );
			if(material.hasNormalMap)
			{
				HRESULT hr = D3DX10CreateShaderResourceViewFromFile( DXUTGetD3D10Device(), Util::stringToUnicode(normalMapFileName).c_str(), NULL, NULL, &normalMapResource, NULL );
				if(FAILED(hr))
				{
					std::cout<<"texture load fail"<< std::endl;
				}
			}
		}
}

void Material::sendToEffect(Effect* effect)
{
	PhongEffect* phongEffect = dynamic_cast<PhongEffect*>(effect);

	if(phongEffect)
	{
		if(material.hasTexture)
		{
			phongEffect->setDiffuseTexture(textureResource);
			
			phongEffect->setHasNormalMap(material.hasNormalMap);
			
			if(material.hasNormalMap)
			{	
			phongEffect->setNormalMap(normalMapResource);
			}
			else
			{
			phongEffect->setNormalMap(NULL);
			}
			
		}
		else
		{
			phongEffect->setDiffuseTexture(NULL);
			phongEffect->setNormalMap(NULL);
		}
		phongEffect->setMaterial(material);
	}

	
}

Material::~Material()
{
	if(nrOfTextures !=0)
	{
		SAFE_RELEASE(textureResource)
		if(material.hasNormalMap)
			SAFE_RELEASE(normalMapResource);
	}
	
}
