#include "CAnimation.h"
#include "Assert.h"

#include <iostream>
#include <fstream>
#include <string>

//----------------------------------------------------------------//
//	Constructor/Deconstructor
//----------------------------------------------------------------//

CAnimation::CAnimation()
{

}

CAnimation::~CAnimation()
{


}

void CAnimation::Init(STRINGC line)
{
	/*
	line will be in this format:
	AnimationName=animationfile.txt X
	(where X:
		0 = play once
		1 = repeat. Jump from Last frame to first
		2 = repeat. Play till end, then play in reverse order, repeat.

	*/

	STRINGC fn;					//FileName
	fn.CopyUpTo(line, ' ');			

	STRINGC properties = line.RightStr(line.Len() - line.CountUntil(' ') -1);	
	int repeat=0;
	sscanf(properties.GetString(),"%i",&repeat);

	switch(repeat)
	{
	case 0:
		m_Repeat=NoRepeat;
		break;

	case 1:
		m_Repeat=JumpRepeat;		
		break;

	case 2:
		m_Repeat=BackwardsRepeat;		
		break;

	default:
		Assert(false, STRINGC("Unkown Repeat Type: ") + STRINGC(repeat));
		break;

	}




	//open up file
	ifstream infile;
	infile.open(fn.GetString());

	//loop through file
	while(infile.eof() == false)
	{		
		string str;
		getline(infile, str);

		STRINGC line(str.c_str());

		//create a new frame based off each line
		CFrame* frame = new CFrame();
		frame->Init(line);

		//insert frame into list

		m_Frames.push_back(*frame);


	}

	

	//end loop

	//Set some pointer to the current frame

	infile.close();
	m_currentFrame = m_Frames.begin();
	


}

//----------------------------------------------------------------//
//
//----------------------------------------------------------------//

AnimationState CAnimation::Update()
{

	//see if we need to advance
	if(--m_currentTick > 0)		//Possible bug here if we were to ever have an animation that was being used as NULL long enough...
		return Waiting;

	if(m_currentFrame==NULL)
		return Done;

	advanceFrame();
	
	//If we should advance the frame:
	FrameListIterator tmp = m_Frames.begin();
	tmp--;
	if(m_currentFrame == m_Frames.end() | m_currentFrame == (tmp))
	{	
	
		//We are at the end. Should we repeat?
		
		switch(m_Repeat)
		{

		case NoRepeat:
			//We're done.			
			m_currentFrame = NULL;
			return Done;			
			break;

		case JumpRepeat:
			//Loop Back to start
			m_currentFrame = m_Frames.begin();			
			break;

		case BackwardsRepeat:
			//switch directions and advance frame
			m_movingForward = !(m_movingForward);
			advanceFrame();

			break;

		default:
			Assert(false, STRINGC("Unknown Repeat Type: ") + STRINGC(m_Repeat));
			break;
		}
		
	}
	

	m_currentTick = (*m_currentFrame).getDelay();	

	return Updating;
	

}

CImage* CAnimation::getCurrentImage()
{

	if(m_currentFrame==NULL)
		return NULL;

	return (*m_currentFrame).getImage();	
	
}

POINT CAnimation::getCurrentDisplacement()
{
	POINT p;
	p.x=0;
	p.y=0;

	if(m_currentFrame!=NULL)
		p = (*m_currentFrame).getDisplacement();


	return p;
}

void CAnimation::Reset()
{
	m_currentFrame = m_Frames.begin();
	m_currentTick = (*m_currentFrame).getDelay();	
}


void CAnimation::advanceFrame()
{
	if(m_movingForward)
	{
		m_currentFrame++;
	}
	else
	{
		m_currentFrame--;

	}

}