#pragma once

#include <common/teams.qh>
#include <common/util.qh>

#ifdef GAMEQC
#include <common/items/item.qh>
#include <common/mutators/mutator/waypoints/all.qh>
#endif
#ifndef SVQC
#include <common/util.qh> // for icon_path_from_HUDskin
#endif

#define REGISTER_BUFF(id, inst) REGISTER(StatusEffects, BUFF_##id, m_id, inst)

#include <common/mutators/mutator/status_effects/_mod.qh>
CLASS(Buff, StatusEffect)
#ifdef GAMEQC
	ATTRIB(Buff, m_itemid, int, IT_BUFF);
#endif
	ATTRIB(Buff, netname, string, "buff");
	ATTRIB(Buff, m_color, vector, '1 1 1');
	ATTRIB(Buff, m_name, string, _("Buff"));
#ifdef GAMEQC
	ATTRIB(Buff, m_skin, int, 0);
#endif
	ATTRIB(Buff, m_sprite, string, "");
#ifdef MENUQC
	METHOD(Buff, describe, string(Buff this))
	{
		TC(Buff, this);
		return SUPER(Buff).describe(this);
	}
#endif
#ifndef SVQC
	ATTRIB(Buff, m_icon, string, "buff");
	METHOD(Buff, display, void(Buff this, void(string name, string icon) returns))
	{
		TC(Buff, this);
		returns(this.m_name, icon_path_from_HUDskin(this.m_icon));
	}
#endif
#ifdef SVQC
	ATTRIB(Buff, m_lifetime, float, 60);
	METHOD(Buff, m_time, float(Buff this))
	{
		return cvar(strcat("g_buffs_", this.netname, "_time"));
	}
#endif
ENDCLASS(Buff)

STATIC_INIT(REGISTER_BUFFS)
{
	FOREACH(StatusEffects, it.instanceOfBuff, it.m_sprite = strzone(strcat("buff-", it.netname)));
}

#ifdef SVQC
	.entity buffdef;
	.int team_forced;
	void buff_Init(entity ent);
	void buff_Init_Compat(entity ent, entity replacement);
	#define BUFF_SPAWNFUNC(e, b, t) \
		spawnfunc(item_buff_##e) \
		{ \
			this.buffdef = b; \
			if (teamplay) \
				this.team_forced = t; \
			buff_Init(this); \
		}
	#define BUFF_SPAWNFUNCS(e, b)                       \
	        BUFF_SPAWNFUNC(e,           b,  0)          \
	        BUFF_SPAWNFUNC(e##_team1,   b,  NUM_TEAM_1) \
	        BUFF_SPAWNFUNC(e##_team2,   b,  NUM_TEAM_2) \
	        BUFF_SPAWNFUNC(e##_team3,   b,  NUM_TEAM_3) \
	        BUFF_SPAWNFUNC(e##_team4,   b,  NUM_TEAM_4)
	#define BUFF_SPAWNFUNC_Q3COMPAT(o, r) \
		spawnfunc(o) \
		{ \
			buff_Init_Compat(this, r); \
		}
#else
	#define BUFF_SPAWNFUNC(e, b, t)
	#define BUFF_SPAWNFUNCS(e, b)
	#define BUFF_SPAWNFUNC_Q3COMPAT(o, r)
#endif

#ifdef GAMEQC
string BUFF_NAME(int i);
#endif

#ifdef SVQC
string Buff_CompatName(string buffname);
#endif

BUFF_SPAWNFUNCS(random, NULL)

#ifdef GAMEQC
REPLICATE_INIT(bool, cvar_cl_buffs_autoreplace);
#endif
