/**
* FX Script
* @version: 12-26-2004
* @author Joseph 'Pcjoe' Florencio
*/

#include "fxsys.h"
#include "../vgui_TeamFortressViewport.h"
#include "../vgui_fxtoolpanel.h"
#include "../tf_defs.h"

HSPRITE g_hsprBlack;

/*
* Init
*/
int CFXScript::Init( void )
{	m_iFlags |= HUD_ACTIVE;
	gHUD.AddHudElem(this);
	return 1;
}

/*
* Precache
*/
int CFXScript::VidInit(void)
{	// Originally by harSens, moved by Pcjoe
	if (!g_FMODWrapper.Init())
	{	ConsolePrint("Couldn't load fmod\n");
	}

	// Load black sprite
	g_hsprBlack = LoadSprite("sprites/black.spr");

	// Init parsing and screen textures (radial blur)
	g_ParseMain.Initialize();
	g_ScreenEffects.Precache();
	int iModelIdx=0;
	gEngfuncs.CL_LoadModel( "models/mountain.mdl", &iModelIdx );
	return 1;
}

/*
* Resets GFX
*/
void CFXScript::Reset(void)
{
}
//CFXEvent *g_pEvent = NULL;

void fxtooltrig(void)
{	// Show FX Tool
	if (gViewPort->m_pCurrentMenu == gViewPort->m_pFXToolPanel)
	{//	gViewPort->m_pFXToolPanel->Reset();
		gViewPort->HideVGUIMenu();
		gHUD.m_iHideHUDDisplay &= ~HIDEHUD_ALL;
	}
	else
	{	gViewPort->ShowVGUIMenu(MENU_FXTOOL);
		gHUD.m_iHideHUDDisplay |= HIDEHUD_ALL;
	}
}

CFXEvent *xmlnew()
{	
	CFXEvent *pNewEvent = new CFXEvent();
	cl_entity_t *pLocal = gEngfuncs.GetLocalPlayer();

	// Set start/end time
	pNewEvent->setStartTime(gEngfuncs.GetClientTime());
	pNewEvent->setEndTime(gEngfuncs.GetClientTime()+55);
	// Set owned index
	pNewEvent->setIndex(pLocal->index);
	if(pLocal)
	{	pNewEvent->setBasePositionStatic(true);
		pNewEvent->setBaseAngleStatic(true);
		pNewEvent->setBaseAngleStatic(vec3_t(0,0,0));
		pNewEvent->setBasePositionStatic(vec3_t(333.039063, -987.742188, 9.031250));
		pNewEvent->setDynamicBaseReference(pLocal->index);
	}

	gViewPort->m_pFXToolPanel->setEvent(pNewEvent);
	gViewPort->m_pFXToolPanel->updateEventData();

	// Add to list
	g_FXSystem.addEvent(pNewEvent);
	pNewEvent->setDeveloperEvent(true);
	return pNewEvent;
}

void xmlsave()
{/*	if(g_pEvent)
	{	// Save
		std::string strDir = gEngfuncs.pfnGetGameDirectory();
		strDir.append("/fxscript/test_event.xml");
		TiXmlDocument document2(strDir.c_str());
		document2.LinkEndChild(g_pEvent->writeXML());
		document2.SaveFile();
	}*/
}

void xmlload()
{	CFXEvent *pNewEvent = xmlnew();

	// Load
	std::string strDir = gEngfuncs.pfnGetGameDirectory();
	strDir.append("/fxscript/test_event.xml");
	TiXmlDocument document2(strDir.c_str());
	if(document2.LoadFile())
	{	pNewEvent->readXML(document2.FirstChildElement());
		pNewEvent->resetTriggers();

		gViewPort->m_pFXToolPanel->setEvent(pNewEvent);
		gViewPort->m_pFXToolPanel->updateEventData();

		// hide hud
	//	gHUD.m_iHideHUDDisplay |= ( HIDEHUD_WEAPONS | HIDEHUD_ALL );
	}
}

void xmldelete()
{	g_FXSystem.Reset();
}

void fxplay()
{/*	if(!g_pEvent)
	{	xmlload();
	}

	// Play
	CFXTimeline_VGUI *pTimeline = gViewPort->m_pFXToolPanel->getTimeline();
	CTimelineMeter *pTimelineMeter = gViewPort->m_pFXToolPanel->getTimeline()->getTimelineMeter();
	if(pTimeline)
	{	if(pTimelineMeter->getPlayStatus() == TIMELINE_PLAY)
			pTimelineMeter->setCurrentTime(0);
		else
			pTimeline->Play();
	}
	// HACK HACK: Reset triggers
	if(gViewPort->m_pFXToolPanel->getEvent())
	{	gViewPort->m_pFXToolPanel->getEvent()->resetTriggers();
	}*/
}


/**
* Think
*/
void CFXScript::Think( void )
{	// Update viewport
	if(gViewPort->m_pFXToolPanel)
	{	gViewPort->m_pFXToolPanel->Update();
	}
}