/*	kd:
	
	This deals with the screen text and stuff.
	
	*/

struct Gc_Shop_Messages {
	
	// kd: Text related shenanigans. Description is for the item, title is the
	// item's full title and comment is the shopkeeper's banter.
	private Gc_RealCounter				item_counter;
	private String						item_title_string;
	
	private Gc_RealCounter				description_counter;
	private Gc_WrapMessage				description_message;
	private Gc_ScrollMessage			description_scroll;
	
	private Gc_RealCounter				talk_counter;
	private Gc_WrapMessage				talk_message;
	
	private Gc_RealCounter				comment_counter;
	private Gc_WrapMessage				comment_message;
	
	private FadeTick					item_color_t;
	private FadeTick					scroll_t;
	private FadeTick					tip_t;
	
	private int							description_lines;
	private int							description_shown_lines;
	
	private vector2						tip_pos;
	private vector2						text_pos;
	private vector2						talk_pos;
	
	private int							current_page;
	private int							page_count;
	private Gc_ColorString				page_str;
	
	private bool						show_page_counter;
	private bool						show_description;
	private bool						show_tip;
	private bool						show_talk;
	
	private int							talk_start_color;
	private int							talk_end_color;
	
	private String						comment_key;
	
	// kd: Text resolution may vary. The message box is a bit of a lulzy one.
	// Find the text positions at startup.
	
	void Init (
	Gc_Shop_UiHandling		ui_handling,
	Gc_Shop_ButtonDrawing	button_anim,
	vector2					text_resolution,
	vector2					sprite_resolution) {
		
		// kd: A bit of a lulzy one.
		page_str.BuildLocalize(
			"page",
			smallfont,
			Font.CR_ICE,
			offset_f: 1.0);
		
		// kd: There's no message shown by default, but the scroll down button
		// needs it, so... we'll place it at the lowest position.
		description_lines	= 5;
		talk_line			= 0;
		comment_key			= "nithor";
		show_description	= false;
		show_page_counter	= false;
		show_tip			= false;
		show_talk			= false;
		
		// kd: Good thing everything is so nicely organised now.
		let message_box_pos		= button_anim.move_anim.FinalPos(ui_handling.message_box);
		let message_box_size	= button_anim.sprites.Size(ui_handling.message_box);
		let top_left			= (
			message_box_pos.x - 0.5 * message_box_size.x,
			message_box_pos.y - 0.5 * message_box_size.y);
		
		tip_pos = (
			top_left.x * text_resolution.x / sprite_resolution.x + 8,
			top_left.y * text_resolution.y / sprite_resolution.y + 8);
		
		text_pos = tip_pos + (130, 0);
		talk_pos = text_pos + (description_lines + 1) * (0, 9);
	}
	
	// kd: Different shopkeepers might wanna have different item descriptions.
	// If one didn't spawn, we'll use Nithor's. They're kiiiiinda neutral.
	
	// Also, not all shopkeepers can talk! If they don't, we'll use all 9 lines
	// for the item description.
	private int						talk_color_in;
	private int						talk_color_final;
	
	void SetCommentary (
	String	key			= "nithor",
	String	name_key	= "shopkeeper_nithor",
	int		color_in	= Font.CR_WHITE,
	int		color_final	= Font.CR_ICE,
	int		lines_shown	= 9) {
		talk_color_in		= color_in;
		talk_color_final	= color_final;
		comment_key			= key;
		description_lines	= lines_shown;
		talk_pos			= text_pos + (description_lines + 1) * (0, 9);
		talk_name			= StringTable.Localize(name_key, false);
	}
	
	vector2 TextPos () const {
		return text_pos;
	}
	
	int DescriptionLines () const {
		return description_lines;
	}
	
	void DisplayItem (Gc_Shop_Inv inv, int item) {
		if(inv) {
			
			// kd: Setup item title.
			item_title_string = inv.ItemTitle(item);
			item_counter.To(item_title_string.Length(), step_: 1.3);
			item_color_t.Force(0);
			
			// kd: Setup item description.
			let description_string = inv.ItemDescription(item, comment_key);
			description_message.Build(description_string, smallfont, wrap_width: 380);
			description_counter.To(133769, step_: 4.1337);
			
			description_shown_lines = min(
				description_lines,
				description_message.Lines());
		
			// kd: Let it scroll, too.
			description_scroll.Build(description_lines, description_message);
			
			// kd: And reset some scroll info here and there.
			current_page	= 1;
			page_count		= 1 + (description_message.Lines() - 1) / description_lines;
			
			// kd: Page counter is now interesting.
			show_page_counter	= true;
			show_description	= true;
		}
	}
	
	private Gc_Shop_Tip				item_tip;
	
	void DisplayTip (PlayerInfo player, Gc_Shop_Inv inv, int item) {
		if(inv && player.mo) {
			if(item_tip) {
				item_tip.Destroy();
			}
			
			item_tip = Gc_Shop_Tip(new(inv.Tip(item)));
			
			if(item_tip) {
				item_tip.Fill(player.mo, inv, item);
			}
			
			show_tip			= true;
		}
	}
	
	// kd: Buying and equipping changes your inventory state one tic later due
	// to network events being updated on the next frame. This handles that.
	const tip_delay = 2;
	
	private PlayerInfo				delayed_player_info;
	private Gc_Shop_Inv				delayed_inv;
	private int						delayed_item;
	
	void DelayedDisplayTip (PlayerInfo player, Gc_Shop_Inv inv, int item) {
		tip_t.To(tip_delay);
		
		delayed_player_info	= player;
		delayed_inv			= inv;
		delayed_item		= item;
	}
	
	private int						talk_line;
	private bool					talk_has_lines_left;
	private String					talk_typing_str;
	private String					talk_name;
	
	void DisplayTalk (String lang_key, double step = 2.369) {
		let talk_str = StringTable.Localize(lang_key, true);
		
		talk_message.Build(String.Format(
			// "%s\c-: %s",
			"%s",
			// talk_name,
			talk_str),
			smallfont,
			wrap_width: 380);
		
		talk_counter.To(133769, step_: step);
		talk_line = 0;
		show_talk = talk_message.HasText();
		
		LoadNextTalkLine();
	}
	
	void NextState () {
		talk_counter.Next();
		item_counter.Next();
		description_counter.Next();
		description_scroll.NextState(description_message);
		comment_counter.Next();
		
		if(item_counter.IsDone()) {
			item_color_t.ContinueTo(20);
			item_color_t.Next();
		}
		
		if(talk_counter.IsDone() && talk_has_lines_left) {
			LoadNextTalkLine();
		}
		
		tip_t.Next();
		
		if(0 < tip_t.Now() && tip_t.IsDone()) {
			DisplayTip(delayed_player_info, delayed_inv, delayed_item);
			tip_t.Force(0);
		}
	}
	
	private void LoadNextTalkLine () {
		let total_lines = talk_message.Lines();
		
		if(total_lines <= talk_line) {
			talk_line = total_lines;
			talk_has_lines_left = false;
		}
		
		else {
			talk_typing_str = talk_message.text.StringAt(talk_line);
			talk_has_lines_left = true;
			talk_line++;
			
			talk_counter.BackTo(talk_typing_str.Length());
		}
	}
	
	void ClearItemDescription () {
		show_page_counter	= false;
		show_description	= false;
		show_tip			= false;
		current_page		= 1;
		page_count			= 1;
	}
	
	void ClearTalk () {
		show_talk			= false;
		talk_line			= 0;
		talk_has_lines_left	= false;
	}
	
	void ScrollDescriptionDown () {
		
		// kd: You could scroll up and down repeatedly to mess up the page
		// counter significantly. It's like secret worlds but for text.
		if(description_scroll.IsWorking()) {
			return;
		}
		
		let lines = description_lines;
		
		if(1 < page_count) {
			
			// kd: We want to scroll only so far that the rest of the message is
			// put on the screen. After that it will loop.
			let scroll_index	= description_scroll.ScrollIndex();
			let scroll_lines	= description_message.Lines();
			let remaining		= scroll_lines - scroll_index - description_lines;
			
			if(lines <= remaining || remaining <= 0) {
				description_scroll.ScrollDown(lines, tics: 4);
				scroll_index += lines;
			}
			
			else {
				description_scroll.ScrollDown(remaining, tics: 4);
				scroll_index += remaining;
			}
			
			// kd: In the end, this was stupidly simple.
			current_page++;
			
			if(page_count < current_page) {
				current_page = 1;
			}
		}
	}
	
	void ScrollDescriptionUp () {
		
		// kd: You could scroll up and down repeatedly to mess up the page
		// counter significantly. It's like secret worlds but for text.
		if(description_scroll.IsWorking()) {
			return;
		}
		
		let lines = description_lines;
		
		if(1 < page_count) {
			
			// kd: Same deal, but ba-ba-ba-ba-ba-BACKWAAAARDS.
			let scroll_index	= description_scroll.ScrollIndex();
			
			if(lines <= scroll_index || scroll_index == 0) {
				description_scroll.ScrollUp(lines, tics: 4);
			}
			
			else {
				description_scroll.ScrollUp(scroll_index, tics: 4);
			}
			
			// kd: Always care about wrap around.
			current_page--;
			
			if(current_page <= 0) {
				current_page = page_count;
			}
		}
	}
	
	ui void Draw (vector2 resolution, double t = 0) const {
		
		// kd: Draw the tip.
		if(item_tip && show_tip) {
			item_tip.Draw(resolution, tip_pos, t);
		}
		
		// kd: Show our page count and stuff.
		
		if(show_page_counter) {
			Screen.DrawText(
				smallfont,
				page_str.text_color,
				text_pos.x + 360 + page_str.offset,
				text_pos.y,
				page_str.text,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_KEEPRATIO,			true);
				
			Screen.DrawText(
				smallfont,
				Font.CR_WHITE,
				text_pos.x + 368,
				text_pos.y,
				String.Format("%i", current_page),
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_KEEPRATIO,			true);
				
			Screen.DrawText(
				smallfont,
				page_str.text_color,
				text_pos.x + 376,
				text_pos.y,
				"/",
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_KEEPRATIO,			true);
				
			Screen.DrawText(
				smallfont,
				Font.CR_WHITE,
				text_pos.x + 384,
				text_pos.y,
				String.Format("%i", page_count),
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_KEEPRATIO,			true);
		}
		
		// kd: Messages...
		
		let item_tick		= item_counter.			WholeAmount(t);
		let comment_tick	= comment_counter.		WholeAmount(t);
		let description_tick= description_counter.	WholeAmount(t);
		let talk_tick		= talk_counter.			WholeAmount(t);
		
		let color_alpha = item_color_t.RenderStandardDelta(20, t);
		
		if(show_description) {
			
			// kd: Shade the item title.
			Screen.DrawText(
				smallfont,
				Font.CR_WHITE,
				text_pos.x,
				text_pos.y,
				item_title_string,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				0.2337,
				DTA_KEEPRATIO,			true);
			
			// kd: Then draw it.
			Screen.DrawText(
				smallfont,
				Font.CR_SAPPHIRE,
				text_pos.x,
				text_pos.y,
				item_title_string,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				1.0,
				DTA_KEEPRATIO,			true,
				DTA_TEXTLEN,			item_tick);
			
			// kd: Fade thingy on it.
			Screen.DrawText(
				smallfont,
				Font.CR_WHITE,
				text_pos.x,
				text_pos.y,
				item_title_string,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				1.0 - color_alpha,
				DTA_KEEPRATIO,			true,
				DTA_TEXTLEN,			item_tick);
			
			// kd: Draw the item description. This thing can scroll, but it won't
			// make things more complicated.
			let scroll_f	= description_scroll.scroll_t.RenderStandardDelta(4, t);
			let scroll_dir	= description_scroll.IsScrollingDown() ? -1 : 1;
			let scroll_offs	= description_scroll.IsWorking() ? 1 : 0;
			let y			= text_pos.y + 12 + 8 * scroll_dir * (scroll_f - scroll_offs);
			
			for(let i = 0; i < description_shown_lines; i++) {
				
				// kd: Kinda cool-ish shading effect.
				Screen.DrawText(
					smallfont,
					Font.CR_WHITE,
					text_pos.x,
					y,
					description_scroll.text [i],
					DTA_VIRTUALWIDTHF,		resolution.x,
					DTA_VIRTUALHEIGHTF,		resolution.y,
					DTA_ALPHA,				0.23,
					DTA_KEEPRATIO,			true);
				
				Screen.DrawText(
					smallfont,
					Font.CR_ICE,
					text_pos.x,
					y,
					description_scroll.text [i],
					DTA_VIRTUALWIDTHF,		resolution.x,
					DTA_VIRTUALHEIGHTF,		resolution.y,
					DTA_ALPHA,				1.0,
					DTA_KEEPRATIO,			true,
					DTA_TEXTLEN,			description_tick);
					
				let line_alpha = 1.0 * i / (description_lines - 1);
				
				Screen.DrawText(
					smallfont,
					Font.CR_WHITE,
					text_pos.x,
					y,
					description_scroll.text [i],
					DTA_VIRTUALWIDTHF,		resolution.x,
					DTA_VIRTUALHEIGHTF,		resolution.y,
					DTA_ALPHA,				line_alpha,
					DTA_KEEPRATIO,			true,
					DTA_TEXTLEN,			description_tick);
				
				// kd: Make some cool subtle fade thingy.
				Screen.DrawText(
					smallfont,
					Font.CR_WHITE,
					text_pos.x,
					y,
					description_scroll.text [i],
					DTA_VIRTUALWIDTHF,		resolution.x,
					DTA_VIRTUALHEIGHTF,		resolution.y,
					DTA_ALPHA,				1.0 - color_alpha,
					DTA_KEEPRATIO,			true,
					DTA_TEXTLEN,			description_tick);
				
				y += 8;
			}
		}
		
		// kd: The shopkeeper's message! If any...
		if(show_talk) {
			let y = talk_pos.y;
			
			if(talk_message.HasText())
			for(let i = 0; i < talk_line - 1; i++) {
				Screen.DrawText(
					smallfont,
					talk_color_final,
					talk_pos.x,
					y,
					talk_message.text.StringAt(i),
					DTA_VIRTUALWIDTHF,		resolution.x,
					DTA_VIRTUALHEIGHTF,		resolution.y,
					DTA_ALPHA,				1.0,
					DTA_KEEPRATIO,			true);
				
				y += 8;
			}
			
			Screen.DrawText(
				smallfont,
				talk_color_final,
				talk_pos.x,
				y,
				talk_typing_str,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				1.0,
				DTA_KEEPRATIO,			true,
				DTA_TEXTLEN,			talk_tick);
			
			Screen.DrawText(
				smallfont,
				talk_color_in,
				talk_pos.x,
				y,
				talk_typing_str,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				1.0 - talk_counter.RealF(t),
				DTA_KEEPRATIO,			true,
				DTA_TEXTLEN,			talk_tick);
		}
	}
}