#ifndef JNR_SPACECRAFT_H
#define JNR_SPACECRAFT_H

#include "engine/entity.h"
#include "engine/animation.h"

/* ═══════════════════════════════════════════════════
 *  Spacecraft
 *
 *  The player's ship. Appears at level start (fly-in
 *  and landing sequence) and at exit zones (landing
 *  and fly-out when the player reaches the exit).
 *
 *  Spritesheet: assets/sprites/spacecraft.png
 *  Layout: 5 frames at 80x48, horizontal strip
 *    0 = landed/idle
 *    1 = landing frame 1 (high, bright thrusters)
 *    2 = landing frame 2 (mid, medium thrusters)
 *    3 = landing frame 3 (low, dim thrusters)
 *    4 = landing frame 4 (touchdown)
 * ═══════════════════════════════════════════════════ */

/* Source sprite dimensions (pixels in PNG) */
#define SPACECRAFT_SRC_W   80
#define SPACECRAFT_SRC_H   48

/* Render scale — drawn larger than the source art */
#define SPACECRAFT_SCALE   2.0f

/* Rendered dimensions (used for hitbox/positioning) */
#define SPACECRAFT_WIDTH   ((int)(SPACECRAFT_SRC_W * SPACECRAFT_SCALE))
#define SPACECRAFT_HEIGHT  ((int)(SPACECRAFT_SRC_H * SPACECRAFT_SCALE))

/* State machine */
typedef enum SpacecraftState {
    SC_FLYING_IN,      /* approaching from off-screen        */
    SC_LANDING,        /* playing landing animation          */
    SC_LANDED,         /* on the ground, idle                */
    SC_TAKEOFF,        /* playing takeoff animation (reverse landing) */
    SC_FLYING_OUT,     /* departing off-screen               */
    SC_DONE            /* sequence complete, can be removed  */
} SpacecraftState;

typedef struct SpacecraftData {
    SpacecraftState state;
    float           state_timer;      /* time spent in current state   */
    Vec2            target_pos;       /* where to land (ground level)  */
    Vec2            start_pos;        /* fly-in origin (off-screen)    */
    float           fly_speed;        /* pixels/s during fly in/out    */
    bool            is_exit_ship;     /* true = exit ship, false = intro ship */
    int             engine_channel;   /* SDL_mixer channel for engine loop, -1 = none */
} SpacecraftData;

/* Register the spacecraft entity type */
void spacecraft_register(EntityManager *em);

/* Spawn a spacecraft that will fly in and land at the given position.
 * The position is the top-left of where the ship should rest (from .lvl ENTITY). */
Entity *spacecraft_spawn(EntityManager *em, Vec2 land_pos);

/* Spawn an exit spacecraft that flies in from the right and lands at the
 * given position.  Used for the level-exit sequence.  The is_exit_ship flag
 * is set to true so level code can identify this ship. */
Entity *spacecraft_spawn_exit(EntityManager *em, Vec2 land_pos);

/* Trigger takeoff on an existing landed spacecraft */
void spacecraft_takeoff(Entity *ship);

/* Check if the spacecraft has finished its full sequence */
bool spacecraft_is_done(const Entity *ship);

/* Check if the spacecraft is in the landed state */
bool spacecraft_is_landed(const Entity *ship);

/* Check if this is the exit ship */
bool spacecraft_is_exit_ship(const Entity *ship);

#endif /* JNR_SPACECRAFT_H */
