#pragma once

#include "MW4.hpp"

#include <Adept\Receiver.hpp>

namespace MechWarrior4 
{

	class Subsystem;
	class Weapon;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Subsystem Salvage Plug
	//
	class SalvagePlug:
		public Stuff::Plug
	{
	public:
		SalvagePlug(Subsystem *subsytem, const char *salvage_name);
		SalvagePlug(Adept::ResourceID salvage_id, Adept::ResourceID data_list, const char *salvage_name);

		~SalvagePlug();

		Subsystem
			*m_subsystem;
		Adept::ResourceID
			m_salvageID;
		Adept::ResourceID
			m_dataListID;
		int
			m_count;
		MString
			m_salvageName;

		//Used for mission review and never saved
		int
			m_newCount;
	};

	typedef Receiver__ClassData				SalvageManager__ClassData;
	typedef Receiver__Message				SalvageManager__Message;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Internal Damage Object class
	//
	class SalvageManager:
		public Adept::Receiver
	{
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Inheritance Support
	//
	public:
		typedef SalvageManager__ClassData		ClassData;
		typedef SalvageManager__Message		  	Message;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Construction, Destruction, Testing
	//
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

		static void
			ConstructSalvageStream(
				Stuff::MemoryStream *stream,
				NotationFile *notation_file
			);

		static SalvageManager*
			MakeNewSalvageManager(const ResourceID& salvage_resource_id);
		static SalvageManager*
			MakeNewSalvageManager(const char *salvage_name);

		void
			Save(MemoryStream *stream);
		void
			WriteToText(const char *file_name);

		SalvageManager(Stuff::MemoryStream *stream);

		~SalvageManager();

		void
      		TestInstance();
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Class Data support
	//
	public:
		static ClassData 
			*DefaultData;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Execution Support
	//
	public:
		void
			GetAndAddComponentSalvage(const char *salvage_string, int number);
		void
			GetAndAddWeaponSalvage(const char *salvage_string, int number);
		void
			AddSalvage(Subsystem *subsystem, const char *salvage_string, bool is_new=false);
		void
			AddSalvage(const ResourceID& salvage_id, const ResourceID& data_list, const char *salvage_string, bool is_new=false);
		void
			RemoveSalvage(Subsystem *subsystem);
		void
			RemoveSalvage(const ResourceID& salvage_id);
		
		Stuff::SortedChainOf<SalvagePlug*, int>
			m_salvageList;
		Stuff::ChainOf<Stuff::PlugOf<MString> *>
			m_mechSalvageNames;
		int
			m_totalSalvage;

		static SalvageManager*
			GetInstance() 
		{
			return reinterpret_cast<SalvageManager *>( GlobalPointers::GetGlobalPointer(SalvageManagerGlobalPointerIndex));
		};

		
	};
}

