#pragma once

#include "ElementRenderer.hpp"

//#########################################################################
//#########################  ListElement  ###############################
//#########################################################################

namespace ElementRenderer {

	class GroupElement;

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

		typedef Element BaseClass;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructors/Destructors
	//
	protected:
		ListElement(
			ClassData *class_data,
			Stuff::MemoryStream *stream,
			int version,
			ShapeHolder shapes,
			bool load_children=true
		);

	public:
		ListElement(ClassData *class_data=DefaultData);
		explicit ListElement(
			GroupElement *group,
			ClassData *class_data = DefaultData
		);
		~ListElement();

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

		void Save(Stuff::MemoryStream *stream);

		static HGOSHEAP s_Heap;

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

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Hierarchy
	//
	public:
		void AttachIndexedChild(
			WORD index,
			Element *element
		);
		Element* GetIndexedElement(WORD index)
			{Check_Object(this); return m_list[index];} 
		virtual void SetSize(WORD max_size);
		virtual size_t GetLength()
			{Check_Object(this); return m_list.GetLength();};
		virtual void ReSize(WORD max_size);
		void SetActiveCount(WORD active)
			{
				Check_Object(this);
				Verify(m_activeChildren<=m_list.GetLength());
				m_activeChildren = active;
			}
		WORD GetActiveCount()
			{Check_Object(this); return m_activeChildren;}


	protected:
		void AttachChild(Element *child);
		void DetachChild(Element *child);

		Stuff::DynamicArrayOf<Element*> m_list;
		WORD m_activeChildren;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Sync support
	//
	protected:
		void SetSyncState();
		static SyncMethod SyncMethods[SyncStateCount];

		void CleanRootSyncMethod();
		void DirtyRootSyncMethod();

		void CleanSyncMethod();
		void DirtySyncMethod();

		void CleanSphereSyncMethod();
		void MatrixDirtySphereSyncMethod();
		void BoundsWrongSphereSyncMethod();
		void FullSphereSyncMethod();

		void CleanOBBSyncMethod();
		void MatrixDirtyOBBSyncMethod();
		void BoundsWrongOBBSyncMethod();
		void FullOBBSyncMethod();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// 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
		);

	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
		);

		int CountTriangles();

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

}
