#pragma once

#include "MW4Headers.hpp"
#if defined( BLACK_KNIGHT )
#include "mechlabheaders_BK.h"
#elif defined( MERCENARIES )
#include "mechlabheaders_MERCS.h"
#elif defined( VENGEANCE )
#include "mechlabheaders_VENG.h"
#endif
#include "ShellScriptHeaders.hpp"
#include "ScriptErrorHeader.hpp"


namespace MechWarrior4
{
	class Mech;
	class Weapon;
}



//The Black Market trading chits
//The chits represent both weapons as well as Mech chassis
//The IDs for Mech Chits are the Mech ID +  FirstMechChitID  (#defined in MEchLabHeaders)


class BlackMarketChit:
		public Stuff::Plug
{
	public:
//		BlackMarketChit (int chitTypeID, char* chitName, MoneyType tradeValue); //**- 
		BlackMarketChit (int chitTypeID, char* chitName, float tradeValue);
		BlackMarketChit (int chitTypeID, char* chitName);

		~BlackMarketChit()
		{
		}

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

//		MoneyType GetTradeValue () //**- 
		float GetTradeValue ()
		{
			return m_tradeValue;
		}

		float GetRange ()
		{
			return m_range;
		}

		float GetDamage ()
		{
			return m_damage;
		}

		float GetHeat ()
		{
			return m_heat;
		}

		float GetRecycle ()
		{
			return m_recycle;
		}

private:
		int m_chitTypeID;
		
		MString	m_chitName;
		
//		MoneyType m_tradeValue; //**- 
		float m_tradeValue;

		// These support weapon statistics shown in the infobox
		float m_range;
		float m_damage;
		float m_heat;
		float m_recycle;

		int m_count;
};

class MarketPlace
{
public:

	MarketPlace();
	~MarketPlace ();


	static MarketPlace* Instance;


	void StartUp();
	void ShutDown();

	int Exit();

	//GOS script call backs

	static int _stdcall BM_CallbackHandler (void* instance, int count, void* data[]);


	//set up list 
	int SetupSalvageChits (void* data[], int count);
	int SetupOfferingChits (void* data[], int count);
	int SetupMarketChits (void* data[], int count);
	int SetupAcquiringChits (void* data[], int count);

	int SetupBeamWeaponSalvageChits (void* data[], int count);
	int SetupBallisticWeaponSalvageChits (void* data[], int count);
	int SetupMissileWeaponSalvageChits (void* data[], int count);
	int SetupMechSalvageChits (void* data[], int count);

	int SetupBeamWeaponMarketChits (void* data[], int count);
	int SetupBallisticWeaponMarketChits (void* data[], int count);
	int SetupMissileWeaponMarketChits (void* data[], int count);
	int SetupMechMarketChits (void* data[], int count);

	//move things in the list
	int OfferItemForTrade (void* data[], int count);
	int CancelOfferForTrade (void* data[], int count);
	int AcquireItemForTrade (void* data[], int count);
	int CancelAcquireForTrade ( void* data[], int count);
	int CancelAllTrades (void* data[], int count);

	//Execute the trade that has been set up 
	int ExecuteTrade (void *data[], int count);
	int GetOfferingValue  (void *data[], int count);
	int GetAcquiringValue (void * data[], int count);

	int IsTradeValid (void *data[], int count);

private:
	void InitializeChits ();
	void InitializeSalvageChits ();
	void InitializeBlackMarketChits ();

	int SetupChits (Stuff::SortedChainOf<BlackMarketChit*, int>& chitList, bool applyMarkup, void* data[], int count);
	int SetupBeamWeaponChits (Stuff::SortedChainOf<BlackMarketChit*, int>& chitList, bool applyMarkup, void* data[], int count);
	int SetupBallisticWeaponChits (Stuff::SortedChainOf<BlackMarketChit*, int>& chitList, bool applyMarkup, void* data[], int count);
	int SetupMissileWeaponChits (Stuff::SortedChainOf<BlackMarketChit*, int>& chitList, bool applyMarkup, void* data[], int count);
	int SetupMechChits (Stuff::SortedChainOf<BlackMarketChit*, int>& chitList, bool applyMarkup, void* data[], int count);

	bool ExecuteTrade();
	bool IsTradeValid();
	void MakePayment ();
	void ReceiveGoods();

//	MoneyType GetTotalOfferingValue (); //**- 
//	MoneyType GetTotalAcquiringValue ();
	float GetTotalOfferingValue ();
	float GetTotalAcquiringValue ();


	//Members to manage the Chit list 
	//Remember that the Mech IDs are offset by FirstMechChitID
	//And the names are the ones in the Weapon's table, and the Mech chassis table
	int  OfferItemForTrade (int id);
	void  CancelOfferForTrade (int id);
	void  AcquireItemForTrade (int id);
	void  CancelAcquireForTrade (int id);

	void AddSalvageChit (int chitTypeID, char* chitName);
//	void AddSalvageChit (int chitTypeID, char* chitName, MoneyType tradeValue); //**- 
	void AddSalvageChit (int chitTypeID, char* chitName, float tradeValue);
	void RemoveSalvageChit (int chitTypeID);

	void AddOfferingChit (int chitTypeID, char* chitName);
//	void AddOfferingChit (int chitTypeID, char* chitName, MoneyType tradeValue); //**- 
	void AddOfferingChit (int chitTypeID, char* chitName, float tradeValue);
	void RemoveOfferingChit (int chitTypeID);

	void AddMarketChit (int chitTypeID, char* chitName);
//	void AddMarketChit (int chitTypeID, char* chitName, MoneyType tradeValue); //**- 
	void AddMarketChit (int chitTypeID, char* chitName, float tradeValue);
	void RemoveMarketChit(int chitTypeID);

	void AddAcquiringChit (int chitTypeID, char* chitName);
//	void AddAcquiringChit (int chitTypeID, char* chitName, MoneyType tradeValue); //**- 
	void AddAcquiringChit (int chitTypeID, char* chitName, float tradeValue);
	void RemoveAcquiringChit (int chitTypeID);


	//variables
	Stuff::SortedChainOf<BlackMarketChit*, int> 
		m_salvageChits;
	Stuff::SortedChainOf<BlackMarketChit*, int>
		m_offeringChits;
	Stuff::SortedChainOf<BlackMarketChit*, int>
		m_marketChits;
	Stuff::SortedChainOf<BlackMarketChit*, int>
		m_acquiringChits;


};
