/*	kd:
	This is to handle binding stuff. It stores an item handler and an item class
	to look up. That means its behaviour is flush with the shop's when it comes
	to equipping items. */

struct Gc_Binds play {
	const bind_keys = 5;
	
	Actor             m_user;
	Gcs_ItemType      m_handler    [bind_keys];
	class <Inventory> m_item       [bind_keys];
	
	void Init (Actor user) {
		m_user = user;
		
		for(uint i = bind_keys; 0 <= --i;) {
			m_handler [i] = NULL;
			m_item    [i] = NULL;
		}
	}
	
	void Set (uint bind, Gcs_ItemType handler, class <Inventory> item) {
		if(bind_keys <= bind) {
			return;
		}
		
		// We don't want one of the two to be invalid exclusively. It's polite
		// for checks.
		if(handler == NULL)			item    = NULL;
		if(item    == NULL)			handler = NULL;
		
		m_handler [bind] = handler;
		m_item    [bind] = item;
	}
	
	void Use (uint bind) {
		if(!m_user || bind_keys <= bind) {
			return;
		}
		
		let handler = m_handler [bind];
		let item    = m_item    [bind];
		
		if(handler && item) {
			let item_title = StringTable.Localize("$" .. item.GetClassName() .. "_title");
			
			if(
			!handler.Equippable(m_user, item) &&
			consoleplayer == m_user.PlayerNumber()) {
				// This item just can't be equipped.
				Console.printf(
					"\c[ice]%s \c-%s.",
					item_title,
					StringTable.Localize("$cannot_use"));
				Gcs_ShopSounds.Failure();
			}
			
			else if(
			!handler.CanEquip(m_user, item) &&
			consoleplayer == m_user.PlayerNumber()) {
				// The item may be equipped, but you can't, cuz you might not
				// have enough.
				Console.printf(
					"\c[ice]%s \c-%s.",
					item_title,
					StringTable.Localize("$cannot_use_now"));
				Gcs_ShopSounds.Failure();
			}
			
			else {
				// Successful usage.
				/* console.printf(
					"\c[ice]%s \c-%s.",
					item_title,
					StringTable.Localize("$activated")); */
				m_handler [bind].OnEquip(m_user, m_item [bind]);
				
				if(m_item [bind] is 'SharedTome') {
					let handler = Gc_SpecialInvHandler(
						Gc_SpecialInvHandler.Find('Gc_SpecialInvHandler'));
					
					if(handler) {
						handler.PlaySpellChime(m_user, (class <SharedTome>)(m_item [bind]));
					}
				}
				
				else {
					// Gcs_ShopSounds.Buy();
				}
			}
		}
		
		else if(consoleplayer == m_user.PlayerNumber()) {
			Console.printf(StringTable.Localize("$no_bind_set"));
			Gcs_ShopSounds.Failure();
		}
	}
}
