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

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

Collider* collider_new(PhysicsEntity owner, Shape* shape, int overlap, PhysicsMask layers, PhysicsMask mask) {
    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,
        .mask = mask,
        .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->mask
    };
}

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;
}

PhysicsMask collider_get_mask(const Collider* self) {
    return self->mask;
}

void collider_set_mask(Collider* self, PhysicsMask mask) {
    self->mask = mask;
}

PhysicsMask collider_get_layers(const Collider* self) {
    return self->layers;
}

void collider_set_layers(Collider* self, PhysicsMask layers) {
    self->layers = layers;
}

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