/*	kd:
	
	Displays a random title image permanently and then some. */

class Gc_TitlePic : EventHandler {
	const help_max_alpha	= 1.0;
	const help_base_alpha	= 0.7;
	const help_alpha_d		= help_max_alpha - help_base_alpha;
	const help_pulse_time	= 10;
	const help_pulse_wait	= 50;
	const help_pulse_f		= 180.0 / help_pulse_time;
	
	const advisor_max_alpha	= 0.666;
	const advisor_fade_tics	= 40;
	const advisor_fade_rate	= advisor_max_alpha / advisor_fade_tics;
	const advisor_time		= 300;
	const advisor_wait		= 200;
	
	const advisor_scale		= 1.2;
	const help_msg_scale	= 0.777;
	
	enum fade_state {
		fade_ready
	,	fade_in
	,	fade_out
	,	fade_stay // night lololololololol!!!!!!!!
	,	fade_done
	};
	
	private TextureId		title_pic;
	private TextureId		advisor_sprite;
	private TextureId		help_sprite;
	private int				screen_height;
	private String			help_msg;
	
	private int				image_width;
	private int				image_height;
	private float			image_aspect;
	
	private float			advisor_alpha;
	private float			advisor_old_alpha;
	private int				advisor_width;
	private int				advisor_height;
	private int				advisor_x;
	private int				advisor_y;
	private int				advisor_tics;
	private fade_state		advisor_state;
	
	private transient Font	help_font;
	private int				help_width;
	private int				help_height;
	private int				help_x;
	private int				help_y;
	private float			help_alpha;
	private float			help_old_alpha;
	private fade_state		help_state;
	private int				help_tics;
	private int				help_pulses;
	
	override void OnRegister () {
		static const String title_pic_lumps [] = {
			"title"
		,	"title2"
		,	"interpic"
		,	"dergpage"
		,	"bossback"
		};
		
		title_pic = TexMan.CheckForTexture(
			title_pic_lumps [random(0, title_pic_lumps.size() - 1)],
			TexMan.type_any);
		
		[image_width, image_height] = TexMan.GetSize(title_pic);
		image_aspect = 1.0 * image_width / image_height;
		
		// Load the advisor message (Lettuce Spine and Sauce):
		advisor_sprite	= TexMan.CheckForTexture("advisor", TexMan.type_any);
		[advisor_width, advisor_height] = TexMan.GetSize(advisor_sprite);
		
		advisor_state	= fade_ready;
		advisor_tics	= 0;
		advisor_x		= 2;
		advisor_y		= image_height - advisor_height - 10;
		
		// Get the help message:
		help_font		= smallfont;
		help_msg		= StringTable.Localize("$TITLEPIC_HELP");
		help_height		= help_font.GetHeight();
		help_width		= help_font.StringWidth(help_msg);
		
		// It's now a picture, but we keep the rest for no reason in particular.
		help_sprite		= TexMan.CheckForTexture("helpglow", TexMan.type_any);
		[help_width, help_height] = TexMan.GetSize(help_sprite);
		help_y			= image_height - help_height - 2;
		help_x			= image_width - help_width - 2;
		help_state		= fade_ready;
		help_tics		= 0;
		help_pulses		= 0;
	}
	
	override void WorldTick () {
		
		help_old_alpha = help_alpha;
		help_tics++;
		
		switch(help_state) {
		case fade_ready:
			help_alpha = help_base_alpha;
			
			if(help_pulse_wait <= help_tics) {
				help_tics = 0;
				help_state = fade_in;
			}
			
			break;
		
		case fade_in:
			help_alpha = help_base_alpha +
				sin(help_tics * help_pulse_f) * help_alpha_d;
			
			if(help_pulse_time <= help_tics) {
				help_tics = 0;
				help_pulses++;
				
				if(3 <= help_pulses) {
					help_state = fade_ready;
					help_pulses = 0;
				}
			}
			
			break;
		}
		
		// Show the advisor message for a while:
		advisor_old_alpha = advisor_alpha;
		advisor_tics++;
		
		switch(advisor_state) {
		case fade_in:
			advisor_alpha += advisor_fade_rate;
			
			if(advisor_fade_tics <= advisor_tics) {
				advisor_alpha = advisor_max_alpha;
				advisor_state = fade_stay;
			}
			
			break;
			
		case fade_out:
			advisor_alpha -= advisor_fade_rate;
			
			if(advisor_fade_tics <= advisor_tics) {
				advisor_alpha = 0;
				advisor_State = fade_done;
			}
			
			break;
			
		case fade_ready:
			if(advisor_wait <= advisor_tics) {
				advisor_tics = 0;
				advisor_state = fade_in;
			}
			
			break;
			
		case fade_stay:
			if(advisor_time <= advisor_tics) {
				advisor_tics = 0;
				advisor_state = fade_out;
			}
			
			break;
		
		case fade_done:
			advisor_tics = 0;
			advisor_alpha = 0;
			break;
		}
	}
	
	override void RenderOverlay (RenderEvent event) {
		float aspect		= Screen.GetAspectRatio();
		int virtual_width	= 0;
		int virtual_height	= 0;
		
		// Letterbox at top and bottom, because the image is too wide:
		if(aspect < image_aspect) {
			virtual_width	= image_width;
			virtual_height	= virtual_width / aspect;
		}
		
		// Letterbox at the left and right, because the image is too tall:
		else {
			virtual_height	= image_height;
			virtual_width	= virtual_height * aspect;
		}
		
		int draw_x			= 0.5 * virtual_width;
		int draw_y			= 0.5 * virtual_height;
		int left			= draw_x - 0.5 * image_width;
		int top				= draw_y - 0.5 * image_height;
		float t				= event.fractic;
		
		Screen.Clear(
			0, 0,
			Screen.GetWidth(), Screen.GetHeight(),
			Color(255, 7, 0, 10));
		Screen.DrawTexture(
			title_pic, true,
			draw_x, draw_y,
			DTA_CENTEROFFSET,	true,
			DTA_VIRTUALWIDTH,	virtual_width,
			DTA_VIRTUALHEIGHT,	virtual_height,
			DTA_KEEPRATIO,		true);
		
		// Advisor:
		Screen.DrawTexture(
			advisor_sprite, true,
			left + advisor_x, top + advisor_y,
			DTA_VIRTUALWIDTH,	virtual_width,
			DTA_VIRTUALHEIGHT,	virtual_height,
			// DTA_TOPOFFSET,		0,
			DTA_ALPHA,			t * advisor_alpha + (1 - t) * advisor_old_alpha,
			DTA_KEEPRATIO,		true);
		
		// Help message:
		Screen.DrawTexture(
			help_sprite, true,
			left + help_x, top + help_y,
			DTA_LEGACYRENDERSTYLE,	STYLE_ADD,
			DTA_VIRTUALWIDTH,		virtual_width,
			DTA_VIRTUALHEIGHT,		virtual_height,
			DTA_ALPHA,				t * help_alpha + (1 - t) * help_old_alpha,
			DTA_KEEPRATIO,			true);
		
		/* Screen.DrawText(
			help_font,
			Font.cr_white,
			left + help_x, top + help_y,
			help_msg,
			DTA_ALPHA,			help_alpha,
			DTA_VIRTUALWIDTH,	virtual_width,
			DTA_VIRTUALHEIGHT,	virtual_height,
			DTA_KEEPRATIO,		true); */
	}
}