/**
	\file

	\brief
	In-game menu

	\author
	johnnycz
**/

#include "quakedef.h"
#include "keys.h"
#include "menu.h"
#include "Ctrl.h"
#include "settings.h"
#include "settings_page.h"
#include "server.h"

#define TOPMARGIN (6*LETTERWIDTH)

settings_page single_menu;
settings_page ingame_menu;
settings_page democtrl_menu;
settings_page botmatch_menu;
settings_page qtv_menu;

#define MENU_ALIAS(func,command,leavem) static void func(void) { Cbuf_AddText(command "\n"); if (leavem) M_LeaveMenus(); }

void MIng_MainMenu(void)		{ M_Menu_Main_f(); }
void MIng_Back(void)			{ M_LeaveMenus(); }

MENU_ALIAS(MIng_ServerBrowser,"menu_slist",false);
MENU_ALIAS(MIng_Options,"menu_options",false);
MENU_ALIAS(MIng_Join,"join",true);
MENU_ALIAS(MIng_Observe,"observe",true);
MENU_ALIAS(MIng_Disconnect,"disconnect",true);
MENU_ALIAS(MIng_Ready, "ready",true);
MENU_ALIAS(MIng_Break, "break",true);
MENU_ALIAS(MIng_SkillUp, "skillup",false);
MENU_ALIAS(MIng_SkillDown, "skilldown",false);
MENU_ALIAS(MIng_AddBot, "addbot",false);
MENU_ALIAS(MIng_RemoveBot, "removebot",false);
MENU_ALIAS(MIng_TeamBlue, "team blue;color 13",true);
MENU_ALIAS(MIng_TeamRed, "team red;color 4",true);
MENU_ALIAS(MDemoCtrl_DemoBrowser,"menu_demos",false);
MENU_ALIAS(MDemoCtrl_DemoControls, "demo_controls",true);
MENU_ALIAS(MSP_Load, "menu_load", false);
MENU_ALIAS(MSP_Save, "menu_save", false);
MENU_ALIAS(MQTV_Autotrack, "autotrack", true);
MENU_ALIAS(MQTV_Lastscores, "lastscores", true);
MENU_ALIAS(MQTV_Observers, "qtvusers", true);
MENU_ALIAS(MQTV_Reconnect, "qtvreconnect", true);

setting single_menu_entries[] = {
	ADDSET_SEPARATOR("In-game Menu"),
	ADDSET_ACTION("Load Game", MSP_Load, ""),
	ADDSET_ACTION("Save Game", MSP_Save, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Options", MIng_Options, ""),
	ADDSET_ACTION("Main Menu", MIng_MainMenu, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Return To Game", MIng_Back, ""),
};

setting ingame_menu_entries[] = {
	ADDSET_SEPARATOR("In-game Menu"),
	ADDSET_ACTION("Ready", MIng_Ready, ""),
	ADDSET_ACTION("Break", MIng_Break, ""),
	ADDSET_ACTION("Join", MIng_Join, ""),
	ADDSET_ACTION("Observe", MIng_Observe, ""),
	ADDSET_ACTION("Disconnect", MIng_Disconnect, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Server Browser", MIng_ServerBrowser, ""),
	ADDSET_ACTION("Options", MIng_Options, ""),
	ADDSET_ACTION("Main Menu", MIng_MainMenu, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Return To Game", MIng_Back, ""),
};

setting democtrl_menu_entries[] = {
	ADDSET_SEPARATOR("Demo Control Menu"),
	ADDSET_ACTION("Toggle autotrack", MQTV_Autotrack, ""),
	ADDSET_ACTION("Demo controls", MDemoCtrl_DemoControls, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Disconnect", MIng_Disconnect, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Demo Browser", MDemoCtrl_DemoBrowser, ""),
	ADDSET_ACTION("Options", MIng_Options, ""),
	ADDSET_ACTION("Main Menu", MIng_MainMenu, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Return To Demo", MIng_Back, ""),
};

setting qtv_menu_entries[] = {
	ADDSET_SEPARATOR("QuakeTV Menu"),
	ADDSET_ACTION("Toggle autotrack", MQTV_Autotrack, ""),
	ADDSET_ACTION("Last scores", MQTV_Lastscores, ""),
	ADDSET_ACTION("List observers", MQTV_Observers, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Reconnect", MQTV_Reconnect, ""),
	ADDSET_ACTION("Disconnect", MIng_Disconnect, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Options", MIng_Options, ""),
	ADDSET_ACTION("Main Menu", MIng_MainMenu, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Return To Game", MIng_Back, ""),
};

setting botmatch_menu_entries[] = {
	ADDSET_SEPARATOR("Botmatch Menu"),
	ADDSET_ACTION("Ready", MIng_Ready, ""),
	ADDSET_ACTION("Break", MIng_Break, ""),
	ADDSET_ACTION("Team Blue", MIng_TeamBlue, ""),
	ADDSET_ACTION("Team Red", MIng_TeamRed, ""),
	ADDSET_ACTION("Add Bot", MIng_AddBot, ""),
	ADDSET_ACTION("Remove Bot", MIng_RemoveBot, ""),
	ADDSET_ACTION("Increase Bot Skill", MIng_SkillUp, ""),
	ADDSET_ACTION("Decrease Bot Skill", MIng_SkillDown, ""),
	ADDSET_ACTION("Disconnect", MIng_Disconnect, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Options", MIng_Options, ""),
	ADDSET_ACTION("Main Menu", MIng_MainMenu, ""),
	ADDSET_BLANK(),
	ADDSET_ACTION("Return To Game", MIng_Back, ""),
};

#define DEMOPLAYBACK() (cls.demoplayback == DT_QWD \
	|| (cls.demoplayback == DT_MVD && cls.mvdplayback == 1) \
	|| cls.demoplayback == DT_NQDEMO)
#define BOTMATCH() (!strcmp(cls.gamedirfile, "fbca"))
#define SINGLEPLAYER() (com_serveractive && cls.state == ca_active && !cl.deathmatch && maxclients.value == 1)
#define QTVPLAYBACK() (cls.mvdplayback == 2)

static settings_page *M_Ingame_Current(void) {
	if (DEMOPLAYBACK()) {
		return &democtrl_menu;
	}
	else if (BOTMATCH()) {
		return &botmatch_menu;
	}
	else if (SINGLEPLAYER())
	{
		return &single_menu;
	}
	else if (QTVPLAYBACK()) {
		return &qtv_menu;
	}
	else {
		return &ingame_menu;
	}
}

void M_Ingame_Draw(void) {
	M_Unscale_Menu();
	Settings_Draw(0, TOPMARGIN, vid.width, vid.height - TOPMARGIN, M_Ingame_Current());
}

void M_Ingame_Key(int key) {
	if (Settings_Key(M_Ingame_Current(), key, 0)) return;

	switch (key) {
	case K_MOUSE2:
	case K_ESCAPE: M_LeaveMenus(); break;
	}
}

qbool Menu_Ingame_Mouse_Event(const mouse_state_t *ms) {
	mouse_state_t m = *ms;
	m.y -= TOPMARGIN;
	return Settings_Mouse_Event(M_Ingame_Current(), &m);
}

void Menu_Ingame_Init(void)
{
	Settings_Page_Init(single_menu, single_menu_entries);
	Settings_Page_SetMinit(single_menu);
	Settings_Page_Init(ingame_menu, ingame_menu_entries);
	Settings_Page_SetMinit(ingame_menu);
	Settings_Page_Init(democtrl_menu, democtrl_menu_entries);
	Settings_Page_SetMinit(democtrl_menu);
	Settings_Page_Init(botmatch_menu, botmatch_menu_entries);
	Settings_Page_SetMinit(botmatch_menu);
	Settings_Page_Init(qtv_menu, qtv_menu_entries);
	Settings_Page_SetMinit(qtv_menu);
}
