/*	kd:
	
	Here's the ring. This deals with all the positioning, drawing and selecting.
	
	
	The mouse cursor exists in your screen space. That's a smartass way to say
	that it has coordinates within your screen resolution. Meanwhile, the shop
	icons and buttons use a resolution in 0 to 1.
	
	This is a problem the ring must deal with, because if you just ignorantly
	throw sin and cos on it you'll get wider ovals the wider your resolution is.
	
	*/

struct Gc_Shop_InvLayout {
	
	// const origin_x				= 0.5;
	// const origin_y				= 0.405;
	// const radius_inner			= 0.235;
	// const radius_outer			= 0.421;
	
	// kd: Visual positioning (sprite resolution).
	private vector2				origin;
	private double				radius_inner;
	private double				radius_outer;
	
	// kd: This is what we measure cursor info in. Shoutouts to Stan.
	// (Should be window resolution.)
	private vector2				cursor_origin;
	private double				cursor_radius_inner2;
	private double				cursor_radius_outer2;
	
	// kd: How many items are there? This is shared between a lot of things.
	private uint				count;
	
	void Reposition (
	vector2	normal_origin	= (0.5, 0.405),
	double	inner_rad		= 0.235,
	double	outer_rad		= 0.421,
	double	draw_rad		= 0.30666,
	double	draw_offset		= 0.035) {
		origin				= normal_origin;
		radius_inner		= inner_rad;
		radius_outer		= outer_rad;
		draw_radius			= draw_rad;
		draw_radius_offset	= draw_offset;
	}
	
	// kd: We only have to store the cursor business once. As a rule of thumb,
	// the more you can offset to an init function like this, the nicer all the
	// important functions will look, and they'll be easier to maintain, because
	// there's guarantee correct values already exist.
	
	// Also, if any of my variable names end with _2, it means "divided by 2",
	// but just 2 means "squared". So be careful there.
	void InitForCursor (vector2 cursor_resolution) {
		cursor_origin = (
			origin.x * cursor_resolution.x,
			origin.y * cursor_resolution.y);
		
		let cursor_radius_inner	= cursor_resolution.y * radius_inner;
		let cursor_radius_outer	= cursor_resolution.y * radius_outer;
		
		cursor_radius_inner2 = cursor_radius_inner * cursor_radius_inner;
		cursor_radius_outer2 = cursor_radius_outer * cursor_radius_outer;
	}
	
	bool IsCursorInside (vector2 cursor_pos) const {
		let diff = cursor_pos - cursor_origin;
		let len2 = diff dot diff;
		return cursor_radius_inner2 <= len2 && len2 <= cursor_radius_outer2;
	}
	
	// kd: This gets the item number. Items start at 6 o' clock and then go
	// clockwise which is kinda weird but that's how it always was.
	uint IndexAtCursor (vector2 cursor_pos) const {
		if(count < 1) {
			return 0;
		}
		
		let diff		= cursor_origin - cursor_pos;
		let ang_2		= 180.0 / max(1, count);
		let ang_select	= (VectorAngle(diff.y, diff.x) + ang_2) % 360.0;
		return clamp(count * ang_select / 360, 0, count - 1);
	}
	
	// kd: This is what we use for drawing. Juggling resolutions and scales can
	// be annoying like that.
	const resolution_y						= 720;
	private double draw_radius;
	private double draw_radius_offset;
	
	// kd: No longer private. This struct was a mistake tbh.
	RenderScreen					rscreen;
	private vector2					screen_origin;
	private double					screen_radius;
	private double					screen_highlight_offset;
	
	// kd: The more icons there are, the smaller they should be drawn, but they
	// should never get too small to see, either.
	const max_scale_count			= 6;
	const min_scale_count			= 20;
	const scale_delta				= min_scale_count - max_scale_count;
	const max_button_scale			= 1.0;
	const min_button_scale			= 0.666;
	
	void Rescale (double scale = 1) {
		rscreen.ScaleToHeight(resolution_y * scale);
		screen_origin = rscreen.NormalToScaledVec((origin.x, origin.y));
		screen_radius = rscreen.NormalToScaledY(draw_radius);
		screen_highlight_offset = rscreen.NormalToScaledY(draw_radius_offset);
	}
	
	// kd: Now for the animation part.
	private Array <double>			pos_x;
	private Array <double>			pos_y;
	private Array <double>			old_pos_x;
	private Array <double>			old_pos_y;
	private Array <double>			draw_x;
	private Array <double>			draw_y;
	private Array <double>			alpha_t;
	
	// kd: You might want to move the buttons themselves a little. We use this
	// to make buttons shake like "no" when you do something that doesn't work.
	private Array <double>				extra_pos_x;
	private Array <double>				extra_pos_y;
	private Array <double>				old_extra_pos_x;
	private Array <double>				old_extra_pos_y;
	
	void ClearAndResize (uint size) {
		pos_x.				clear();
		pos_y.				clear();
		old_pos_x.			clear();
		old_pos_y.			clear();
		extra_pos_x.		clear();
		extra_pos_y.		clear();
		old_extra_pos_x.	clear();
		old_extra_pos_y.	clear();
		draw_x.				clear();
		draw_y.				clear();
		alpha_t.			clear();
		
		pos_x.				reserve(size);
		pos_y.				reserve(size);
		old_pos_x.			reserve(size);
		old_pos_y.			reserve(size);
		extra_pos_x.		reserve(size);
		extra_pos_y.		reserve(size);
		old_extra_pos_x.	reserve(size);
		old_extra_pos_y.	reserve(size);
		draw_x.				reserve(size);
		draw_y.				reserve(size);
		alpha_t.			reserve(size);
		
		count = size;
	}
	
	void Expand () {
		pos_x.				push(origin.x);
		pos_y.				push(origin.y);
		old_pos_x.			push(origin.x);
		old_pos_y.			push(origin.y);
		extra_pos_x.		push(origin.x);
		extra_pos_y.		push(origin.y);
		old_extra_pos_x.	push(origin.x);
		old_extra_pos_y.	push(origin.y);
		draw_x.				push(origin.x);
		draw_y.				push(origin.y);
		alpha_t.			push(0);
		count++;
	}
	
	// kd: We need to store the old positions for uncapped fps.
	
	// This is not private cuz you might need to call it outside when the
	// animation finishes to prevent flickering.
	void CacheOldPositions () {
		old_pos_x.copy(pos_x);
		old_pos_y.copy(pos_y);
	}
	
	// kd: Then we move. The resulting vectors, if you scale them by the screen
	// radius, will be the base screen positions (on which you also need to
	// add the offsets -- see Draw).
	
	// We do store the old positions, though. That's cuz I don't want to call
	// sin and cos like a million times during draw just for interpolation.
	// (You wouldn't be able to tell the difference anyway.)
	
	// You only have to call this when the ring is being animated (rotating and
	// moving in or out).
	void NextFrame (double anim_f = 1, double rotation_count = 1) {
		let smooth_t	= 1.0 * Gc_Interpol.trerpfast_unit(anim_f);
		let ang			= rotation_count * 360.0 * (1 - smooth_t);
		let ang_step	= 360.0 / max(1, count);
		
		for(let i = 0; i < count; i++) {
			pos_x [i] = -smooth_t * sin(ang);
			pos_y [i] = -smooth_t * cos(ang);
			ang += ang_step;
		}
	}
	
	void NextFrameNoRot (double anim_f = 1) {
		let smooth_t	= 1.0 * Gc_Interpol.trerpfast_unit(anim_f);
		let ang_step	= 360.0 / max(1, count);
		let ang			= 0.0;
		
		for(let i = 0; i < count; i++) {
			pos_x [i] = -smooth_t * sin(ang);
			pos_y [i] = -smooth_t * cos(ang);
			ang += ang_step;
		}
	}
	
	// kd: Prevents flickering when called on shop open.
	void ClearPositions () {
		for(let i = 0; i < count; i++) {
			pos_x [i] = 0;
			pos_y [i] = 0;
		}
		
		old_pos_x.copy(pos_x);
		old_pos_y.copy(pos_x);
		extra_pos_x.copy(pos_x);
		extra_pos_y.copy(pos_x);
		old_extra_pos_x.copy(pos_x);
		old_extra_pos_y.copy(pos_x);
	}
	
	vector2 NormalOrigin (int i) const {
		let pos = screen_radius * (pos_x [i], pos_y [i]) + screen_origin;
		return pos;
	}
	
	// kd: Now for the extra animations.
	
	// void CacheOldExtraPositions () {
		// old_extra_pos_x.copy(extra_pos_x);
		// old_extra_pos_y.copy(extra_pos_y);
	// }
	
	void NextExtraFrame (int index, vector2 pos) {
		old_extra_pos_x [index] = extra_pos_x [index];
		old_extra_pos_y [index] = extra_pos_y [index];
		
		extra_pos_x [index] = pos.x;
		extra_pos_y [index] = pos.y;
	}
	
	// kd: Finally, drawing. total_alpha is multiplied to all alpha transl.,
	// while base_alpha is how translucent the buttons appear when not
	// highlighted.
	
	// This is so I can fade the entire shop from one state to another, since
	// highlighted alpha is added on top.
	
	// But here comes a real banger: If you draw the button with the item on
	// top and expect to be able to mix alpha for the second item in the same
	// way, you'll have a bad time, because the effective alpha will be
	// way different. You know what to do: Draw all the button backgrounds
	// with properly mixed alpha, then the icons, then the darkness, etc.
	
	// So I'll just put this function here that does everything. It's not so
	// interesting that you'd want to scroll past it in main control.txt every
	// single time, is it?
	
	ui void Draw (
	Gc_Shop_CacheSmoothing	smoothing,
	Gc_NormalFades			offsets,
	Gcs_ButtonSprites		sprites,
	double					ring_alpha	= 1,
	double					total_alpha	= 1,
	double					t			= 1) const {
		ring_alpha		*= total_alpha;
		let co_alpha 	= (ring_alpha - total_alpha) / (ring_alpha - 1);
		
		// kd: Some alpha mixing has to be done to ensure the buttons, once they
		// overlap, always "add up" to the same alpha.
		
		// Note how drawing the same sprite twice with 0.5 alpha will not give
		// you the sprite with 1.0 alpha.
		
		BeginDraw(offsets, t);
		
		// kd: First the properly mixed buttons.
		DrawButtons(
			smoothing.fading_to_cache,
			sprites,
			base_alpha: ring_alpha);
		
		DrawButtons(
			smoothing.cache,
			sprites,
			base_alpha: co_alpha);
			
		DrawHighlight(
			smoothing.fading_to_cache,
			sprites,
			base_alpha: ring_alpha);
		
		DrawHighlight(
			smoothing.cache,
			sprites,
			base_alpha: co_alpha);
		
		// kd: Mix the icons on top.
		DrawIcons(
			smoothing.fading_to_cache,
			base_alpha: total_alpha);
		
		// kd: And the darkness / equip ring.
		
		if(total_alpha <= ring_alpha) {
			DrawOverlays(
				smoothing.cache,
				sprites,
				base_alpha: total_alpha);
		}
		
		else {
			DrawOverlays(
				smoothing.cache,
				sprites,
				base_alpha: total_alpha - ring_alpha);
			
			DrawOverlays(
				smoothing.fading_to_cache,
				sprites,
				base_alpha: ring_alpha);
		}
	}
	
	vector2 DrawPos (int i) const {
		return (
			draw_x [i],
			draw_y [i]);
	}
	
	vector2 Resolution () const {
		return (rscreen.width, rscreen.height);
	}
	
	ui void BeginDraw (
	Gc_NormalFades		offsets,
	double				t		= 1) const {
		for(let i = 0; i < count; i++) {
			alpha_t [i]			= Gc_Interpol.trerp_unit(offsets.At(i, t));
			let off_highlight	= alpha_t [i] * screen_highlight_offset;
			
			draw_x [i]	= t * pos_x [i] + (1 - t) * old_pos_x [i];
			draw_y [i]	= t * pos_y [i] + (1 - t) * old_pos_y [i];
			
			draw_x [i]	*= off_highlight + screen_radius;
			draw_y [i]	*= off_highlight + screen_radius;
			
			draw_x [i]	+= screen_origin.x;
			draw_y [i]	+= screen_origin.y;
			
			// kd: Shake anim.
			// draw_x [i]	+= extra_pos_x [i];
			// draw_y [i]	+= extra_pos_y [i];
		}
	}
	
	ui void DrawButtons (
	Gc_Shop_DrawCache	cache,
	Gcs_ButtonSprites	sprites,
	double				base_alpha	= 1.0) const {
		for(let i = 0; i < count; i++) {
			// kd: This draws the lamp sprite (it should already have the proper
			// colour and lamp count based on your inventory).
			Screen.DrawTexture(
				cache.LampSprite(i),
				true,	// Can use animdefs
				draw_x [i],
				draw_y [i],
				DTA_ALPHA,				base_alpha,
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_VIRTUALWIDTHF,		rscreen.width,
				DTA_VIRTUALHEIGHTF,		rscreen.height);
		}
	}
	
	ui void DrawHighlight (
	Gc_Shop_DrawCache	cache,
	Gcs_ButtonSprites	sprites,
	double				base_alpha	= 1.0) const {
		for(let i = 0; i < count; i++) {
			Screen.DrawTexture(
				cache.HighlightSprite(i),
				true,	// Can use animdefs
				draw_x [i],
				draw_y [i],
				DTA_ALPHA,				base_alpha * alpha_t [i],
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_VIRTUALWIDTHF,		rscreen.width,
				DTA_VIRTUALHEIGHTF,		rscreen.height);
		}
	}
	
	ui void DrawIcons (
	Gc_Shop_DrawCache	cache,
	double				base_alpha	= 1.0) const {
		for(let i = 0; i < count; i++) {
			// kd: Now put the item's sprite over it all.
			Screen.DrawTexture(
				cache.ItemSprite(i),
				true,
				draw_x [i],
				draw_y [i],
				DTA_ALPHA,				base_alpha + (1 - base_alpha) * alpha_t [i],
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_VIRTUALWIDTHF,		rscreen.width,
				DTA_VIRTUALHEIGHTF,		rscreen.height);
		}
	}
	
	ui void DrawOverlays (
	Gc_Shop_DrawCache	cache,
	Gcs_ButtonSprites	sprites,
	double				base_alpha	= 1.0) const {
		
		let sprite_ring		= sprites.EquipOverlay();
		let sprite_darken	= sprites.Darken();
		
		for(let i = 0; i < count; i++) {
			
			// kd: The glowy ring when you activate an item.
			let equip_alpha = base_alpha * sprites.equip_alpha * cache.EquipAlpha(i);
			
			Screen.DrawTexture(
				sprite_ring,
				true,
				draw_x [i],
				draw_y [i],
				DTA_LEGACYRENDERSTYLE,	STYLE_ADDSTENCIL,
				DTA_FILLCOLOR,			sprites.equip_ring_tint,
				DTA_ALPHA,				equip_alpha,
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_VIRTUALWIDTHF,		rscreen.width,
				DTA_VIRTUALHEIGHTF,		rscreen.height);
			
			// kd: And the dark stuff for stuff you cannot afford.
			let darken_alpha = base_alpha * sprites.darken_alpha * cache.DarkenAlpha(i);
			
			Screen.DrawTexture(
				sprite_darken,
				true,
				draw_x [i],
				draw_y [i],
				DTA_ALPHA,				darken_alpha,
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_VIRTUALWIDTHF,		rscreen.width,
				DTA_VIRTUALHEIGHTF,		rscreen.height);
		}
	}
}
