#pragma once

#ifdef GAMEQC
#include <common/models/all.qh>
#include <common/sounds/all.qh>
#include <common/sounds/all.inc>
#include <common/stats.qh>
#endif

#ifdef SVQC
#include <server/items/spawning.qh>
#endif

#ifndef SVQC
#include <common/util.qh> // for icon_path_from_HUDskin
#endif

#ifdef MENUQC
#include <menu/draw.qh>
#endif

#ifdef GAMEQC
const int IT_UNLIMITED_AMMO 			=  BIT(0); // when this bit is set, using a weapon does not reduce ammo. Checkpoints can give this powerup.
const int IT_UNLIMITED_SUPERWEAPONS		=  BIT(1); // when this bit is set, superweapons don't expire. Checkpoints can give this powerup.

const int IT_JETPACK               		=  BIT(2); // actual item
const int IT_USING_JETPACK         		=  BIT(3); // confirmation that button is pressed
const int IT_FUEL_REGEN            		=  BIT(4); // fuel regeneration trigger

const int IT_RESOURCE       			=  BIT(5); // bitflag to mark this item as a resource (unused)

const int IT_KEY1						= BIT(6);
const int IT_KEY2						= BIT(7);

const int IT_BUFF						= BIT(8); // unused bit for buff items

// special colorblend meaning in engine
// legacy bitflags for powerups
const int IT_INVISIBILITY				= BIT(9);
const int IT_INVINCIBLE					= BIT(10);
const int IT_SUPERWEAPON				= BIT(11); // suit
const int IT_STRENGTH					= BIT(12);
const int IT_SPEED						= BIT(13);

// item masks
const int IT_PICKUPMASK                	= IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS | IT_JETPACK | IT_FUEL_REGEN; // strength and invincible are handled separately

// item networking
const int ISF_REMOVEFX          = BIT(0); // technically unnecessary (after the kludge in Item_Think() is reverted), but cheaper and cleaner than using ITS_AVAILABLE
const int ISF_LOCATION          = BIT(1);
const int ISF_SIZE2             = BIT(2);
const int ISF_STATUS            = BIT(3);
const int ISF_COLORMAP          = BIT(4);
const int ISF_DROP              = BIT(5);
const int ISF_ANGLES            = BIT(6);
const int ISF_SIZE              = BIT(7);

REGISTER_NET_LINKED(ENT_CLIENT_ITEM)

// item status
.int ItemStatus;
const int ITS_STAYWEP           = BIT(0);
const int ITS_ANIMATE1          = BIT(1);
const int ITS_ANIMATE2          = BIT(2);
const int ITS_AVAILABLE         = BIT(3);
const int ITS_ALLOWFB           = BIT(4);
const int ITS_ALLOWSI           = BIT(5);
const int ITS_GLOW              = BIT(6);
const int ITS_EXPIRING          = BIT(7);

// enough to notice it's about to despawn and circle jump to grab it
const float IT_DESPAWNFX_TIME = 1.5;

// 2hz probably enough to correct a desync caused by serious lag
// FIXME but updating faster applies the kludge in Item_Think() sooner so it's less noticeable
const float IT_UPDATE_INTERVAL = 0.0625;

// item bboxes for sv_legacy_bbox_expand 0
// Small, Default and Large (large maxs are used with default mins)
const vector ITEM_S_MINS = '-24 -24 0';
const vector ITEM_S_MAXS = '24 24 48';
const vector ITEM_D_MINS = '-30 -30 0'; // 0.8.6 set '-16 -16 0' then DP subtracted '15 15 1' but NetRadiant used '-30 -30 0'
const vector ITEM_D_MAXS = '30 30 48';  // 0.8.6 set '16 16 48' then DP added '15 15 1' but NetRadiant used '30 30 32'
const vector ITEM_L_MAXS = '30 30 70';  // 0.8.6 set '16 16 80' for powerups, '16 16 70' for megas, '16 16 60' for buffs

.float fade_start;
.float fade_end;

.string mdl;
#endif

#ifdef SVQC
.float strength_finished; // NOTE: this field is used only by map entities, it does not directly apply the strength stat
.float invincible_finished; // ditto
.float buffs_finished; // ditts

#define SPAWNFUNC_BODY(item) \
	if (item && Item_IsDefinitionAllowed(item)) \
		StartItem(this, item); \
	else \
	{ \
		startitem_failed = true; \
		delete(this); \
	}

#define SPAWNFUNC_ITEM(name, item) \
	spawnfunc(name) \
	{ \
		SPAWNFUNC_BODY((item)) \
	}

#else

#define SPAWNFUNC_ITEM(name, item)

#endif

enum
{
	ITEM_FLAG_NORMAL = BIT(0), ///< Item is usable during normal gameplay.
	ITEM_FLAG_MUTATORBLOCKED = BIT(1),
	ITEM_FLAG_RESOURCE = BIT(2) ///< Item is is a resource, not a held item.
};

#define ITEM_HANDLE(signal, ...) __Item_Send_##signal(__VA_ARGS__)
CLASS(GameItem, Object)
	ATTRIB(GameItem, m_id, int, 0);
	/** the canonical spawnfunc name */
	ATTRIB(GameItem, m_canonical_spawnfunc, string);
	METHOD(GameItem, m_spawnfunc_hookreplace, GameItem(GameItem this, entity e)) { return this; }
	ATTRIB(GameItem, m_name, string);
	ATTRIB(GameItem, m_icon, string);
	ATTRIB(GameItem, m_color, vector, '1 1 1');
	ATTRIB(GameItem, m_waypoint, string);
	ATTRIB(GameItem, m_waypointblink, int, 1);
#ifdef GAMEQC
	ATTRIB(GameItem, m_glow, bool, false);
	ATTRIB(GameItem, m_respawnsound, Sound, SND_ITEMRESPAWN);
#endif
#ifdef MENUQC
	METHOD(GameItem, describe, string(GameItem this))
	{
		TC(GameItem, this);
		return SUPER(GameItem).describe(this);
	}
#endif
#ifndef SVQC
	METHOD(GameItem, display, void(GameItem this, void(string name, string icon) returns))
	{
		TC(GameItem, this);
		returns(this.m_name, icon_path_from_HUDskin(this.m_icon));
	}
#endif
	METHOD(GameItem, show, void(GameItem this))
	{
		TC(GameItem, this);
		LOG_INFO("A game item");
	}
	void ITEM_HANDLE(Show, GameItem this) { this.show(this); }
ENDCLASS(GameItem)
