#pragma once

#include "MW4.hpp"

#include <Adept\Receiver.hpp>

namespace MechWarrior4 
{

	class Subsystem;
	class Weapon;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Subsystem BlackMarket Plug
	//

	//The Black Market Chits represent items available on the Black Market
	//They contain the name of the salvage , which should be the same name as in the
	//weapon and Mech tables, so that they can be spawned as actual Mechs and Weapons
	//when the purchase has been made.
	//The IDs for the Weapons are identiacal to the Weapon IDs in the Mech Lab Headers 
	//While the Mech Chit ID sre offset by #define FirstMechChitID

	class BlackMarketPlug:
		public Stuff::Plug
	{
	public:
		BlackMarketPlug (int chitTypeID, char* chitName):
		  	Plug(Plug::DefaultData),
			m_chitName (chitName),
			m_chitTypeID (chitTypeID),
			m_count (1)
		{				
		}

		~BlackMarketPlug()
		{
		}

		bool IsMechChit ()
		{
			return (m_chitTypeID >= FirstMechChitID);
		} 
		
		MString& GetChitName () 
		{
			return m_chitName;
		}
		
		int GetChitTypeID()
		{
			return m_chitTypeID;
		}

		int GetCount ()
		{
			return m_count;
		}

		void Add ()
		{
			m_count ++;
		}

		void Subtract ()
		{
			m_count --;
		}

		bool IsEmpty ()
		{
			return m_count <= 0;
		}

private:
		MString
			m_chitName;
		int 
			m_chitTypeID;
		int
			m_count;
	};

///////////////////     Now the Black Market Manager  //////////////////////////////


	typedef Receiver__ClassData				BlackMarketManager__ClassData;
	typedef Receiver__Message				BlackMarketManager__Message;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Internal Damage Object class
	//
	class BlackMarketManager:
		public Adept::Receiver
	{
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Inheritance Support
	//
	public:
		typedef BlackMarketManager__ClassData		ClassData;
		typedef BlackMarketManager__Message		  	Message;

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

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

		static BlackMarketManager*
			MakeNewBlackMarketManager(const ResourceID& blackMarketResourceID);
		
		static BlackMarketManager*
			MakeNewBlackMarketManager(const char *blackMarketName);	

		void
			Save(MemoryStream *stream);

		BlackMarketManager(Stuff::MemoryStream *stream);

		~BlackMarketManager();

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

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Execution Support
	//
	public:
		//The members to add items to the Black Market 
		void AddWeapon (char* szWeaponName);
		void AddMech   (char* szMechName);
		void AddItem   (int itemID);
		void AddItem   (int itemID, char* szItemName);
		
		//get info
		int GetWeaponCount (char* szWeaponName);
		int GetMechCount (char* szMechName);
		int GetTotalItemCount() { return m_totalItems; };

		//The removal 
		void RemoveWeapon (char* szItemName);
		void RemoveAllWeapons ();
		void RemoveMech   (char* szMechName);
		void RemoveAllMechs ();
		void RemoveItem (int itemID);
		

		static BlackMarketManager*
			GetInstance() 
		{
			return reinterpret_cast<BlackMarketManager *>( GlobalPointers::GetGlobalPointer(BlackMarketManagerGlobalPointerIndex));
		};


		Stuff::SortedChainOf<BlackMarketPlug*, int>
			m_chitList;

private:
		int
			m_totalItems;
	
	};
}

