#include "SpritePartconfigList.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SpritePartconfigList::SpritePartconfigList()
{

	head = NULL;
	numItems2 = 0;
}

///and load from file
void SpritePartconfigList::load(char* filename )
{

	ifstream inData;
	inData.open(filename );
	string namer ;
	char trash ;
	int num = 0;
	int num2 = 0;
			 
	inData >> num;
	inData >> trash;	

	while( 	numItems2 < num)
  	{
textout(screen,font,"loading spriteParts",100,400,makecol(255,255,255));
Sleep(1000);		
		SpritePartconfig* nd;
		nd = new SpritePartconfig();

		inData >> namer;
		nd->partObject.set_partName(namer);
		//inData >> trash;


		

		inData >> num2;
		nd->partObject.set_partNumber(num2);
		inData >> trash;
		//textprintf_centre(screen, font, 40, 200, makecol(255, 255, 255),"FPS:%d", num2);

		inData >> num2;
		nd->partObject.set_charSetNumber(num2);
		inData >> trash;

		inData >> num2;
		nd->partObject.set_source_X(num2);
		inData >> trash;

		inData >> num2;
		nd->partObject.set_source_Y(num2);
		inData >> trash;

		inData >> num2;
		nd->partObject.set_w(num2);
		inData >> trash;

		inData >> num2;
		nd->partObject.set_h(num2);
		inData >> trash;



		
		if(numItems2 == 0)
		{
			head = nd;
		}
		else
		{
			head->SetPre(nd);
			nd->SetNext( head );
			head = nd;
		}
		numItems2++;
	
	}

	inData.close();
	
	
}






	



///and save to file
void SpritePartconfigList::save(char* filename )
{
	SpritePartconfig* nd;

	nd = head;
	
	ofstream outData;
	outData.open(filename );
	
	outData << numItems2;
	outData << ",";
	outData << endl;
	while( nd != NULL ) 
	{
		
		outData << nd->partObject.get_partName();
		outData << endl;
		
		outData << nd->partObject.get_charSetNumber();
		outData << ",";
		outData << nd->partObject.get_charSetNumber();
		outData << ",";
		outData << nd->partObject.get_source_X();
		outData << ",";
		outData << nd->partObject.get_source_Y();
		outData << ",";
		outData << nd->partObject.get_w();
		outData << ",";
		outData << nd->partObject.get_h();
		outData << endl;
		
		nd = (nd->GetNext( ));
		}
	
	
	outData.close();
	
}
////





void SpritePartconfigList::traverse2( )
{
	
	SpritePartconfig* nd;
	nd = head;
	
	for( int i = 0; i < numItems2; i++ )
	{
		
		nd = nd->GetNext();
	}
	
}

bool SpritePartconfigList::isEmpty( )
{
	return( numItems2 == 0 );
}

 int SpritePartconfigList::GetNumItems( )
{
	return numItems2;
}

bool SpritePartconfigList::IsInList( int d )
{

	SpritePartconfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->partObject.get_partNumber() == d )
			return true;
		nd = nd->GetNext( );
	}
	
	return false;
}



int SpritePartconfigList::IndexOf( int d )
{
	
	SpritePartconfig* nd;
	nd = head;
	int index = 0;
	while( nd != NULL )
	{
		if( nd->partObject.get_partNumber() == d )
			return index;
		nd = nd->GetNext( );
		index++;
	}
	
	return -1;
}

SpritePart SpritePartconfigList::Find_Part_id(int d)
{
SpritePartconfig* nd;
	nd = head;
	int index = 0;
	
	while( nd != NULL )
	{

		if( nd->partObject.get_partNumber() == d )
		{
		
		return  nd->partObject;
		}
	nd = nd->GetNext( );	
	}
textout(screen,font," SpritePart not found",120,200,makecol(255,255,255));
Sleep(1000);	
return SpritePart();
}







int SpritePartconfigList::Find_Part_id( int d ,int c)
{
	/*
	TileAnimationConfig* nd;
	nd = head;
	int index = 0;
	
	while( nd != NULL )
	{

		if( nd->animeObject.Get_animate_id() == d )
		{

			if (c == 1)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame2());

			}
			else if(c == 2)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame3());

			}
			else if(c == 3)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame4());

			}
			else if(c == 4)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame5());

			}
			else 
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame1());

			}

			return nd->animeObject.Get_corrent_frame();
		}
		nd = nd->GetNext( );
		index++;
	}
	*/
	return -1;
}








SpritePartconfigList::~SpritePartconfigList()
{

}