#include "instruction.h"
Instruction::Instruction()
{
	Reset();
}
Instruction::~Instruction()
{
	if(wzButtons != NULL)
	{
		if(numWizBtns > 1)	delete [] wzButtons;
		else				delete wzButtons;
	}
}
void Instruction::Reset()
{
	wzButtons		= NULL;
	numWizBtns		= 0;
	intFrameWidth	= WizardWidth;
	intFrameHeight	= WizardHeigh;

	intActionIndex	= -1;
	intCurFrame		= 0;
	intVarStart		= 0;
	numActions		= 0;

	bRepeatImage	= false;
	bIsActive		= false;
	bCloseAni		= false;
}
void Instruction::LoadImages(HWND hWin)
{
	HDC tempDC = GetDC(hWin);
	//This group represents figures facing User...
	bmpImages[wizActTeleportIn]				.Create(tempDC, "BMP\\wa\\C\\wiz00.bmp");
	bmpImages[wizActBowing]					.Create(tempDC, "BMP\\wa\\C\\wiz01.bmp");
	bmpImages[wizActArmsSpread]				.Create(tempDC, "BMP\\wa\\C\\wiz02.bmp");
	bmpImages[wizActRaiseArms]				.Create(tempDC, "BMP\\wa\\C\\wiz03.bmp");
	bmpImages[wizActTeleportOut]			.Create(tempDC, "BMP\\wa\\C\\wiz04.bmp");
	bmpImages[wizActHeadTiltR]				.Create(tempDC, "BMP\\wa\\C\\wiz05.bmp");
	bmpImages[wizActHeadTiltL]				.Create(tempDC, "BMP\\wa\\C\\wiz06.bmp");
	bmpImages[wizActFoldedHands]			.Create(tempDC, "BMP\\wa\\C\\wiz07.bmp");
	bmpImages[wizActTalkHeadTiltR]			.Create(tempDC, "BMP\\wa\\C\\wiz08.bmp");
	bmpImages[wizActTalkHeadTiltL]			.Create(tempDC, "BMP\\wa\\C\\wiz09.bmp");
	bmpImages[wizActArmsHeldOpen]			.Create(tempDC, "BMP\\wa\\C\\wiz10.bmp");
	bmpImages[wizActTalkHeadTiltRJesture]	.Create(tempDC, "BMP\\wa\\C\\wiz11.bmp");
	bmpImages[wizActTalkHeadTiltRHold]		.Create(tempDC, "BMP\\wa\\C\\wiz12.bmp");
	bmpImages[wizActCenterBlahBlahBlah]		.Create(tempDC, "BMP\\wa\\C\\wiz13.bmp");

	bmpImages[wizActPointLeftHighMoving]	.Create(tempDC, "BMP\\wa\\L\\wiz00.bmp");
	bmpImages[wizActPointLeftHighHold]		.Create(tempDC, "BMP\\wa\\L\\wiz01.bmp");
	bmpImages[wizActPointLeftLowMoving]		.Create(tempDC, "BMP\\wa\\L\\wiz02.bmp");
	bmpImages[wizActPointLeftLowHold]		.Create(tempDC, "BMP\\wa\\L\\wiz03.bmp");

	bmpImages[wizActPointRightHighMoving]	.Create(tempDC, "BMP\\wa\\R\\wiz00.bmp");
	bmpImages[wizActPointRightHighHold]		.Create(tempDC, "BMP\\wa\\R\\wiz01.bmp");
	bmpImages[wizActPointRightLowMoving]	.Create(tempDC, "BMP\\wa\\R\\wiz02.bmp");
	bmpImages[wizActPointRightLowHold]		.Create(tempDC, "BMP\\wa\\R\\wiz03.bmp");

	bmpMsgBG								.Create(tempDC, "BMP\\wa\\wizTalkBubble.bmp");
	bmpWizBG								.Create(tempDC, "BMP\\wa\\wizBG.bmp");

	for(int i = 0; i < wizACTSMAX; i++)
	{
		numFramesPer[i] = int(bmpImages[i].GetWidth() / intFrameWidth);
	//	if(i == wizActTeleportOut) numFramesPer[i];
	}
	ReleaseDC(hWin, tempDC);
}
void Instruction::IncrementFrame(int Action)
{
	if(intCurFrame < numFramesPer[Action] - 1)
		intCurFrame++;
	else intCurFrame = 0;
}
void Instruction::Move(int X, int Y)
{
	SetRect(&WizLocation, X, Y, X + WizardWidth, Y + WizardHeigh);
}
//This method assigns the over-all message board, and places the 
//wizard's location within it - M. White, 8 June, 2019
void Instruction::MoveMsg(int L1, int T1, int R1, int B1, int L2, int T2, int R2, int B2)
{
	Move(L2, T2);//Move the Wizard
	SetRect(&MsgRect, L1, T1, R1, B1);//Set up the background area
}
int	Instruction::GetFrame() const
{
	return intCurFrame;
}
int	Instruction::GetFrameWidth() const
{
	return 	intFrameWidth;
}
int Instruction::GetFrameHeight() const
{
	return intFrameHeight;
}
void Instruction::SetActionIndex(int Index)
{
	if(Index >= 0 && Index < wizACTSMAX)	intActionIndex = Index;
	else									intActionIndex = 0;
}
int Instruction::GetActionIndex() const
{
	return intActionIndex;
}
void Instruction::SetActive(bool data)
{
	bIsActive = data;
}
bool Instruction::GetActive() const
{
	return bIsActive;
}
void Instruction::AssignTheatric(int BMPAniIndex, int StartFrame, int NumFrames, bool Repeat)
{
	WizAniNodes tempNode;

	if(BMPAniIndex >= 0 && BMPAniIndex < wizACTSMAX)				tempNode.BMP_Index = BMPAniIndex; else return;
	if(StartFrame  >= 0 && StartFrame  < numFramesPer[BMPAniIndex])	tempNode.ImgStartFrame = StartFrame; else return;
	if(NumFrames + StartFrame <= numFramesPer[BMPAniIndex])			tempNode.HowManyFramesToShow = NumFrames; else return;
	tempNode.RepeatUntilUserInput = Repeat;
	Theatrics.insertBack(tempNode);
	numActions++;
	bIsActive = true;
}
void Instruction::ClearCurTheater()
{
	if(numActions > 0)
	{
		while(Theatrics.isEmpty() == false)
		{
			Theatrics.popFront();
		}
		numActions	= 0;
	}
}
void Instruction::PaintSelf(HDC hDC, int MouseX, int MouseY)
{
	int x, y, i, j;
	WizAniNodes tempNode;
	
	//The wizard and its speach bubble...
	bmpWizBG.Draw(hDC, WizLocation.left - 4, WizLocation.top - 4, true);//background
	bmpImages[intActionIndex].DrawPart(hDC, 
		WizLocation.left, WizLocation.top, intCurFrame * WizardWidth, 0, WizardWidth, WizardHeigh, true);

	if(bDisplayMsg == true)
	{
		//Wizard's Message Background
		bmpMsgBG.DrawPart(hDC, MsgRect.left,       MsgRect.top,           0,   0, 19, 19, true);	//Corner-top left
		bmpMsgBG.DrawPart(hDC, MsgRect.right - 19, MsgRect.top,         101,   0, 19, 19, true);	//Corner-top right
		bmpMsgBG.DrawPart(hDC, MsgRect.left,       MsgRect.bottom - 19,   0, 104, 19, 19, true);	//Corner-bottom left
		bmpMsgBG.DrawPart(hDC, MsgRect.right - 19, MsgRect.bottom - 19, 101, 104, 19, 19, true);	//Corner-bottom right
		//Horizontal top/bottom
		for(x = 0; x < int(((MsgRect.right - 19) - (MsgRect.left + 19)) / 82); x++)
		{
			bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.top,         19,   0, 82, 19, false);
			bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.bottom - 19, 19, 104, 82, 19, false);
		}
		bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.top,         19,   0, ((MsgRect.right - 19) - (MsgRect.left + 19)) - (x * 82), 19, false);
		bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.bottom - 19, 19, 104, ((MsgRect.right - 19) - (MsgRect.left + 19)) - (x * 82), 19, false);
		//Vertical Left/Right
		for(y = 0; y < int( ((MsgRect.bottom - 19) - (MsgRect.top + 19)) / 85); y++)
		{
			bmpMsgBG.DrawPart(hDC, MsgRect.left,       MsgRect.top + 19 + (y * 85),   0, 19, 19, 85, false);
			bmpMsgBG.DrawPart(hDC, MsgRect.right - 19, MsgRect.top + 19 + (y * 85), 101, 19, 19, 85, false);
		}
		bmpMsgBG.DrawPart(hDC, MsgRect.left,       MsgRect.top + 19 + (y * 85),   0, 19, 19, ((MsgRect.bottom - 19) - (MsgRect.top + 19)) - (y * 85), false);
		bmpMsgBG.DrawPart(hDC, MsgRect.right - 19, MsgRect.top + 19 + (y * 85), 101, 19, 19, ((MsgRect.bottom - 19) - (MsgRect.top + 19)) - (y * 85), false);
		//Main Body
		for(y = 0; y < int(((MsgRect.bottom - 19) - (MsgRect.top + 19)) / 85); y++)
		{
			for(x = 0; x < int(((MsgRect.right - 19) - (MsgRect.left + 19)) / 82); x++)
				bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.top  + 19 + (y * 85), 19, 19, 82, 85, false);
		}
		i = x; j = y;//remember what x and y used to be...
		for(y = 0; y < int(((MsgRect.bottom - 19) - (MsgRect.top + 19)) / 85); y++)
		{
			bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (i * 82), MsgRect.top  + 19 + (y * 85), 19, 19, 
				((MsgRect.right - 19) - (MsgRect.left + 19)) - (i * 82), 85, false);
		}
		for(x = 0; x < int(((MsgRect.right - 19) - (MsgRect.left + 19)) / 82); x++)
		{
			bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.top  + 19 + (j * 85), 19, 19, 82, 
				((MsgRect.bottom - 19) - (MsgRect.top + 19)) - (j * 85), false);
		}
		//The last chunk to be covered...
		bmpMsgBG.DrawPart(hDC, MsgRect.left + 19 + (x * 82), MsgRect.top  + 19 + (y * 85), 19, 19, 
			((MsgRect.right - 19) - (MsgRect.left + 19)) - (x * 82), ((MsgRect.bottom - 19) - (MsgRect.top + 19)) - (y * 85), false);
		bmpMsgBG.DrawPart(hDC, MsgRect.left - 26, MsgRect.top + 24, 0, 123, 31, 32, true);
	}
	if(intActionIndex >= 0)
	{
		if(bRepeatImage == true)
		{		
			if(intCurFrame < numFramesPer[intActionIndex] - 1) intCurFrame++;
			else
			{
				if(intVarStart > 0)	intCurFrame = intVarStart;
				else				intCurFrame = 0;
			}
		}
		else
		{
			if(intCurFrame < numFramesPer[intActionIndex] - 1)	intCurFrame++;
			else
			{
				if(numActions > 0)
				{
					Theatrics.getNodes(0, tempNode);
					Theatrics.popFront();
					numActions--;
					intActionIndex		= tempNode.BMP_Index;
					intVarStart			= tempNode.ImgStartFrame;
					intCurFrame			= 0;//tempNode.ImgStartFrame;
					bRepeatImage		= tempNode.RepeatUntilUserInput;
				}
				else
				{
					//Took this out so that the game paint functions can iterate one more time
					//to clean up any old wizard images on screen - M. White 06 August, 2019
				//	intActionIndex	= -1;
					bIsActive		= false;
				}
			}
		}
		if(bCloseAni == true)
		{
			if((MsgRect.right - MsgRect.left) > 0)
			{
				SetRect(&MsgRect, MsgRect.left += 2, MsgRect.top += 3, MsgRect.right -= 2, MsgRect.bottom -= 3);
			}
		}
	}
	else
	{
		if(numActions > 0)
		{
			Theatrics.getNodes(0, tempNode);
			Theatrics.popFront();
			intActionIndex		= tempNode.BMP_Index;
			intVarStart			= tempNode.ImgStartFrame;
			intCurFrame			= tempNode.ImgStartFrame;
			bRepeatImage		= tempNode.RepeatUntilUserInput;
			numActions--;
		}
		else bIsActive = false;
	}
}