#include "../utils.h"
#include "../factory_objects.h"
#include "../resources.h"
#include "../camera.h"
#include "../player.h"
#include "../scenes.h"

typedef struct actor_spawn_s
{
    object_t *self_object;
    sprite_t *sprite;
    vector_t  position;
    bool      go_nextlevel;
} actor_spawn_t;

static void _Actor_Spawn_CreateAnimations(actor_spawn_t *actor)
{
    Sprite_AnimationAdd(actor->sprite, "idle", 12);
    Sprite_AnimationAddFrame(actor->sprite, "idle", Utils_GetRect(0,  0, 24, 24));
    Sprite_AnimationAddFrame(actor->sprite, "idle", Utils_GetRect(24, 0, 24, 24));
    Sprite_AnimationAddFrame(actor->sprite, "idle", Utils_GetRect(48, 0, 24, 24));
    Sprite_AnimationAddFrame(actor->sprite, "idle", Utils_GetRect(72, 0, 24, 24));
}

static void _Actor_Spawn_GetInfo(actor_spawn_t *actor, object_info_t *info)
{
    if (actor == NULL)
        return;

    if (info == NULL)
        return;

    info->box_rect       = Utils_GetRect(0, 0, 72, 72);
    info->collision_rect = Utils_GetRect(0, 0, 0, 0);
    info->position       = actor->position;

    if (actor->sprite != NULL)
    {
        info->box_rect.x = actor->position.x - actor->sprite->origin.x;
        info->box_rect.y = actor->position.y - actor->sprite->origin.y;

        info->collision_rect.x = actor->position.x;
        info->collision_rect.y = actor->position.y;
    }

    info->collision_cell_ul = Utils_GetPoint(0, 0);
    info->collision_cell_br = Utils_GetPoint(0, 0);
}

static void _Actor_Spawn_Update(actor_spawn_t *actor, float dt)
{
    if (actor == NULL)
        return;

    if (Sprite_IsEndCurrentAnimation(actor->sprite))
    {
        if (!actor->go_nextlevel)
        {
            object_t     *object = FactoryObjects_Create();
            actor_robo_t *robo   = Actor_Robo_Create(object, actor->position.x+33, actor->position.y+33);
            Player_BindRobo(robo);
        }
        else Scene_Game_NextLevel();

        FactoryObjects_SignRemove(actor->self_object);
    }

    Sprite_Update(actor->sprite);
}

static void _Actor_Spawn_Draw(SDL_Renderer *renderer, actor_spawn_t *actor)
{
    if (actor == NULL)
        return;

    if (actor->sprite != NULL)
        Sprite_Draw(renderer, actor->sprite, actor->position);
}

static void _Actor_Spawn_Free(actor_spawn_t *actor)
{
    if (actor == NULL)
        return;

    Sprite_Free(actor->sprite);

    free(actor);

    actor = NULL;
}

void Actor_Spawn_Create(object_t *object, float x, float y, bool go_nextlevel)
{
    if (object != NULL)
    {
        actor_spawn_t *actor = (actor_spawn_t*) malloc(sizeof(actor_spawn_t));

        actor->self_object = object;

        FactoryObjects_SetActor(object, (void*) actor, AT_SPAWN, OLAY_SPAWN);

        FactoryObjects_SetFunc(object, _Actor_Spawn_GetInfo, OFUNC_GETINFO);
        FactoryObjects_SetFunc(object, _Actor_Spawn_Update,  OFUNC_UPDATE);
        FactoryObjects_SetFunc(object, _Actor_Spawn_Draw,    OFUNC_DRAW);
        FactoryObjects_SetFunc(object, _Actor_Spawn_Free,    OFUNC_FREE);

        actor->sprite       = Sprite_Create(Utils_GetPoint(0, 0), 72, 72, spawnTex);
        actor->position     = Utils_GetVector(x, y);
        actor->go_nextlevel = go_nextlevel;

        _Actor_Spawn_CreateAnimations(actor);

        Sprite_AnimationPlay(actor->sprite, "idle", true);
    }
}
