/*	kd:
	
	This is the little blurb that tells you how much of a certain item you got
	and other good info.
	
	*/

/*	kd:
	
	I HATE JUGGLING COLORS AND OFFSETS AAAAAAAAAAAAAAAAAA
	
	*/

struct Gc_ColorString {
	
	// kd: All deliberately not private.
	String							text;
	int								offset;
	int								text_color;
	
	void Build (
	String	text_,
	Font	ffont,
	int		color_		= Font.CR_UNTRANSLATED,
	double	offset_f	= 0) {
		text			= text_;
		text_color		= color_;
		offset			= -int(offset_f * ffont.StringWidth(text_));
	}
	
	void BuildLocalize (
	String	lang_key,
	Font	ffont,
	int		color_		= Font.CR_UNTRANSLATED,
	double	offset_f	= 0) {
		Build(
			StringTable.Localize(lang_key, false),
			ffont,
			color_,
			offset_f);
	}
}

class Gc_Shop_Tip {
	
	// kd: Info about the player
	protected int					price;
	protected int					afford_count;
	protected int					give_amount;
	protected int					amount;
	protected int					max_amount;
	protected int					dosh;
	
	// kd: Some more boring draw data.
	protected Gc_ColorString		type_str;
	
	protected Gc_ColorString		amount_slash_str;
	protected Gc_ColorString		amount_str;
	protected Gc_ColorString		max_amount_str;
	protected Gc_ColorString		got_it_str;
	
	protected Gc_ColorString		dosh_str;
	protected Gc_ColorString		price_str;
	protected Gc_ColorString		dosh_num_str;
	protected Gc_ColorString		price_num_str;
	protected Gc_ColorString		afford_str;
	
	static const int color_stat [] = {
		Font.CR_RED,
		Font.CR_ORANGE,
		Font.CR_YELLOW,
		Font.CR_GREEN
	};
	
	virtual void Fill (Actor user, Gc_Shop_Inv shop, uint item) {
		if(!shop) {
			return;
		}
		
		// kd: Read some boring shop stuff.
		let type		= shop.Handler(item);
		let inv_class	= shop.InvClass(item);
		
		if(type) {
			price			= shop.Price(item);
			give_amount		= shop.GiveAmount(item);
			afford_count	= shop.CanBuyXtimes(user, item);
			amount			= type.Count(user, inv_class);
			max_amount		= type.MaxInv(user, inv_class);
			dosh			= user.CountInv(shop.currency);
		}
		
		let afford_color	= color_stat [3 - clamp(afford_count, 0, 3)];
		let amount_color	= color_stat [int(clamp(4.0 * amount / max_amount, 0, 3))];
		
		// kd: Now let's fill in some strings. These all pack up the text with
		// their offset and colour.
		let ffont = smallfont;
		
		afford_str.Build(
			String.Format("%i", afford_count),
			ffont,
			afford_color,
			offset_f: 1.0);
		
		amount_str.Build(
			String.Format("%i", amount),
			ffont,
			amount_color,
			offset_f: 1.0);
		
		max_amount_str.Build(
			String.Format("%i", max_amount),
			ffont,
			amount_color,
			offset_f: 0);
		
		got_it_str.BuildLocalize(
			0 < amount ? "got_it" : "got_it_not",
			ffont,
			0 < amount ? Font.CR_WHITE : Font.CR_YELLOW,
			offset_f: 0.5);
		
		type_str.Build(
			type.TypeString(),
			ffont,
			Font.CR_FIRE,
			offset_f: 0.5);
		
		dosh_str.BuildLocalize(
			"dosh",
			ffont,
			Font.CR_WHITE,
			offset_f: 0);
		
		price_str.BuildLocalize(
			"price",
			ffont,
			Font.CR_WHITE,
			offset_f: 0);
		
		dosh_num_str.Build(
			String.Format("%i\c[yellow]G", dosh),
			ffont,
			Font.CR_ICE,
			offset_f: 0);
		
		price_num_str.Build(
			String.Format("%i\c[yellow]G", price),
			ffont,
			Font.CR_ICE,
			offset_f: 0);
		
		amount_slash_str.Build(
			"/",
			ffont,
			Font.CR_WHITE,
			offset_f: 0);
	}
	
	void DrawText (vector2 resolution, Gc_ColorString str, vector2 pos) const {
		Screen.DrawText(
			smallfont,
			str.text_color,
			pos.x + str.offset,
			pos.y,
			str.text,
			DTA_KEEPRATIO,		true,
			DTA_VIRTUALWIDTHF,	resolution.x,
			DTA_VIRTUALHEIGHTF,	resolution.y);
	}
	
	virtual void UpdateRealInfo (class <Inventory> inv) {}
	virtual void Draw (vector2 resolution, vector2 pos, double t = 1) const {
		
		// kd: Oh look, zs can look nice, too.
		DrawText(resolution, type_str,				pos + (65,	0));
		DrawText(resolution, dosh_str,				pos + (12,	8));
		DrawText(resolution, price_str,				pos + (82,	8));
		DrawText(resolution, dosh_num_str,			pos + (12,	16));
		DrawText(resolution, price_num_str,			pos + (82,	16));
		
		if(1 < max_amount) {
			DrawText(resolution, amount_str,		pos + (48,	24));
			DrawText(resolution, amount_slash_str,	pos + (56,	24));
			DrawText(resolution, max_amount_str,	pos + (70,	24));
		}
		
		else {
			DrawText(resolution, got_it_str,		pos + (65,	24));
		}
	}
}

class Gcs_WeaponTip : Gcs_SpellTip {
	protected Gc_ColorString		slot_str;
	protected Gc_ColorString		slot_num_str;
	
	override void Fill (Actor user, Gc_Shop_Inv shop, uint item) {
		super.Fill(user, shop, item);
		
		let type = shop.Handler(item);
		
		if(type) {
			let inv_class	= (class <Weapon>)(shop.InvClass(item));
			let def_type	= GetDefaultByType(inv_class);
			
			if(def_type) {
				slot_str.BuildLocalize(
					"weapon_slot",
					smallfont,
					Font.CR_WHITE,
					offset_f: 0);
				
				slot_num_str.Build(
					String.Format("%i", def_type.slotnumber),
					smallfont,
					Font.CR_ICE,
					offset_f: 0);
			}
		}
		
		selected_str.BuildLocalize(
			is_selected ? "weapon_on" : "weapon_off",
			smallfont,
			is_selected ? Font.CR_FIRE : Font.CR_WHITE,
			offset_f: 0.5);
	}
	
	override void Draw (vector2 resolution, vector2 pos, double t) const {
		super.Draw(resolution, pos, t);
		DrawText(resolution, slot_str,		pos + (0, 48));
		DrawText(resolution, slot_num_str,	pos + (32, 48));
	}
}

class Gcs_SpellTip : Gc_Shop_Tip {
	protected Gc_ColorString		selected_str;
	protected bool					is_selected;
	
	override void Fill (Actor user, Gc_Shop_Inv shop, uint item) {
		super.Fill(user, shop, item);
		
		let type		= shop.Handler(item);
		let inv_class	= shop.InvClass(item);
		
		if(type && inv_class) {
			is_selected = type.IsEquipped(user, inv_class);
			
			selected_str.BuildLocalize(
				is_selected ? "spell_on" : "spell_off",
				smallfont,
				is_selected ? Font.CR_FIRE : Font.CR_WHITE,
				offset_f: 0.5);
		}
	}
	
	override void Draw (vector2 resolution, vector2 pos, double t) const {
		super.Draw(resolution, pos, t);
		DrawText(resolution, selected_str, pos + (65, 32));
	}
}

class Gcs_TogglerTip : Gcs_SpellTip {
	override void Fill (Actor user, Gc_Shop_Inv shop, uint item) {
		super.Fill(user, shop, item);
		
		selected_str.BuildLocalize(
			is_selected ? "toggler_on" : "toggler_off",
			smallfont,
			is_selected ? Font.CR_FIRE : Font.CR_WHITE,
			offset_f: 0.5);
	}
}

class Gcs_InventoryTip : Gc_Shop_Tip {}

class Gcs_AmmoTip : Gc_Shop_Tip {}

class Gcs_PowerupTip : Gc_Shop_Tip {}

class Gcs_ArmorTip : Gc_Shop_Tip {
	protected Gc_ColorString		def_str;
	protected Gc_ColorString		your_def_str;
	protected Gc_ColorString		save_pc_str;
	protected Gc_ColorString		your_save_pc_str;
	
	override void Fill (Actor user, Gc_Shop_Inv shop, uint item) {
		Super.Fill(user, shop, item);
		
		Gcs_ItemType       type      = shop.Handler(item);
		class <Inventory>  inv_class = shop.InvClass(item);
		
		if(!inv_class || !type) {
			return;
		}
		
		let armor			= GetDefaultByType((class <BasicArmorPickup>)(inv_class));
		let player_armor	= BasicArmor(user.FindInventory("BasicArmor"));
		
		if(armor && player_armor) {
			def_str.BuildLocalize(
				"armor_def",
				smallfont,
				Font.CR_WHITE,
				offset_f: 0);
			
			your_def_str.BuildLocalize(
				"armor_your_def",
				smallfont,
				Font.CR_WHITE,
				offset_f: 0);
			
			save_pc_str.Build(
				String.Format("%01.0f\c[yellow]%%", armor.savepercent),
				smallfont,
				Font.CR_ICE,
				offset_f: 0);
			
			your_save_pc_str.Build(
				String.Format("%01.0f\c[yellow]%%", player_armor.savepercent * 100),
				smallfont,
				Font.CR_ICE,
				offset_f: 0);
		}
	}
	
	override void Draw (vector2 resolution, vector2 pos, double t) const {
		super.Draw(resolution, pos, t);
		DrawText(resolution, def_str,			pos + (10, 40));
		DrawText(resolution, your_def_str,		pos + (70, 40));
		DrawText(resolution, save_pc_str,		pos + (10, 48));
		DrawText(resolution, your_save_pc_str,	pos + (70, 48));
	}
}
