#include "MTLreader.h"
#include "./console/StrException.h"

void MTLmaterial::clear(void)
{
	this->name.clear();
	this->map_Ka.clear();
	this->map_Kd.clear();
	this->map_Ks.clear();

	this->disolve=0;
	this->specular_exp=0;

	for ( int i=0; i<3; ++i)
	{
		this->ambient[i]=0;
		this->diffuse[i]=0;
		this->specular[i]=0;
	}
}

MTLmaterial::MTLmaterial(void)
{
	for (int i=0; i<3; ++i)
	{
		this->ambient[i]=0.0;
		this->diffuse[i]=0.0;
		this->specular[i]=0.0;
	}

	this->specular_exp=1.0;
}

MTLmaterial::~MTLmaterial(void)
{
}

MTLmaterial::MTLmaterial(const MTLmaterial& material)
{
	for (int i=0; i<3; ++i)
	{
		this->ambient[i]=material.ambient[i];
		this->diffuse[i]=material.diffuse[i];
		this->specular[i]=material.specular[i];
	}

	this->map_Ka=material.map_Ka;
	this->map_Kd=material.map_Kd;
	this->map_Ks=material.map_Ks;

	this->disolve=material.disolve;
	this->name=material.name;
	this->specular_exp=material.specular_exp;
}

 MTLreader::MTLreader(const string &fileName)
{
	this->file.open( fileName.c_str() );
	
	if ( this->file.fail() )
	{
		throw StrException("Could not open `%s' for reading.\n", fileName.c_str() );
	}
}

MTLreader::~MTLreader()
{
	this->file.close();
}

bool MTLreader::readNextMaterial( MTLmaterial& dst_storage )
{
	/*
	newmtl material_name
		Ka 0.0435 0.0435 0.0435
 		Kd 0.1086 0.1086 0.1086
 		Ks 0.0000 0.0000 0.0000
 		Tf 0.9885 0.9885 0.9885
 		illum 6
 		d -halo 0.6600
 		Ns 10.0000
 		sharpness 60
 		Ni 1.19713

		map_Ka C:\Users\Mantharis\Desktop\3ds with texture\WOOD.JPG
		map_Kd C:\Users\Mantharis\Desktop\3ds with texture\WOOD.JPG
	*/

	while (file_buffer!=MTL_NEWMATERIAL_TAG)
	{
		if (this->file.eof())
		{
			return false;
		}

		this->file>>file_buffer;	
	}


	this->file>>dst_storage.name;

	do
	{
		this->file>>file_buffer;

//VEKTOROVE ATRIBUTY
		if (file_buffer==MTL_KA_TAG)
		{
			this->file>>dst_storage.ambient[0];
			this->file>>dst_storage.ambient[1];
			this->file>>dst_storage.ambient[2];
		}
		else if (file_buffer==MTL_KD_TAG)
		{
			this->file>>dst_storage.diffuse[0];
			this->file>>dst_storage.diffuse[1];
			this->file>>dst_storage.diffuse[2];
		}
		else if (file_buffer==MTL_KS_TAG)
		{
			this->file>>dst_storage.specular[0];
			this->file>>dst_storage.specular[1];
			this->file>>dst_storage.specular[2];
		}

//SKALARNI ATRIBUTY
		else if (file_buffer==MTL_NS_TAG)
		{
			this->file>>dst_storage.specular_exp;
		}
		else if (file_buffer==MTL_DISOLVE_TAG)
		{
			this->file>>dst_storage.disolve;
		}
		else if (file_buffer==MTL_MAPKD_TAG)
		{
			getline(this->file, file_buffer);
			int pos =file_buffer.find_first_not_of(" "); //najdu skutecny zacatek cesty k souboru (tj bez pocatecnich mezer)

			dst_storage.map_Kd=file_buffer.substr(pos,file_buffer.length()-pos);

		}
		else if (file_buffer==MTL_MAPKS_TAG)
		{
			getline(this->file, file_buffer);
			int pos =file_buffer.find_first_not_of(" "); //najdu skutecny zacatek cesty k souboru (tj bez pocatecnich mezer)

			dst_storage.map_Ks=file_buffer.substr(pos,file_buffer.length()-pos);
		}
		else if (file_buffer==MTL_MAPKA_TAG)
		{
			getline(this->file, file_buffer);
			int pos =file_buffer.find_first_not_of(" "); //najdu skutecny zacatek cesty k souboru (tj bez pocatecnich mezer)

			dst_storage.map_Ka=file_buffer.substr(pos,file_buffer.length()-pos);
		}
			
	}
	while (file_buffer!=MTL_NEWMATERIAL_TAG && !this->file.eof());

	

	return true;
}




