/*	kd:
	
	This handles displaying a supermag on the screen and stuff like:
	
	1. Showing up
	2. Drawing the bars
	3. Whatever
	
	It's REALLY annoying to do repeatedly, believe me.
	
	*/

class Gc_SbarSupermags {
	// kd: This is really vintage, but gzd cannot have dynamic arrays of struct,
	// like I'd need for FadeTick, and I'm not goingg to make a wrapper class
	// that forces me to do something like wrapper.tick.DoSomething().
	const supermag_max	= 8;
	const fade_tics		= 19;
	
	// kd: Base data:
	private String						bar_sprite_name		[supermag_max];
	private String						bg_sprite_name		[supermag_max];
	private class <Inventory>			left_counter_inv	[supermag_max];
	private class <Inventory>			right_counter_inv	[supermag_max];
	private class <Weapon>				weapon_inv			[supermag_max];
	private class <Inventory>			supermag_inv		[supermag_max];
	private Gc_WeapObserver				mag_weap			[supermag_max];
	private Gc_InvObserver				mag_inv				[supermag_max];
	
	private int							count;
	
	// Not private cuz gzd cannot return struct.
	private Gc_InvObserver				gunzerk_inv;
	FadeTick							left_t, right_t;
	protected Gc_SbarInvMeter			left_bar, right_bar;
	
	private vector2						draw_offset;
	
	private bool						is_right_shown;
	private bool						old_is_right_shown;
	private bool						is_left_shown;
	private bool						old_is_left_shown;
	private bool						is_active;
	private int							current;
	
	void Reset () {
		current = 0;
		is_active = false;
		right_t.Force(0);
		left_t.Force(0);
		draw_offset = (100, 52);
		gunzerk_inv.Observe("PowerGunzerking");
	}
	
	ui void Init () {
		left_bar	= left_bar.Create("tnt1a0");
		right_bar	= right_bar.Create("tnt1a0");
		
		left_bar.SetFillTics(5);
		right_bar.SetFillTics(5);
		
		left_bar.SetScreenAlignment(
			hor: Gc_SbarObject.left,
			ver: Gc_SbarObject.bottom);
		right_bar.SetScreenAlignment(
			hor: Gc_SbarObject.right,
			ver: Gc_SbarObject.bottom);
		
		left_bar.SetSelfAlignment(Gc_SbarObject.center, Gc_SbarObject.top);
		right_bar.SetSelfAlignment(Gc_SbarObject.center, Gc_SbarObject.top);
	}
	
	play void Add (
	class <Inventory>	supermag_class,
	class <Weapon>		supermag_weap,
	class <Inventory>	right_counter,
	class <Inventory>	left_counter,
	String				filled_sprite_name,
	String				empty_sprite_name) {
		if(count < supermag_max) {
			let i = count;
			bar_sprite_name		[i] = filled_sprite_name;
			bg_sprite_name		[i] = empty_sprite_name;
			left_counter_inv	[i] = left_counter;
			right_counter_inv	[i]	= right_counter;
			weapon_inv			[i]	= supermag_weap;
			supermag_inv		[i] = supermag_class;
			mag_weap			[i].Observe(supermag_weap);
			mag_inv				[i].Observe(supermag_class);
			count++;
		}
	}
	
	ui void Tick (Gc_StatusBar bar, PlayerInfo player) {
		// kd: The tricky thing is that many things can cause the bars to show
		// up. It might be that you have a supermag and then select the weapon,
		// or you have the weapon and then pick up a supermag. Either way, there
		// should be an animation. Add on top of that: Gunslinger can go off.
		// Phantasmagorical. But I have secret of win.
		
		gunzerk_inv.Next(player.mo);
		
		// Fade supermags in and out:
		bool is_done = false;
		
		for(int i = 0; i < count && !is_done; i++) {
			
			// kd:
			// This will look evil, but it boils down to this gag. Instead of
			// listing supermag "show up" and "go away" conditions, we just
			// observe "when should it be seen?" And if the state changes from
			// "it shouldn't be visible" to "it should be visible", that's the
			// same as "show it up".
			
			mag_weap [i].Next(player);
			mag_inv [i].Next(player.mo);
			
			if(mag_weap [i].Holding() || mag_weap [i].Deselected()) {
				old_is_right_shown	= is_right_shown;
				is_right_shown		=
					mag_weap	[i].Holding() &&
					mag_inv		[i].Has();
				
				old_is_left_shown	= is_left_shown;
				is_left_shown		=
					is_right_shown &&
					(gc_gunslinger || gunzerk_inv.Has());
			}
			
			else {
				continue;
			}
			
			bool fade_right_in	= is_right_shown	&& !old_is_right_shown;
			bool fade_left_in	= is_left_shown		&& !old_is_left_shown;
			bool fade_right_out	= old_is_right_shown&& !is_right_shown;
			bool fade_left_out	= old_is_left_shown	&& !is_left_shown;
			
			if(fade_right_in) {
				right_t.ContinueTo(fade_tics);
				is_done = true;
				current = i;
			}
			
			else if(fade_right_out) {
				right_t.ContinueTo(0);
				is_done = true;
			}
			
			if(fade_left_in) {
				left_t.ContinueTo(fade_tics);
				is_done = true;
			}
			
			else if(fade_left_out) {
				left_t.ContinueTo(0);
				is_done = true;
			}
			
			if(is_done) {
				LoadSupermagIndexSprites(i);
			}
		}
		
		right_t.Next();
		left_t.Next();
		
		if(right_bar) {
			right_bar.InversedNext(right_counter_inv [current], player.mo);
		}
		
		if(left_bar) {
			left_bar.InversedNext(left_counter_inv [current], player.mo);
		}
		// right_bar.ToInventoryRev(bar, right_counter_inv [current], speed: 1);
		// left_bar. ToInventoryRev(bar, left_counter_inv  [current], speed: 1);
	}
	
	ui void Draw (Gc_StatusBar bar, double t) {
		double right_f	= right_t.RenderStandardDelta(fade_tics, t);
		double left_f	= left_t.RenderStandardDelta(fade_tics, t);
		
		// kd: Make it snap in:
		right_f	= Gc_PulseFunc.over_pulse2f(right_f);
		left_f	= Gc_PulseFunc.over_pulse2f(left_f);
		
		double right_y	= draw_offset.y * right_f;
		double left_y	= draw_offset.y * left_f;
		
		if(left_bar) {
			bar.DrawBarHor(left_bar, draw_offset.x, -left_y, t: t);
		}
		
		if(right_bar) {
			bar.DrawBarHorRev(right_bar, -draw_offset.x, -right_y, t: t);
		}
		
		bar.RestoreClipRectFromCache();
	}
	
	ui private void LoadSupermagIndexSprites (int i) {
		if(0 <= i && i < supermag_max) {
			left_bar.	LoadSprite(bar_sprite_name [i], bg_sprite_name [i]);
			right_bar.	LoadSprite(bar_sprite_name [i], bg_sprite_name [i]);
			left_bar.	CacheOffsetToTopLeft();
			right_bar.	CacheOffsetToTopLeft();
		}
	}
}