#include "collider.h"
#include "collision.h"
#include "debug.h"

struct Collider {
    Shape* shape;
    PhysicsEntity owner;
    RigidBody* body;
    PhysicsMask layers;
    int overlap;
};

Collider* collider_new(PhysicsEntity owner, Shape* shape, int overlap, PhysicsMask layers) {
    Collider* self = malloc(sizeof(Collider));
    ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for Collider");
    *self = (Collider) {
        .shape = shape,
        .owner = owner,
        .body = owner.tc->get_rigidbody(owner.data),
        .layers = layers,
        .overlap = overlap,
    };
    rigidbody_add_collider(self->body, self);
    return self;
}

void collider_destroy(Collider* self) {
    shape_destroy(self->shape);
    rigidbody_remove_collider(self->body, self);
    free(self);
}

PhysicsQuery collider_to_query(Collider* self) {
    return (PhysicsQuery) {
        .shape = self->shape,
        .transform = rigidbody_get_transform(self->body),
        .mask = self->layers
    };
}

Shape* collider_get_shape(Collider* self) {
    return self->shape;
}

RigidBody* collider_get_rigidbody(Collider* self) {
    return self->body;
}

int collider_is_overlap(Collider* self) {
    return self->overlap;
}

void collider_set_overlap(Collider* self, int value) {
    self->overlap = value;
}

PhysicsEntity collider_get_owner(Collider* self) {
    return self->owner;
}
