#pragma once

#include "ElementRenderer.hpp"
#include "Element.hpp"

namespace ElementProxies {
	class ElementGroupProxy;
	class ElementSceneProxy;
}

namespace ElementRenderer {

	//#########################################################################
	//#########################  GroupElement  ################################
	//#########################################################################

	class GroupElement:
		public Element
	{
		friend class ElementProxies::ElementGroupProxy;
		friend class ElementProxies::ElementSceneProxy;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void InitializeClass();
		static void TerminateClass();
		static ClassData *DefaultData;

		typedef Element BaseClass;

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

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

		static GroupElement* Make(
			Stuff::MemoryStream *stream,
			int version,
			ShapeHolder shapes=NULL
		);

		void Save(Stuff::MemoryStream *stream);

		static HGOSHEAP s_Heap;

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

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Hierarchy
	//
	public:
		void AttachChild(Element *child);
		void DetachChild(Element *child);
		void DetachChildren();
		Stuff::ChainIteratorOf<Element*>* MakeIterator()
			{
				Check_Object(this);
				return new Stuff::ChainIteratorOf<Element*>(&m_group);
			}

	protected:
		Stuff::ChainOf<Element*> m_group;

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

	public:
		int CountTriangles();

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

}
