#ifndef UNIT_H
#define UNIT_H

#include <camera.h>
#include <gfx.h>
#include <sfx.h>
#include <instance.h>
#include <unit_type.h>
#include <util.h>
#include <fixmath.h>
#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C"
{
#endif

enum unit_state
{
    UNIT_STATE_IDLE_MOVING
};

struct unit_target
{
    struct instance *ins;
    enum unit_state state;
    instance_done_cb done;
    void *op;
};

struct unit
{
    struct instance instance;
    enum unit_type type;
    const char *name;

    enum unit_dir
    {
        UNIT_DIR_W,
        UNIT_DIR_S,
        UNIT_DIR_N,
        UNIT_DIR_E
    } dir;

    enum unit_state state;

    struct
    {
        unsigned char t, i;
    } frame;

    fix16_t rx, ry, tx, ty;
    struct unit_target target;
};

UTIL_STATIC_ASSERT(!offsetof(struct unit, instance), "must be at offset zero");

struct unit_cfg
{
    enum
    {
        UNIT_CFG_TYPE_WALKER,
        UNIT_CFG_TYPE_CAR
    } type;

    unsigned long x, y;
};

void unit_create(const struct unit_cfg *cfg, struct unit *u);
int unit_render(const struct unit *u, const struct camera *cam, bool sel);
bool unit_can_harvest(const struct unit *u);
bool unit_target_valid(const struct unit *u, const struct unit_target *t);
void unit_set_target(struct unit *u, const struct unit_target *t);
void unit_move_to(struct unit *u, unsigned long x, unsigned long y);
void unit_update(struct unit *u);

enum
{
    UNIT_1_SPRITE,
    UNIT_2_SPRITE,
    UNIT_3_SPRITE,
    UNIT_4_SPRITE,
    UNIT_5_SPRITE,
    UNIT_6_SPRITE,
    UNIT_CAR_1_SPRITE,
    UNIT_CAR_2_SPRITE,

    MAX_UNIT_SPRITES
};

extern struct sprite unit_sprites[MAX_UNIT_SPRITES];

enum unit_sound
{
    UNIT_SOUND_SELECTED,
    UNIT_SOUND_MOVE,
    UNIT_SOUND_MOVE_2,

    MAX_UNIT_SOUNDS
};

extern struct sound unit_sounds[MAX_UNIT_SOUNDS];

#ifdef __cplusplus
}
#endif

#endif /* UNIT_H */
