#pragma once
#include "uielement.h"

float backdrop_pause;
int in_game;
uielement_c menu_current;
uielement_c menu_main;
uielement_c menu_main_game;
uielement_c menu_options;
uielement_c menu_options_accessibility;
uielement_c menu_options_display;
uielement_c menu_options_graphics;
uielement_c menu_options_audio;
uielement_c menu_options_controls;
uielement_c menu_options_hud;
uielement_c menu_customize;
uielement_c menu_serverbrowser;
uielement_c menu_gameoverlay;

float menu_conditional(uielement_c this)
 {
	if (menu_current != this)
		return FALSE;
	return TRUE;
};

void menu_gotomain(void)
{
	if (in_game)
		menu_current = menu_main_game;
	else
		menu_current = menu_main;
}

void menu_spawnlogo(uielement_c m);
.int logo_wantedmode;

typedef struct
{
	string 			img;
	void(uielement_c this) func;
	float(uielement_c this) condition;
} buttons_t;

#define Button_SpawnSchema(m, b) buttons_spawn(m, b, b.length)
void buttons_spawn(uielement_c menu, buttons_t *buttons, int listlength)

#define MENU_LIST \
	MENU_INIT(MenuInit_Backdrop) \
	MENU_INIT(MenuInit_Main) \
	MENU_INIT(MenuInit_Main_Game) \
	MENU_INIT(MenuInit_Options) \
	MENU_INIT(MenuInit_Options_Display) \
	MENU_INIT(MenuInit_Options_Graphics) \
	MENU_INIT(MenuInit_Options_Audio) \
	MENU_INIT(MenuInit_Options_Hud) \
	MENU_INIT(MenuInit_Options_Controls) \
	MENU_INIT(MenuInit_Options_Accessibility) \
	MENU_INIT(MenuInit_CustomizeNew) \
	MENU_INIT(MenuInit_ServerBrowser) \
	MENU_INIT(MenuInit_Overlay) 

#define MENU_INIT(x) void x(void);
MENU_LIST
#undef MENU_INIT

/* #region reinit list */
.uielement_c reinit_list;
.void(uielement_c this) reinit;
void Add_To_Reinit_List(uielement_c menu, uielement_c object)
{
	object.reinit_list = menu.reinit_list;
	menu.reinit_list = object;
}
void Run_Reinit_List(uielement_c object)
{
	while(object)
	{
		if (object.reinit)
			object.reinit(object);
		object = object.reinit_list;
	}
}
/* #endregion */

/* #region slide bar */
.void(uielement_c slider, __inout float frac) bar_change;
.float bar_frac;
.float bar_scrollamt;
uielement_c slider_selected;
uielement_c slider_scroll_selected;
float slider_scroll_autotime;
uielement_c UI_Sliderbar(uielement_c own, vector org, vector anc, vector just, vector sz)
{
	// partner = slider tick
	// partner2 = scroll left
	// partner3 = scroll right
	// partner6 = bar

	void(uielement_c this, __inout vector position) bar_preloop = {
		uitext_c scroll_left = (uitext_c)this.partner2;
		uitext_c scroll_right = (uitext_c)this.partner3;
		uifill_c bar_back = (uifill_c)this.partner6;
		uifill_c slider = (uifill_c)this.partner;
		
		// hightlight title
		if (this.owner.type == UITYPE::TEXT)
		{
			if (slider_selected == this || (!slider_selected && (ui_hover == this || ui_oldhover == scroll_left || ui_oldhover == scroll_right)))
				this.owner.color = '1 1 1';
			else
				this.owner.color = '0.6 0.6 0.6';
		}

		if (slider_selected == this)
		{
			float frac = (ui_mouseposition[0] - ui_position[0]) / (this.size[0] * ui_scale[0]);
			frac = bound(0, frac, 1);
			if (this.bar_change)
				this.bar_change(slider, frac);
			this.bar_frac = frac;
		}
		
		// default slider 0-1 text
		if (!this.bar_change)
		{
			uitext_c txt = (uitext_c)slider.partner;
			if (txt.text)
				strunzone(txt.text);
			txt.text = strzone(sprintf("%.2f", this.bar_frac));
		}
		//

		slider.origin[0] = this.size[0] * this.bar_frac;
	};
	void(uielement_c this, float nkey, float ascii, float released) bar_input = {
		if (nkey != K_MOUSE1)
			return;
		
		if (released)
		{
			if (slider_selected == this)
				slider_selected = 0;
		}
		else
		{
			if (INBOUNDS(ui_mouseposition, ui_position, (ui_position + [this.size[0] * ui_scale[0], this.size[1] * ui_scale[1]])))
			{
				slider_selected = this;
			}
		}
	}
	void(uitext_c this, __inout string txt_to_render, __inout vector color_to_render, __inout float alpha_to_render) scroll_button_predraw = {
		if (ui_hover == this && !slider_selected)
			color_to_render = '1 1 1';
	};
	void(uielement_c this, float nkey, float ascii, float released) scroll_button_input = {
		if (slider_scroll_selected != this)
			return;
		
		if (nkey == K_MOUSE1 && released)
			slider_scroll_selected = 0;
	};
	void(uielement_c this, __inout vector position) scroll_button_preloop = {
		if (slider_scroll_selected == this)
		{
			if (!INBOUNDS(ui_mouseposition, ui_position, (ui_position + this.totalsize())))
			{
				slider_scroll_selected = 0;
				return;
			}

			if (time > slider_scroll_autotime) // do auto repeat
			{
				localsound("sounds/menu.wav");
				uielement_c bar = this.owner;
				uifill_c slider = (uifill_c)bar.partner;

				slider_scroll_autotime = time + 0.1;

				float frac = bar.bar_frac + (bar.bar_scrollamt * this.bar_scrollamt);
				frac = bound(0, frac, 1);
				if (bar.bar_change)
					bar.bar_change(slider, frac);
				bar.bar_frac = frac;
			}
		}
	};
	void(uielement_c this) scroll_button_click = {
		if (this.owner)
		{
			localsound("sounds/menu.wav");
			uielement_c bar = this.owner;
			uifill_c slider = (uifill_c)bar.partner;

			slider_scroll_selected = this;
			slider_scroll_autotime = time + 0.3;

			float frac = bar.bar_frac + (bar.bar_scrollamt * this.bar_scrollamt);
			frac = bound(0, frac, 1);
			if (bar.bar_change)
				bar.bar_change(slider, frac);
			bar.bar_frac = frac;
		}
	};

	uielement_c bar = spawn(uielement_c, owner: own, origin: org, anchor: anc, justify: just, size: sz + '0 24', flags: UIFLAG::HIGHLIGHT);
		bar.preloop = bar_preloop;
		bar.input = bar_input;
		bar.bar_scrollamt = 0.05;
	// bar itself
	bar.partner6 = spawn(uifill_c, owner: bar, justify: '0 0.5', anchor: '0 0.5', size: sz, color: '0.6 0.6 0.6', alpha: 0.3);

	// scroll buttons
	float scroll_sz = sz[1] + 12;
	bar.partner2 = spawn(uitext_c, owner: bar, anchor: '0 0.5', justify:'0 0.5', origin: '-24 2', font_size: [scroll_sz, scroll_sz], color: '1 1 1', alpha: 0.9, text: "<", flags: UIFLAG::CLICKABLE, bar_scrollamt: -1);
		((uitext_c)bar.partner2).prerender = scroll_button_predraw;
		bar.partner2.m_click = scroll_button_click;
		bar.partner2.preloop = scroll_button_preloop;
		bar.partner2.input = scroll_button_input;
	bar.partner3 = spawn(uitext_c, owner: bar, anchor: '1 0.5', justify:'1 0.5', origin: '24 2', font_size: [scroll_sz, scroll_sz], color: '1 1 1', alpha: 0.9, text: ">", flags: UIFLAG::CLICKABLE, bar_scrollamt: 1);
		((uitext_c)bar.partner3).prerender = scroll_button_predraw;
		bar.partner3.m_click = scroll_button_click;
		bar.partner3.preloop = scroll_button_preloop;
		bar.partner3.input = scroll_button_input;
	
	// slider tick and number text
	bar.partner = spawn(uifill_c, owner: bar, anchor: '0 0.5', justify: '0.5 0.5', size: [sz[1], sz[1] + 12], color: '0.6 0.6 0.6' * 0.8, alpha: 1);
	bar.partner.partner = spawn(uitext_c, owner: bar.partner, origin: '0 -2', anchor: '0.5 0', justify: '0.5 1', font_size: '10 10', text: strzone("1.0"), font: FONT_HUD, color: '1 1 1');

	return bar;
}
/* #endregion */