#pragma once

#include "ElementRenderer.hpp"
#include "LODElement.hpp"

namespace MidLevelRenderer {
	class MLRShape;
}

namespace ElementRenderer {

	//#########################################################################
	//#########################  ShapeLODElement  ################################
	//#########################################################################

	class ShapeLODElement:
		public Element
	{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void InitializeClass();
		static void TerminateClass();
		static ClassData *DefaultData;

		typedef Element BaseClass;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructors/Destructors
	//
	protected:
		ShapeLODElement(
			ClassData *class_data,
			Stuff::MemoryStream *stream,
			int version,
			ShapeHolder shapes
		);

	public:
		ShapeLODElement(ClassData *class_data = DefaultData);
		~ShapeLODElement();

		static ShapeLODElement* Make(
			Stuff::MemoryStream *stream,
			int version,
			ShapeHolder shapes
		);

		void Save(Stuff::MemoryStream *stream);

		static HGOSHEAP s_Heap;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Testing
	//
	public:
		void TestInstance();
		bool CheckBounds();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Hierarchy
	//
	protected:
		void AttachChild(Element *child);
		void DetachChild(Element *child);

	public:
		void AttachLOD(
			WORD index,
			MidLevelRenderer::MLRShape *shape,
			const LODElement::Entry& ranges
		);
		int AttachLODFirstAvailableSlot(
			MidLevelRenderer::MLRShape *shape,
			const LODElement::Entry &ranges
		);
		void GetLOD(
			WORD index,
			MidLevelRenderer::MLRShape **shape,
			const LODElement::Entry** ranges
		);

		void SetLOD(
			WORD index,
			const LODElement::Entry &ranges
		);


		void SetSize(WORD max_size);
		WORD GetSize();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Sync support
	//
	public:
		const Stuff::LinearMatrix4D& GetWorldToLocal()
			{
				Check_Object(this); Check_Object(&m_worldToLocal);
				return m_worldToLocal;
			}

	protected:
		void SetSyncState();
		static SyncMethod SyncMethods[SyncStateCount];

		Stuff::LinearMatrix4D m_worldToLocal;

		void DirtySyncMethod();

		void CleanSphereSyncMethod();
		void DirtySphereSyncMethod();

		void CleanOBBSyncMethod();
		void DirtyOBBSyncMethod();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Collision
	//
	protected:
		bool CastCulledRay(CollisionQuery *query);
		Element* FindSmallestElementContainingCulled(SphereTest *test);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Drawing
	//
	protected:
		void SetDrawState();
		static DrawMethod DrawMethods[DrawStateCount];

		void InheritDrawMethod(
			CameraElement *camera,
			const StateChange *inherited_state,
			int clipping_state
		);
		void OverrideDrawMethod(
			CameraElement *camera,
			const StateChange *inherited_state,
			int clipping_state
		);

		int CullLights(
			LightList culled_lights,
			const LightList lights
		);

		int CountTriangles();

	public:
		void CleanDamage();

		void ApplyDamage(
			const Stuff::LinearMatrix4D &damage_spot,
			Stuff::Scalar radius
		);

		void ApplyDamageDecal(
			const Stuff::LinearMatrix4D &damage_spot,
			Stuff::Scalar radius,
			MidLevelRenderer::MLRTexture *texture
		);

		void CastShadow(
			const Stuff::LinearMatrix4D &shadow_to_world,
			const Stuff::UnitVector3D &sun_in_world,
			Stuff::Scalar radius
		);

		void MakeFootStep(
			MidLevelRenderer::MLRShape *shape,
			const Stuff::LinearMatrix4D &foot,
			Stuff::Scalar radius,
			MidLevelRenderer::MLRTexture *tex
		);

		MidLevelRenderer::MLRShape* GetMLRShape(CameraElement *camera=NULL);

		void GetCoordData(Stuff::DynamicArrayOf<Stuff::Point3D> *array);

	protected:
		Stuff::DynamicArrayOf<MidLevelRenderer::MLRShape*> m_MLRShapes;
		Stuff::DynamicArrayOf<LODElement::Entry> m_ranges;
	};

}
