#include "Node.h"


using namespace std;

class LinkedList  
{
private:
	Node* head;
	int numItems;
public:
	LinkedList();
	//insert at beginning
	
	void load();
	void save( );
	void traverse2( );
	bool isEmpty( );
	int GetNumItems( );
	bool IsInList( int );
	int IndexOf( int );
	bool IsInList2( int );
	
	int Find_animate_id( int d,int c );
	

	virtual ~LinkedList();

};




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

LinkedList::LinkedList()
{
	head = NULL;
	numItems = 0;
}

///and load from file
void LinkedList::load( )
{
	ifstream inData;
	inData.open("animelist.txt" );
	
	char trash ;
	int num = 0;
	int num2 = 0;
			 
	inData >> num;
	inData >> trash;	
	while( 	numItems < num)
  	{
	Node* nd;
	nd = new Node;

inData >> num2;
nd->Set_animate_id(num2);
inData >> trash;

inData >> num2;
nd->Set_num_frames(num2);
inData >> trash;

inData >> num2;
nd->Set_frame1(num2);
inData >> trash;

inData >> num2;
nd->Set_frame2(num2);
inData >> trash;

inData >> num2;
nd->Set_frame3(num2);
inData >> trash;

inData >> num2;
nd->Set_frame4(num2);
inData >> trash;

inData >> num2;
nd->Set_frame5(num2);
		
	nd->SetNext( head );
	head = nd;
	numItems++;
	
	}

	inData.close();
	
	
}






	



///and save to file
void LinkedList::save( )
{
	Node* nd;
	nd = head;
	
	ofstream outData;
	outData.open("datastuff.txt" );
	
	outData << numItems;
	outData << ",";
	outData << endl;
	while( nd != NULL ) 
	{
outData << nd->Get_animate_id();
outData << ",";
outData << nd->Get_num_frames();
outData << ",";
outData << nd->Get_frame1();
outData << ",";
outData << nd->Get_frame2();
outData << ",";
outData << nd->Get_frame3();
outData << ",";
outData << nd->Get_frame4();	
outData << ",";
outData << nd->Get_frame5();	

	 
		
		nd = nd->GetNext( );

	}
	
	
	outData.close();
	
}
////






void LinkedList::traverse2( )
{
	Node* nd;
	nd = head;
	
	for( int i = 0; i < numItems; i++ )
	{
		
		nd = nd->GetNext();
	}
	
}

bool LinkedList::isEmpty( )
{
	return( numItems == 0 );
}

int LinkedList::GetNumItems( )
{
	return numItems;
}

bool LinkedList::IsInList( int d )
{
	Node* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->animate_id == d )
			return true;
		nd = nd->GetNext( );
	}
	return false;
}



int LinkedList::IndexOf( int d )
{
	Node* nd;
	nd = head;
	int index = 0;
	while( nd != NULL )
	{
		if( nd->animate_id == d )
			return index;
		nd = nd->GetNext( );
		index++;
	}
	return -1;
}









int LinkedList::Find_animate_id( int d ,int c)
{
	Node* nd;
	nd = head;
	int index = 0;
	
	while( nd != NULL )
	{
		if( nd->animate_id == d )
		{

			if (c == 1)
			{
			nd->Set_corrent_frame(nd->frame2);

			}
			else if(c == 2)
			{
			nd->Set_corrent_frame(nd->frame3);

			}
			else if(c == 3)
			{
			nd->Set_corrent_frame(nd->frame4);

			}
			else if(c == 4)
			{
			nd->Set_corrent_frame(nd->frame5);

			}
			else 
			{
			nd->Set_corrent_frame(nd->frame1);

			}
			return nd->Get_corrent_frame();
		}
		nd = nd->GetNext( );
		index++;
	}
	return -1;
}








LinkedList::~LinkedList()
{

}