/*	kd:
	
	Shopkeepers are by far the most regrettable feature added to this mod next
	to dancing.
	
	*/

class Gcs_Shopkeeper : Actor {
	default {
		+NOGRAVITY
		+DONTSPLASH
		+DONTBLAST
		+NOTIMEFREEZE
		-SOLID
		Scale 0.5;
		
		// WHO AM I
		Gcs_Shopkeeper.NameKey "Shopkeeper";
		Gcs_Shopkeeper.Icon "fonta56";
		
		// For offsetting the sprite vertically in your (non-rolled) view:
		Gcs_Shopkeeper.Zoffset 0;
		
		// How far in front of you the shopkeeper is:
		Gcs_Shopkeeper.ViewDistance 50;
		
		// When describing items, the language key looked up will be:
		// <item class name>_desc_<commentary key>
		Gcs_Shopkeeper.CommentaryKey "nithor";
		
		// Not all shopkeepers talk, though. In that case, the entire message
		// box will be left for item descriptions. If you set this to true, half
		// the message box will be reserved for the shopkeeper to say stuff.
		Gcs_Shopkeeper.CanMessage false;
		
		// Colours for the commentary as it is being typed, or for speech
		// through the message box or console:
		Gcs_Shopkeeper.TalkStartColor   Font.CR_BLACK;
		Gcs_Shopkeeper.TalkFinalColor   Font.CR_ICE;
		Gcs_Shopkeeper.ConsoleColor		"\c[darkred]";
	}
	
	Gcs_ShopList	shops;
	Gc_Shop			shop;
	PlayerInfo		customer;
	Actor			user_mo;
	vector3			view_offset;
	bool			shop_has_expanded;
	
	// Properties:
	double			z_offset;
	double			view_distance;
	String			commentary_key;
	int				talk_color1;
	int				talk_color2;
	String			shopkeeper_name;
	String			color_key;
	String			icon;
	bool			can_message;
	bool			is_leaving;
	
	property Zoffset:           z_offset;
	property ViewDistance:      view_distance;
	property CommentaryKey:     commentary_key;
	property TalkStartColor:    talk_color1;
	property TalkFinalColor:    talk_color2;
	property ConsoleColor:		color_key;
	property NameKey:			shopkeeper_name;
	property Icon:				icon;
	property CanMessage:		can_message;
	
	void AttachTo (PlayerInfo player) {
		customer	= player;
		user_mo	= player.mo;
	}
	
	// Some good old helpers:
	static bool IsValidItem (uint i) {
		// return i != Gcs_PlayerData.ignored_item;
		return true;
	}
	
	// Remember to check the result for null!
	class <Inventory> ItemClass (int shop = 0, int item = 0) const {
		let shop_inst = shops.At(shop);
		
		if(shop_inst) {
			return shop_inst.At(item);
		}
		
		return NULL;
	}
	
	int ItemCost (int shop = 0, int item = 0) const {
		let shop_inst = shops.At(shop);
		
		if(shop_inst) {
			return shop_inst.Price(item);
		}
		
		return 0;
	}
	
	// Speak through the console with the user. Use a language key. If you don't
	// then the message you typed will appear, but you should do it anyway.
	void ConsoleSpeak (String lang_key) const {
		if(customer.mo && consoleplayer == customer.mo.PlayerNumber()) {
			Console.printf(
				"%s: %s%s",
				StringTable.Localize(shopkeeper_name, false),
				color_key,
				StringTable.Localize(lang_key, false));
		}
	}
	
	// Same but with an index appended.
	void ConsoleSpeakIndex (String lang_key, int i = 0) const {
		ConsoleSpeak(lang_key .. i);
	}
	
	// Speak through the message box.
	const letters_per_second	= 1.0 / 35;
	const tics_per_beep			= 3.0;
	
	void BoxSpeak (
	String	lang_key,
	double	type_speed = 30 * letters_per_second) {
		if(shop && customer.mo && consoleplayer == customer.mo.PlayerNumber()) {
			shop.Talk(StringTable.Localize(lang_key, false), type_speed);
		}
	}
	
	// This one appends an index to the language key so you don't have to use
	// String.Format yourself. If you call BoxSpeakIndex("nithor_poor", 5),
	// you'll print the message from nithor_poor5.
	void BoxSpeakIndex (
	String	lang_key,
	int		i = 0,
	double	type_speed = 30 * letters_per_second) {
		BoxSpeak(lang_key .. i);
	}
	
	// Same but plays a voice with the same key.
	void BoxSpeakVoice (
	String	lang_key,
	int		i = 0,
	double	type_speed = 30 * letters_per_second) {
		if(shop && customer.mo && consoleplayer == customer.mo.PlayerNumber()) {
			let key = lang_key .. i;
			
			shop.Talk(StringTable.Localize(key, false), type_speed);
			A_PlaySound(key, CHAN_VOICE);
		}
	}
	
	// Same, but plays typewriter sounds instead of a voice.
	void BoxSpeakTypewriter (
	String	lang_key,
	int		i = 0,
	String	beep_sound	= "startuptick",
	String	end_sound	= "startuptick",
	double	type_speed	= 30 * letters_per_second,
	double	beep_speed	= 3 * tics_per_beep) {
		if(shop && customer.mo && consoleplayer == customer.mo.PlayerNumber()) {
			shop.StartTalkSound(beep_sound, ticks_per_sound: tics_per_beep);
			shop.TalkEndSound(end_sound);
			shop.Talk(StringTable.Localize(lang_key .. i, false), type_speed);
		}
	}
	
	// Events from the shop:
	virtual void OnRemove () { is_leaving = true; }
	virtual void OnBuy (int result, uint shop_i, uint itei) {}
	virtual void OnEquip (int result, uint shop_i, uint itei) {}
	// virtual void OnButtonClick (int button) {}
	// virtual void OnKeeperChangeIn () {}
	// virtual void OnKeeperChangeOut () {}
	// virtual void OnShopOpen (uint opened_i, bool entering) {}
	// virtual void OnShopClose (uint current_shop, uint pending_shop, bool leaving) {}
	// virtual void OnMenuOpen () {}
	// virtual void OnMenuClose () {}
	// virtual void OnAppear () {}
	virtual void OnItemHover (int shop_index, int item_index) {}
	virtual void OnItemDehover (int shop_index, int item_index) {}
	virtual void OnButtonHover (int button_index) {}
	virtual void OnButtonDehover (int button_index) {}
	virtual void OnAction (int button_index) {}
	virtual void OnLeaveRing () {}
	// virtual void OnShopDeath (Actor killer, int damage, Actor source,
		// Name damage_type, edmgflags flags, double angle) {}
	
	// virtual void OnItemSelect (uint shop_i, uint current, uint prev) {}
	
	/*	These happen when the player buys, equips, etc. and the return value
		determines whether they allow this to happen or not.
		
		It's a silly thing, but nobody said we were strict.
		
		You also must NOT call any "risky" play functions here, like setting a
		cvar or spawning monsters. It will cause a desync, because this code is
		called directly from a consoleplayer-branch. If you need to output a
		reaction, use the above "On"-event variations.
		
		Return value:
		false if you want to block the request, true if it's ok.*/
	/*virtual bool HookBuy (int shop_i, int itei) const {
		return true;
	}
	
	virtual bool HookEquip (int shop_i, int itei) const {
		return true;
	}
	
	virtual bool HookButtonClick (int button) const {
		return true;
	}*/
	
	bool TieTo (PlayerInfo user, Gcs_ShopList shops_, Gc_Shop shop_) {
		customer = user;
		user_mo  = user.mo;
		shops    = shops_;
		shop     = shop_;
		
		// The player can't look while doing shop things, so we do this
		// calculation just once now.
		
		let cosang       = cos(user_mo.angle);
		let sinang       = sin(user_mo.angle);
		let cosvang      = cos(user_mo.pitch);
		let sinvang      = sin(user_mo.pitch);
		view_offset      = view_distance * (
			cosvang * cosang,
			cosvang * sinang,
		-	sinvang);
		view_offset.z	+= z_offset;
		return true;
	}
	
	override void Tick () {
		if(user_mo == NULL) {
			super.Tick();
			return;
		}
		
		SetOrigin(user_mo.vec3offset(
			view_offset.x,
			view_offset.y,
			view_offset.z + customer.viewz - user_mo.pos.z), true);
		A_SetAngle(user_mo.angle + 180, SPF_INTERPOLATE);
		super.Tick();
	}
	
	override void OnDestroy () {
		if(shop && user_mo && consoleplayer == user_mo.PlayerNumber()) {
			shop.OnShopkeeperRemoved();
		}
	}
	
	// kd:
	// This allows making talking to the player through the shop's message box
	// a little bit easier.
	
	static String, String MsgAndVoice (String lang_key, uint index = 0) {
		String key = String.Format("%s%i", lang_key, index);
		return
			StringTable.Localize(String.Format("%s%i", lang_key, index), false),
			key;
	}
	
	// kd:
	// And this does the same, but outputs it to the shop's own message box.
	
	void OutRandom (String lang_key, uint low, uint high) {
		String msg, sound;
		[msg, sound] = MsgAndVoice(lang_key, random(low, high));
		
		if(consoleplayer == customer.mo.PlayerNumber()) {
			
			// shop.StartTalkSound("startuptick", ticks_per_sound: 3);
			// shop.TalkEndSound("weapons/rocklx");
			shop.Talk(msg);
		}
		
		A_PlaySound(sound, CHAN_VOICE);
	}
}