//====== Copyright © 1996-2003, Valve Corporation, All rights reserved. =======
//
// Purpose: 
//
//=============================================================================

#include "cbase.h"
#include "shareddefs.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterial.h"
#include "view.h"
#include "iviewrender.h"
#include "view_shared.h"
#include "texture_group_names.h"
#include "tier0/icommandline.h"

#include "../public/materialsystem/MaterialSystemUtil.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class C_EnvProjectedTextureUnlit : public C_BaseEntity
{
	DECLARE_CLASS( C_EnvProjectedTextureUnlit, C_BaseEntity );
public:
	DECLARE_CLIENTCLASS();

	virtual void OnDataChanged( DataUpdateType_t updateType );
	void	ShutDownProjectedTextureHandle( void );

	virtual void Simulate();

	void	UpdateProjectedTexture( bool bForceUpdate );

	C_EnvProjectedTextureUnlit();
	~C_EnvProjectedTextureUnlit();

private:

	ClientShadowHandle_t m_PTHandle;

	bool	m_bState;
	char	m_ProjectedTextureName[ MAX_PATH ];
	int		m_nProjectedTextureFrame;
	CMaterialReference m_ProjectTexture;
};

IMPLEMENT_CLIENTCLASS_DT( C_EnvProjectedTextureUnlit, DT_EnvProjectedTextureUnlit, CEnvProjectedTextureUnlit )
	RecvPropBool(	 RECVINFO( m_bState )			),
	RecvPropString(  RECVINFO( m_ProjectedTextureName ) ),
	RecvPropInt(	 RECVINFO( m_nProjectedTextureFrame ) ),
END_RECV_TABLE()

C_EnvProjectedTextureUnlit::C_EnvProjectedTextureUnlit( void )
{
	m_PTHandle = CLIENTSHADOW_INVALID_HANDLE;
}

C_EnvProjectedTextureUnlit::~C_EnvProjectedTextureUnlit( void )
{
	ShutDownProjectedTextureHandle();
}

void C_EnvProjectedTextureUnlit::ShutDownProjectedTextureHandle( void )
{
	// Clear out the light
	if( m_PTHandle != CLIENTSHADOW_INVALID_HANDLE )
	{
		g_pClientShadowMgr->DestroyPTUnlit( m_PTHandle );
		m_PTHandle = CLIENTSHADOW_INVALID_HANDLE;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : updateType - 
//-----------------------------------------------------------------------------
void C_EnvProjectedTextureUnlit::OnDataChanged( DataUpdateType_t updateType )
{
	//ShutDownProjectedTextureHandle();
	UpdateProjectedTexture( true );
	BaseClass::OnDataChanged( updateType );
}

void C_EnvProjectedTextureUnlit::UpdateProjectedTexture( bool bForceUpdate )
{
	if ( m_bState == false )
	{
		if ( m_PTHandle != CLIENTSHADOW_INVALID_HANDLE )
		{
			ShutDownProjectedTextureHandle();
		}

		return;
	}

	if( m_PTHandle == CLIENTSHADOW_INVALID_HANDLE )
	{
		if( m_ProjectedTextureName[0] )
		{
			m_ProjectTexture.Init(m_ProjectedTextureName, TEXTURE_GROUP_DECAL);
			if( m_ProjectTexture.IsValid() )
			{
				Vector vAbsMins, vAbsMaxs;
				if( CollisionProp() )
				{
					//CollisionProp()->WorldSpaceAABB( &vAbsMins, &vAbsMaxs );
					vAbsMins = CollisionProp()->OBBMins();
					vAbsMaxs = CollisionProp()->OBBMaxs();

					ProjectTextureUnlitInfo_t info;
					info.m_PTUnlitShadow = &(m_ProjectTexture);
					info.m_PTUnlitMins = vAbsMins;
					info.m_PTUnlitMaxs = vAbsMaxs;
					info.m_cR = 255;
					info.m_cG = 255;
					info.m_cB = 255;
					info.m_cA = 255;
					info.m_vOrigin = GetAbsOrigin();
					info.m_Angle = GetAbsAngles();

					int flags = SHADOW_FLAGS_SHADOW|SHADOW_FLAGS_PROJECTTEXTUREUNLIT;
					m_PTHandle = g_pClientShadowMgr->CreatePTUnlit( info, INVALID_CLIENTENTITY_HANDLE, flags );
				}
			}
		}
	}
}

void C_EnvProjectedTextureUnlit::Simulate( void )
{
	UpdateProjectedTexture( false );

	BaseClass::Simulate();
}

