#pragma once

#include "utils.qh"
#include <server/intermission.qh>
//#include <common/resources/resources.qh>
#include <common/replicate.qh>
#include <common/sounds/all.qh>

bool autocvar__notarget;
bool autocvar_sv_show_entnum;
int autocvar_g_balance_armor_start;
float autocvar_g_balance_pause_armor_rot_spawn;
float autocvar_g_balance_pause_fuel_rot_spawn;
float autocvar_g_balance_pause_health_regen_spawn;
float autocvar_g_balance_pause_health_rot_spawn;
bool autocvar_g_botclip_collisions;
bool autocvar_g_fullbrightplayers;
bool autocvar_g_playerclip_collisions;
float autocvar_g_player_alpha;
float autocvar_g_player_brightness;
float autocvar_g_player_damageforcescale = 2;
float autocvar_g_respawn_delay_small;
int autocvar_g_respawn_delay_small_count;
float autocvar_g_respawn_delay_large;
int autocvar_g_respawn_delay_large_count;
float autocvar_g_respawn_delay_max;
bool autocvar_g_respawn_delay_forced;
bool autocvar_g_respawn_ghosts;
float autocvar_g_respawn_ghosts_alpha = 1;
float autocvar_g_respawn_ghosts_fadetime = 1.5;
float autocvar_g_respawn_ghosts_time = 4.5;
float autocvar_g_respawn_ghosts_speed;
int autocvar_g_respawn_waves;
bool autocvar_g_nodepthtestplayers;
string autocvar_g_mutatormsg;
float autocvar_sv_foginterval;
float autocvar_sv_maxidle;
int autocvar_sv_maxidle_minplayers = 2;
float autocvar_sv_maxidle_playertospectator = 60;
bool autocvar_sv_maxidle_alsokickspectators;
int autocvar_sv_maxidle_slots;
bool autocvar_sv_maxidle_slots_countbots;
bool autocvar_g_forced_respawn;
int autocvar_g_maxplayers;
float autocvar_g_maxplayers_spectator_blocktime;
string autocvar_g_xonoticversion;
float autocvar_gameversion;
float autocvar_gameversion_min;
float autocvar_gameversion_max;
string autocvar_hostname;
int autocvar_spawn_debug;
string autocvar_sv_motd;
int autocvar_sv_name_maxlength = 64;
string autocvar_sv_quickmenu_file;
bool autocvar_sv_servermodelsonly;
bool autocvar_sv_showspectators;
bool autocvar_sv_spectate;
bool autocvar_sv_teamnagger;
float autocvar_sv_player_scale;

// WEAPONTODO
.string weaponorder_byimpulse;

.entity clientdata;

.float jointime; // time of connecting
.float startplaytime; // time of switching from spectator to player
.float alivetime_start; // start time of being alive
.float death_time;

.int spectatee_status;
.bool zoomstate;

/// > 0 is a team index, if wants_join == -1 the player can't have the team they selected (conflict)
.int team_selected;
.bool just_joined;
/// > 0 is a team index, -1 means team selection is deferred until Join()
.int wants_join;

.int pressedkeys;

.int playerid;

.string playermodel;
.string playerskin;

void ClientState_attach(entity this);

CLASS(Client, Object)
    /** Client name */
    ATTRIB(Client, netname, string, this.netname);
    ATTRIB(Client, colormap, int, this.colormap);
    ATTRIB(Client, team, int, this.team);
    ATTRIB(Client, clientcolors, int, this.clientcolors);
    /** Client IP */
    ATTRIB(Client, netaddress, string, this.netaddress);
    ATTRIB(Client, playermodel, string, this.playermodel);
    ATTRIB(Client, playerskin, string, this.playerskin);

    /** fingerprint of CA key the player used to authenticate */
    ATTRIB(Client, crypto_keyfp, string, this.crypto_keyfp);
    /** fingerprint of CA key the server used to authenticate to the player */
    ATTRIB(Client, crypto_mykeyfp, string, this.crypto_mykeyfp);
    /** fingerprint of ID used by the player entity, or string_null if not identified */
    ATTRIB(Client, crypto_idfp, string, this.crypto_idfp);
    /** set if the player's ID has been signed */
    ATTRIB(Client, crypto_idfp_signed, bool, this.crypto_idfp_signed);
    /** the string "AES128" if encrypting, and string_null if plaintext */
    ATTRIB(Client, crypto_encryptmethod, string, this.crypto_encryptmethod);
    /** the string "HMAC-SHA256" if signing, and string_null if plaintext */
    ATTRIB(Client, crypto_signmethod, string, this.crypto_signmethod);

    // engine client fields
    ATTRIB(Client, impulse, int, this.impulse);

    ATTRIB(Client, button0, int, this.button0);
    ATTRIB(Client, button2, int, this.button2);
    ATTRIB(Client, button3, int, this.button3);
    ATTRIB(Client, button4, int, this.button4);
    ATTRIB(Client, button5, int, this.button5);
    ATTRIB(Client, button6, int, this.button6);
    ATTRIB(Client, button7, int, this.button7);
    ATTRIB(Client, button8, int, this.button8);
    ATTRIB(Client, button9, int, this.button9);
    ATTRIB(Client, button10, int, this.button10);
    ATTRIB(Client, button11, int, this.button11);
    ATTRIB(Client, button12, int, this.button12);
    ATTRIB(Client, button13, int, this.button13);
    ATTRIB(Client, button14, int, this.button14);
    ATTRIB(Client, button15, int, this.button15);
    ATTRIB(Client, button16, int, this.button16);
    ATTRIB(Client, buttonuse, int, this.buttonuse);
    ATTRIB(Client, buttonchat, int, this.buttonchat);

    ATTRIB(Client, cursor_active, int, this.cursor_active);
    ATTRIB(Client, cursor_screen, vector, this.cursor_screen);
    ATTRIB(Client, cursor_trace_start, vector, this.cursor_trace_start);
    ATTRIB(Client, cursor_trace_endpos, vector, this.cursor_trace_endpos);
    ATTRIB(Client, cursor_trace_ent, entity, this.cursor_trace_ent);

    ATTRIB(Client, ping, float, this.ping);
    ATTRIB(Client, ping_packetloss, float, this.ping_packetloss);
    ATTRIB(Client, ping_movementloss, float, this.ping_movementloss);

    ATTRIB(Client, v_angle, vector, this.v_angle);
    ATTRIB(Client, movement, vector, this.movement);

    // custom

    ATTRIB(Client, playerid, int, this.playerid);

    ATTRIB(Client, parm_idlesince, int, this.parm_idlesince);
    ATTRIB(Client, muted, bool, this.muted);
    ATTRIB(Client, idlekick_lasttimeleft, float, this.idlekick_lasttimeleft);
    ATTRIB(Client, pm_frametime, float, this.pm_frametime);
    ATTRIB(Client, pressedkeys, int, this.pressedkeys);
    ATTRIB(Client, movement_old, vector, this.movement_old);
    ATTRIB(Client, buttons_old, int, this.buttons_old);
    ATTRIB(Client, teamkill_complain, float, this.teamkill_complain);
    ATTRIB(Client, teamkill_soundtime, float, this.teamkill_soundtime);
    ATTRIB(Client, teamkill_soundsource, entity, this.teamkill_soundsource);
    ATTRIB(Client, usekeypressed, bool, this.usekeypressed);
    ATTRIB(Client, jointime, float, this.jointime);
    ATTRIB(Client, wants_join, int, this.wants_join);
    ATTRIB(Client, spectatortime, float, this.spectatortime);
    ATTRIB(Client, startplaytime, float, this.startplaytime);
    ATTRIB(Client, version_nagtime, float, this.version_nagtime);
    ATTRIB(Client, netname_previous, string, this.netname_previous);
    ATTRIB(Client, allowed_timeouts, int, this.allowed_timeouts);
    ATTRIB(Client, active_minigame, entity, this.active_minigame);
    ATTRIB(Client, taunt_soundtime, float, this.taunt_soundtime);
    ATTRIB(Client, killcount, int, this.killcount);
    ATTRIB(Client, version_mismatch, bool, this.version_mismatch);
    ATTRIB(Client, version, int, this.version);
    ATTRIB(Client, spectatee_status, int, this.spectatee_status);
    ATTRIB(Client, zoomstate, bool, this.zoomstate);
    ATTRIB(Client, just_joined, bool, this.just_joined);
    ATTRIB(Client, race_completed, bool, this.race_completed);
    ATTRIB(Client, latency_sum, float, this.latency_sum);
    ATTRIB(Client, latency_cnt, int, this.latency_cnt);
    ATTRIB(Client, latency_time, float, this.latency_time);
    ATTRIB(Client, v_angle_old, vector, this.v_angle_old);
    ATTRIB(Client, model_randomizer, float, this.model_randomizer);
    ATTRIB(Client, accuracy, entity, this.accuracy);
    ATTRIB(Client, hasweapon_complain_spam, float, this.hasweapon_complain_spam);
    ATTRIB(Client, scorekeeper, entity, this.scorekeeper);
    ATTRIB(Client, specialcommand_pos, int, this.specialcommand_pos);
    ATTRIB(Client, hitplotfh, int, this.hitplotfh);
    ATTRIB(Client, clientdata, entity, this.clientdata);
    ATTRIB(Client, cmd_floodtime, float, this.cmd_floodtime);
    ATTRIB(Client, weaponorder_byimpulse, string, this.weaponorder_byimpulse);
    ATTRIB(Client, autojoin_checked, int, this.autojoin_checked);

    // networked cvars

// not currently handled by ClientState
#if 0
    ATTRIBARRAY(Client, msg_choice_choices, int, 20); // TODO: actually NOTIF_CHOICE_MAX
    ATTRIB(Client, cvar_cl_allow_uid2name, int, this.cvar_cl_allow_uid2name);
    ATTRIB(Client, cvar_cl_allow_uidtracking, int, this.cvar_cl_allow_uidtracking);
    ATTRIB(Client, cvar_cl_autotaunt, float, this.cvar_cl_autotaunt);
    ATTRIB(Client, cvar_cl_voice_directional, int, this.cvar_cl_voice_directional);
    ATTRIB(Client, cvar_cl_voice_directional_taunt_attenuation, float, this.cvar_cl_voice_directional_taunt_attenuation);
    ATTRIB(Client, cvar_cl_physics, string, this.cvar_cl_physics);
    ATTRIB(Client, cvar_cl_buffs_autoreplace, bool, this.cvar_cl_buffs_autoreplace);
    ATTRIB(Client, cvar_cl_nade_type, string, this.cvar_cl_nade_type);
    ATTRIB(Client, cvar_cl_pokenade_type, string, this.cvar_cl_pokenade_type);
    ATTRIB(Client, cvar_cl_spawn_near_teammate, bool, this.cvar_cl_spawn_near_teammate);
    ATTRIB(Client, cvar_cl_gunalign, int, this.cvar_cl_gunalign);
    ATTRIB(Client, cvar_cl_handicap_damage_given, float, this.cvar_cl_handicap_damage_given);
    ATTRIB(Client, cvar_cl_handicap_damage_taken, float, this.cvar_cl_handicap_damage_taken);
    ATTRIB(Client, cvar_cl_clippedspectating, bool, this.cvar_cl_clippedspectating);
    ATTRIB(Client, cvar_cl_autoscreenshot, int, this.cvar_cl_autoscreenshot);
    ATTRIB(Client, cvar_cl_jetpack_jump, bool, this.cvar_cl_jetpack_jump);
    ATTRIB(Client, cvar_cl_noantilag, bool, this.cvar_cl_noantilag);
    ATTRIB(Client, cvar_cl_movement_track_canjump, bool, this.cvar_cl_movement_track_canjump);
    ATTRIB(Client, cvar_cl_weaponimpulsemode, int, this.cvar_cl_weaponimpulsemode);
    ATTRIB(Client, cvar_g_xonoticversion, string, this.cvar_g_xonoticversion);
    ATTRIB(Client, cvar_cl_autoswitch, bool, this.cvar_cl_autoswitch);
    ATTRIB(Client, cvar_cl_casings, bool, this.cvar_cl_casings);
    ATTRIB(Client, cvar_r_drawviewmodel, bool, this.cvar_r_drawviewmodel);
    ATTRIB(Client, cvar_cl_dodging_timeout, float, this.cvar_cl_dodging_timeout);
    ATTRIB(Client, cvar_cl_dodging, float, this.cvar_cl_dodging);
    ATTRIB(Client, cvar_cl_multijump, bool, this.cvar_cl_multijump);
    ATTRIB(Client, cvar_cl_accuracy_data_share, bool, this.cvar_cl_accuracy_data_share);
    ATTRIB(Client, cvar_cl_accuracy_data_receive, bool, this.cvar_cl_accuracy_data_receive);
    ATTRIBARRAY(Client, cvar_cl_weaponpriorities, string, 10);
    ATTRIB(Client, cvar_cl_weaponpriority, string, this.cvar_cl_weaponpriority);
    ATTRIB(Client, cvar_cl_autoswitch_cts, int, this.cvar_cl_autoswitch_cts);
    ATTRIB(Client, cvar_cl_weapon_switch_reload, bool, this.cvar_cl_weapon_switch_reload);
    ATTRIB(Client, cvar_cl_weapon_switch_fallback_to_impulse, bool, this.cvar_cl_weapon_switch_fallback_to_impulse);
#endif

    METHOD(Client, m_unwind, bool(Client this));

    STATIC_METHOD(Client, Add, void(Client this, int _team));
    STATIC_METHOD(Client, Remove, void(Client this));

    INIT(Client) {
        if (this.m_unwind(this)) return this;
        make_impure(this);
        this.classname = "player_joining";
        static int playerid_last;
        this.playerid = ++playerid_last;
        ClientState_attach(this);
    }
    DESTRUCTOR(Client) {
        Client_Remove(this);
    }
    CONSTRUCTOR(Client, string name) {
        CONSTRUCT(Client);
        this.netname = name;
        this.netaddress = "local";
        this.playermodel = cvar_defstring("sv_defaultplayermodel");
    }
ENDCLASS(Client)

CLASS(Observer, Client)
    INIT(Observer) {
        this.classname = STR_OBSERVER;
    }
    DESTRUCTOR(Observer) { }
ENDCLASS(Observer)

CLASS(Spectator, Client)
    INIT(Spectator) {
        this.classname = STR_SPECTATOR;
    }
    DESTRUCTOR(Spectator) { }
ENDCLASS(Spectator)

CLASS(Player, Client)

    // custom

    ATTRIB(Player, dual_weapons, vector, this.dual_weapons); // TODO: actually WepSet!
    ATTRIB(Player, itemkeys, int, this.itemkeys);
    ATTRIB(Player, ballistics_density, float, this.ballistics_density);

    INIT(Player) {
        this.classname = STR_PLAYER;
    }
    DESTRUCTOR(Player) {
    }
ENDCLASS(Player)

METHOD(Client, m_unwind, bool(Client this))
{
    TC(Client, this);
    #define UNWIND(class) MACRO_BEGIN if (this.instanceOf##class) { METHOD_REFERENCE(class, dtorimpl)(this); } MACRO_END
    switch (this.classname) {
        case "Observer":
            UNWIND(Spectator);
            UNWIND(Player);
            return true;
        case "Spectator":
            UNWIND(Observer);
            UNWIND(Player);
            return true;
        case "Player":
            UNWIND(Observer);
            UNWIND(Spectator);
            return true;
    }
    #undef UNWIND
    return false;
}

int autocvar__independent_players;
bool independent_players;
#define INDEPENDENT_PLAYERS (autocvar__independent_players ? (autocvar__independent_players > 0) : independent_players)
#define IS_INDEPENDENT_PLAYER(e) ((e).solid == SOLID_TRIGGER)
#define MAKE_INDEPENDENT_PLAYER(e) ((e).solid = SOLID_TRIGGER, (e).frags = FRAGS_PLAYER_NONSOLID)

.int killcount;

void SendWelcomeMessage(entity this, int msg_type);

// respawning
.int respawn_flags;
.float respawn_time;
.float respawn_time_max;

.float respawn_countdown; // next number to count

const int RESPAWN_FORCE = BIT(0);
const int RESPAWN_SILENT = BIT(1);
const int RESPAWN_DENY = BIT(2);

.float spectatortime; // point in time since the client is spectating or observing

.bool player_blocked;

const int SVC_SETVIEW = 5; // TODO: move to dpdefs where this belongs!

// TODO: standardise resource regeneration
.float pauseregen_finished;
.float pauserothealth_finished;
.float pauserotarmor_finished;
.float pauserotfuel_finished;

// g_<gametype>_str:
// If 0, default is used.
// If <0, 0 is used.
// Otherwise, g_str (default value) is used.
// For consistency, negative values there are mapped to zero too.
#define GAMETYPE_DEFAULTED_SETTING(str) \
    ((gametype_setting_tmp = cvar(strcat("g_", GetGametype(), "_" #str))), \
    (gametype_setting_tmp < 0) ? 0 \
    : (gametype_setting_tmp == 0 || autocvar_g_respawn_delay_forced) ? max(0, autocvar_g_##str) \
    : gametype_setting_tmp)

void calculate_player_respawn_time(entity this);

bool PlayerInList(entity player, string list);

bool PlayerInIDList(entity p, string idlist);

bool PlayerInIPList(entity p, string iplist);

void ClientData_Touch(entity e, bool to_spectators_too);

void PlayerUseKey(entity this);

void FixClientCvars(entity e);

// called when a client connects, useful for updating sounds and such of static objects
.void(entity this, entity player) init_for_player;

IntrusiveList g_initforplayer;
IntrusiveList g_observepoints;
STATIC_INIT(client_lists)
{
    g_initforplayer = IL_NEW();
    g_observepoints = IL_NEW();
}

void play_countdown(entity this, float finished, Sound samp);
void player_powerups_remove_all(entity this, bool allow_poweroff_sound);

// NOTE: current type is Resource (avoiding circular includes!)
void RotRegen(entity this, entity current, float limit_mod,
	float regenstable, float regenfactor, float regenlinear, float regenframetime,
	float rotstable, float rotfactor, float rotlinear, float rotframetime);

bool Spectate(entity this, entity pl);

void ClientInit_Spawn();

void PutObserverInServer(entity this, bool is_forced, bool use_spawnpoint);

void SetSpectatee(entity this, entity spectatee);
void SetSpectatee_status(entity this, int spectatee_num);

void FixPlayermodel(entity player);

void GiveWarmupResources(entity this);

void ClientInit_misc(entity this);

int GetPlayerLimit();

const int MIN_SPEC_TIME = 1;
void Join(entity this, bool queued_join);
int nJoinAllowed(entity this);
bool joinAllowed(entity this, int team_index);

void PlayerFrame (entity this);

#define SPECTATE_COPY() ACCUMULATE void SpectateCopy(entity this, entity spectatee)
#define SPECTATE_COPYFIELD(fld) SPECTATE_COPY() { this.(fld) = spectatee.(fld); }

const int MAX_SPECTATORS = 7;
