#include "SpriteList.h"




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

SpriteList::SpriteList()
{

	head = NULL;
	numItems2 = 0;
}

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

	ifstream inData;
	inData.open(filename );
	
	char trash ;
		unsigned int num = 0;
		unsigned int num2 = 0;
		unsigned int num3 = 0;
		int offse = 0;
		int i = 0;
	inData >> num;
	inData >> trash;	

	while( 	numItems2 < num)
  	{
textout(screen,font,"loading sprites",300,400,makecol(255,255,255));
Sleep(1000);	
	

	
		Spriteconfig* nd;
		nd = new Spriteconfig();

		


		

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

		inData >> num2;
		nd->spriteObject.set_gender(num2);
		inData >> trash;

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

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

		inData >> num2;
		nd->spriteObject.set_numOfParts(num2);
		inData >> trash;
		 i = 0;
		while(i < (num2))
  		{
			//use num3 to get the ints for the part ids for par
			inData >> num3;
			nd->spriteObject.get_spritePartIds()[i] = num3;
			inData >> trash;
			inData >>offse;
			nd->spriteObject.get_spriteXOffsets()[i]= offse;
			inData >> trash;
			inData >>offse;
			nd->spriteObject.get_spriteYOffsets()[i]= offse;
			inData >> trash;
			i++;
		}
		
		if(numItems2 == 0)
		{
			head = nd;
		}
		else
		{
			head->SetPre(nd);
			nd->SetNext( head );
			head = nd;
		}
		numItems2++;
	
	}

	inData.close();
	
	
}






	



///and save to file
void SpriteList::save(char* filename )
{
	Spriteconfig* nd;

	nd = head;
	int i = 0;
	ofstream outData;
	outData.open(filename );
	
	outData << numItems2;
	outData << ",";
	outData << endl;
	while( nd != NULL ) 
	{
		




		outData << nd->spriteObject.get_spriteNumber();
		outData << ",";
		outData << nd->spriteObject.get_gender();
		outData << ",";
		outData << nd->spriteObject.get_w();
		outData << ",";
		outData << nd->spriteObject.get_h();
		outData << ",";
		outData << nd->spriteObject.get_numOfParts();
		outData << ",";
		

while(i < nd->spriteObject.get_numOfParts())
  		{
			
		
			outData << nd->spriteObject.get_spritePartIds()[i] ;
			outData << ",";
			outData << 	nd->spriteObject.get_spriteXOffsets()[i];
			outData << ",";
			outData << 	nd->spriteObject.get_spriteYOffsets()[i];
			outData << ",";
			i++;
		}


		outData << endl;
		
		nd = (nd->GetNext( ));
		}
	
	
	outData.close();
	
}
////





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

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

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

bool SpriteList::IsInList( int d )
{

	Spriteconfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->spriteObject.get_spriteNumber() == d )
			return true;
		nd = nd->GetNext( );
	}
	
	return false;
}



int SpriteList::IndexOf( int d )
{
	
	Spriteconfig* nd;
	nd = head;
	int index = 0;
	while( nd != NULL )
	{
		if( nd->spriteObject.get_spriteNumber() == d )
			return index;
		nd = nd->GetNext( );
		index++;
	}
	
	return -1;
}

Sprite &SpriteList::Find_Sprite_id(int d)
{
textprintf_centre(screen, font, 40, 300, makecol(255, 255, 255),"looking for id  source:%d",d);

Spriteconfig* nd;
	nd = head;
	int index = 0;
	
	while( nd != NULL )
	{
		if( nd->spriteObject.get_spriteNumber() == d )
		{
		Sprite &temp= nd->spriteObject;
		return  temp;
		}
	nd = nd->GetNext( );	
	}
textout(screen,font," Sprite not found",120,200,makecol(255,255,255));
Sleep(1000);	
Sprite &temp = Sprite();
return temp;
}


Spriteconfig *SpriteList::Find_Sprite_id2(int d)
{
textprintf_centre(screen, font, 40, 300, makecol(255, 255, 255),"looking for id  source:%d",d);

Spriteconfig* nd = new Spriteconfig();
	nd = head;
	int index = 0;
	
	while( nd != NULL )
	{
		if( nd->spriteObject.get_spriteNumber() == d )
		{
		Spriteconfig* temp= new Spriteconfig();
			temp = nd;
		return  temp;
		}
	nd = nd->GetNext( );	
	}
textout(screen,font," Sprite not found",120,200,makecol(255,255,255));
Sleep(1000);	
Spriteconfig *temp = new Spriteconfig();
 temp= NULL;
return temp;
}





int SpriteList::Find_Sprite_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;
}








SpriteList::~SpriteList()
{

}