#pragma once
#include "main.h"
#include "physics.h"
#include "ai.h"
#include "outfit.h"
#include "faction.h"
#include "ship.h"

#define PLAYER_ID 1

// Aproximation for pilot size.
#define PILOT_SIZE_APROX      0.8
#define PILOT_DISABLED_ARMOUR 0.2 // Based on armour percentage.

// Flags.
#define pilot_isFlag(p,f)   (p->flags & f)
#define pilot_setFlag(p,f)  (p->flags |= f)
#define pilot_rmFlag(p,f)   (p->flags ^= f)
// Creation.
#define PILOT_PLAYER        (1<<0)  // Pilot is a player.
// Dynamic.
#define PILOT_HOSTILE       (1<<1)  // Pilot is hostile to the player.
#define PILOT_COMBAT        (1<<2)  // Pilot is engaged in combat.
#define PILOT_HYPERSPACE    (1<<3)  // Pilot is in hyperspace.
#define PILOT_DISABLED      (1<<4)  // Pilot is disabled.
// Just makes life simpler.
#define pilot_isPlayer(p)   ((p)->flags & PILOT_PLAYER)
#define pilot_isDisabled(p) ((p)->flags & PILOT_DISABLED)

typedef struct {
  Outfit* outfit; // Associated outfit.
  unsigned int quantity; // Number of outfits of this type that the pilot has.
  unsigned int timer; // Used to store last used weapon time.
} PilotOutfit;

// Primary pilot structure.
typedef struct Pilot {
  unsigned int id; // Pilots id.
  char* name;     // Pilot's name (if unique).

  Faction* faction;

  // Object characteristics.
  Ship* ship;     // Pilots ship.
  Solid* solid;   // Associated solid (physics).

  // Current health.
  double armour, shield, energy;
  double armour_max, shield_max, energy_max;

  void (*think)(struct Pilot*); // AI thinking for the pilot.
  void (*update)(struct Pilot*, const double); // Update the pilot.
  void (*render)(struct Pilot*); // Rendering the pilot.

  // Outfit management.
  PilotOutfit* outfits;
  int noutfits;
  PilotOutfit* secondary; // Secondary weapon.
  PilotOutfit* ammo;      // Secondary ammo (if needed).

  unsigned int flags; // Used for AI etc.

  // AI.
  AI_Profile* ai; // Ai personality profile.
  unsigned int tcontrol; // Timer for control tick.
  unsigned int timer[MAX_AI_TIMERS]; // Timers for AI.
  Task* task; // Current action.
} Pilot;

// Fleets.
typedef struct {
  Ship* ship; // Ship that the pilot is flying.
  char* name; // For special 'unique' names.
  int chance; // Chance of this pilot appearing in the fleet.
} FleetPilot;

typedef struct {
  char* name; // Fleet name, used as an identifier.
  Faction* faction; // Faction of the fleet.
  
  AI_Profile* ai; // A useable profile.

  FleetPilot* pilots; // The pilots in the fleet.
  int npilots; // Total number of pilots.
} Fleet;

// Grabing pilot crap.
extern Pilot* player; // The player.
Pilot* pilot_get(unsigned int id);
unsigned int pilot_getNext(const unsigned int id);
unsigned int pilot_getNearest(const Pilot* p);
unsigned int pilot_getHostile(void); // Only for the player.
Fleet* fleet_get(const char* name);

// MISC.
void pilot_shoot(Pilot* p, const unsigned int target, const int secondary);
void pilot_hit(Pilot* p, const double damage_shield, const double damage_armour);
void pilot_setAmmo(Pilot* p);

// Creation.
void pilot_init(Pilot* dest, Ship* ship, char* name, Faction* faction, AI_Profile* ai,
      const double dir, const Vec2* pos, const Vec2* vel, const int flags);

unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* ai,
      const double dir, const Vec2* pos, const Vec2* vel, const int flags);

// Init/Cleanup.
void pilot_destroy(Pilot* p);
void pilots_free(void);
int fleet_load(void);
void fleet_free(void);

// Update.
void pilots_update(double dt);
void pilots_render(void);