#include "../utils.h"
#include "../factory_objects.h"
#include "../resources.h"
#include "../level.h"
#include "../camera.h"

typedef struct actor_cloud_s
{
    object_t *self_object;
    sprite_t *sprite;
    vector_t  position;
} actor_cloud_t;

static void _Actor_Cloud_GetInfo(actor_cloud_t *actor, object_info_t *info)
{
    if (actor == NULL)
        return;

    if (info == NULL)
        return;

    info->box_rect       = Utils_GetRect(0, 0, 0, 0);
    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->box_rect.w = actor->sprite->width;
        info->box_rect.h = actor->sprite->height;

        info->collision_rect.x = actor->position.x;
        info->collision_rect.y = actor->position.y;
        info->collision_rect.w = actor->sprite->width;
        info->collision_rect.h = actor->sprite->height;
    }

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

static void _Actor_Cloud_Update(actor_cloud_t *actor, float dt)
{
    if (actor == NULL)
        return;

    Sprite_Update(actor->sprite);

    actor->position.x -= 0.1f;

    if (actor->position.x < -actor->sprite->width)
        actor->position.x = Level_GetWidth();
}

static void _Actor_Cloud_Draw(SDL_Renderer *renderer, actor_cloud_t *actor)
{
    if (actor == NULL)
        return;

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

static void _Actor_Cloud_Free(actor_cloud_t *actor)
{
    if (actor == NULL)
        return;

    Sprite_Free(actor->sprite);

    free(actor);

    actor = NULL;
}

void Actor_Cloud_Create(object_t *object, float x, float y)
{
    if (object != NULL)
    {
        actor_cloud_t *actor = (actor_cloud_t*) malloc(sizeof(actor_cloud_t));

        actor->self_object = object;

        FactoryObjects_SetActor(object, (void*) actor, AT_CLOUD, OLAY_CLOUDS);

        FactoryObjects_SetFunc(object, _Actor_Cloud_GetInfo, OFUNC_GETINFO);
        FactoryObjects_SetFunc(object, _Actor_Cloud_Update,  OFUNC_UPDATE);
        FactoryObjects_SetFunc(object, _Actor_Cloud_Draw,    OFUNC_DRAW);
        FactoryObjects_SetFunc(object, _Actor_Cloud_Free,    OFUNC_FREE);

        actor->sprite   = Sprite_Create(Utils_GetPoint(0, 0), 87, 42, cloudTex);
        actor->position = Utils_GetVector(x, y);
    }
}
