
#pragma once

#include "MW4.hpp"
#include "Subsystem.hpp"

namespace MechWarrior4
{
	class Vehicle;

	//##########################################################################
	//##################    AMS::CreateMessage    ##############################
	//##########################################################################

	class AMS__CreateMessage:
		public Subsystem__CreateMessage
	{
	public:
		typedef Subsystem__CreateMessage BaseClass;

		int 
			ammoCount;

		AMS__CreateMessage(
			size_t length,
			int priority,
			int message_flags,
			Stuff::RegisteredClass::ClassID class_id,
			int replicator_flags,
			const ResourceID& instance_id,
			const Stuff::LinearMatrix4D &creation_matrix,
			Stuff::Scalar age,
			int execution_state,
			int name_id,
			int entity_alignment,
			int subsystem_index,
			BYTE location_id,
			int critical_hits,
			int ammo_count
		):
			Subsystem__CreateMessage(
				length, 
				priority, 
				message_flags, 
				class_id, 
				replicator_flags,
				instance_id,
				creation_matrix,
				age,
				execution_state,
				name_id,
				entity_alignment,
				subsystem_index,
				location_id,
				critical_hits
			),
				ammoCount(ammo_count)
			{}

		static void
			ConstructCreateMessage(Script *script);
	};

	//##########################################################################
	//###########################   AMS GameModel    ###########################
	//##########################################################################

	class AMS__GameModel:
		public Subsystem::GameModel
	{
	public:
		typedef Subsystem::GameModel BaseClass;

		Adept::ResourceID
			amsEffectResource;

		enum {
			AMSEffectResourceAttributeID = Subsystem__GameModel::NextAttributeID,
			NextAttributeID
		};

		static bool
			ReadAndVerify(
				AMS__GameModel *model,
				ModelAttributeEntry *attribute_entry, 
				const char *data,
				char **error,
				int error_buffer = 128
			);


		static void
			ConstructGameModel(Script *script);
	};


	typedef Subsystem__ClassData							AMS__ClassData;
	typedef Subsystem__Message								AMS__Message;
	typedef Subsystem__ExecutionStateEngine					AMS__ExecutionStateEngine;

	//##########################################################################
	//###########################   AMS    #####################################
	//##########################################################################

	class AMS:
		public Subsystem
	{
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

		typedef Subsystem BaseClass;

	//##########################################################################
	// Inheritance support
	//
	public:
		typedef AMS__ClassData						ClassData;
		typedef AMS__GameModel						GameModel;
		typedef AMS__Message						Message;
		typedef AMS__ExecutionStateEngine			ExecutionStateEngine;
		typedef AMS__CreateMessage					CreateMessage;


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Run-time Construction and Destruction Support
	//
	public:
		static AMS* 
			Make(
				CreateMessage *message,
				ReplicatorID *base_id
			);

		Adept::Replicator::CreateMessage*
			SaveMakeMessage(MemoryStream *stream, Adept::ResourceFile *res_file);

		void
			SaveInstanceText(Stuff::Page *page);

		static void
			CreateStream(
				Adept::ResourceID data_list,
				Stuff::MemoryStream *stream
			);

	protected:
		AMS(
			ClassData *class_data,
			CreateMessage *message,
			ReplicatorID *base_id,
			ElementRenderer::Element *element
		);

		~AMS();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Class Data & Game Model Support
	//
	public:
		const GameModel*
			GetGameModel()
				{return Cast_Pointer(const GameModel*, gameModelResource.GetPointer());}
	public:
		static ClassData
			*DefaultData;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Simulation Support
	//
	public:
		void
			DestroySubsystem();
		bool
			DoesHaveAmmo()
				{
					Check_Object(this); 
					if (ammoCount == -1) 
						return true; 
					
					return ammoCount != 0;
				}

		int GetAmmo()
			{Check_Object(this); return ammoCount;}

		void
			SetAmmo(int ammo_number)
				{Check_Object(this); ammoCount = ammo_number;};

		int GetMaxAmmoCount()
				{Check_Object(this); return ammoCount;};



		void
			SubtractAmmo(int count);
		int
			ammoCount;
		int 
			maxAmmoCount;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Test Support
	//
	public:
		void
      		TestInstance() const;
	};
}


