#pragma once

#include <common/command/_mod.qh>
#include "config.qh"

#include "turret.qh"

REGISTRY(Turrets, BITS(5))
#define get_turretinfo(i) REGISTRY_GET(Turrets, i)
REGISTER_REGISTRY(Turrets)
REGISTRY_CHECK(Turrets)

#define TR_PROPS_COMMON(P, class, prefix) \
	P(class, prefix, aim_firetolerance_dist, float) \
	P(class, prefix, aim_maxpitch, float) \
	P(class, prefix, aim_maxrot, float) \
	P(class, prefix, aim_speed, float) \
	P(class, prefix, ammo_max, float) \
	P(class, prefix, ammo_recharge, float) \
	P(class, prefix, health, float) \
	P(class, prefix, respawntime, float) \
	P(class, prefix, shot_dmg, float) \
	P(class, prefix, shot_force, float) \
	P(class, prefix, shot_radius, float) \
	P(class, prefix, shot_refire, float) \
	P(class, prefix, shot_speed, float) \
	P(class, prefix, shot_spread, float) \
	P(class, prefix, shot_volly, float) \
	P(class, prefix, shot_volly_refire, float) \
	P(class, prefix, target_range, float) \
	P(class, prefix, target_range_min, float) \
	P(class, prefix, target_range_optimal, float) \
	P(class, prefix, target_select_anglebias, float) \
	P(class, prefix, target_select_missilebias, float) \
	P(class, prefix, target_select_playerbias, float) \
	P(class, prefix, target_select_rangebias, float) \
	P(class, prefix, target_select_samebias, float) \
	P(class, prefix, track_accel_pitch, float) \
	P(class, prefix, track_accel_rot, float) \
	P(class, prefix, track_blendrate, float) \
	P(class, prefix, track_type, float) \
	/**/

#define TR_PROPS(L, class, prefix) \
	L(TR_CONFIG_BEGIN, TR_CONFIG, TR_CONFIG_END, class, prefix) \
	/**/

#define TR_CONFIG(class, turname, fld, T) \
	_TR_CONFIG(class, fld, T, turname)
#ifdef SVQC
	#define TR_CONFIG_BEGIN(class) METHOD(class, tr_config, void(class this)) \
	{
		#define _TR_CONFIG(class, fld, T, turname) \
		if (#turname == this.netname) \
			TUR_CONFIG_WRITE_CVARS(turname, fld, T);
	#define TR_CONFIG_END() \
	}
#else
	#define TR_CONFIG_BEGIN(class)
	#define _TR_CONFIG(class, fld, T, turname)
	#define TR_CONFIG_END()
#endif

#define DEFAULT_FILENAME "turrets_dump.cfg"
// NOTE: dumpeffectinfo, dumpnotifs, dumpturrets and dumpweapons use similar code
GENERIC_COMMAND(dumpturrets, "Dump all turrets into " DEFAULT_FILENAME, false)
{
	switch (request)
	{
		case CMD_REQUEST_COMMAND:
		{
		#ifdef SVQC
			tur_config_file = -1;
			tur_config_alsoprint = -1;
			string filename = argv(1);

			if (filename == "")
			{
				filename = DEFAULT_FILENAME;
				tur_config_alsoprint = false;
			}
			else if (filename == "-")
			{
				filename = DEFAULT_FILENAME;
				tur_config_alsoprint = true;
			}
			tur_config_file = fopen(filename, FILE_WRITE);

			if (tur_config_file >= 0)
			{
				Dump_Turret_Settings();
				LOG_INFOF("Dumping turrets... File located in ^2data/data/%s^7.", filename);
				fclose(tur_config_file);
				tur_config_file = -1;
				tur_config_alsoprint = -1;
			}
			else
				LOG_INFOF("^1Error: ^7Could not open file '%s'!", filename);
		#else
			LOG_INFO("Turrets dump command only works with sv_cmd.");
		#endif
			return;
		}
		default:
		case CMD_REQUEST_USAGE:
			LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " dumpturrets [<filename>]");
			LOG_HELPF("  Where <filename> is the file to write (default is %s),", DEFAULT_FILENAME);
			LOG_HELP("  if supplied with '-' output to console as well as default,");
			LOG_HELP("  if left blank, it will only write to default.");
			return;
	}
}
#undef DEFAULT_FILENAME


const int TUR_FIRST = 1;
#define TUR_LAST (REGISTRY_COUNT(Turrets) - 1)

#define REGISTER_TURRET(id, inst) REGISTER(Turrets, TUR, id, m_id, inst)

CLASS(NullTurret, Turret)
	ATTRIB(NullTurret, m_hidden, bool, true);
ENDCLASS(NullTurret)
REGISTER_TURRET(Null, NEW(NullTurret));
REGISTRY_DEFINE_GET(Turrets, TUR_Null)

#include "turret/_mod.qh"
