#include "spritesheet.h"
#include "assets.h"
#include "render.h"
#include "debug.h"
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_image.h>

struct Spritesheet {
    asset_id asset_id;

    // The texture to sample.
    SDL_Texture* texture;
    // The resolution of the texture.
    IVector resolution;
    // The number of tiles in the sheet.
    size_t tile_count;
    // The number of tiles contained in the width of the texture.
    size_t tile_shear;
    // The resolution of a single tile of the sheet.
    IVector tile_size;
};

impl_Drop_for(Spritesheet,
    _internal_spritesheet_destroy
)

impl_Asset_for(Spritesheet,
    spritesheet_get_asset_id,
    spritesheet_set_asset_id
)

void _internal_spritesheet_destroy(Spritesheet* self) {
    SDL_DestroyTexture(self->texture);
    free(self);
}

Spritesheet* spritesheet_load(const char* texture_name, IVector tile_size) {
    SDL_Texture* texture = IMG_LoadTexture(g_renderer, texture_name);
    ASSERT_RETURN(texture != NULL, NULL, "Failed to load texture from file %s.", texture_name);
    return spritesheet_from_texture(texture, tile_size);
}

Spritesheet* spritesheet_from_texture(SDL_Texture* texture, IVector tile_size) {
    Spritesheet* self = malloc(sizeof(Spritesheet));

    ASSERT_RETURN(self != NULL, NULL, "Failed to allocate spritesheet.");

    self->asset_id = store_asset(Spritesheet_as_Asset(self));

    // Load the texture image and query it's size
    self->texture = texture;
    SDL_QueryTexture(self->texture, NULL, NULL, &self->resolution.x, &self->resolution.y);

    self->tile_size = tile_size;
    self->tile_shear = (size_t)self->resolution.x / self->tile_size.x;
    self->tile_count = (size_t)self->resolution.x / self->tile_size.x * self->resolution.y / self->tile_size.y;
    return self;
}

void spritesheet_destroy(Spritesheet* self) {
    free_asset(self->asset_id);
}

SDL_Texture* spritesheet_get_texture(const Spritesheet* self) {
    return self->texture;
}

SDL_Rect spritesheet_get_tile_rect(const Spritesheet* self, size_t index) {
    IVector tile_coord = {(int)(index % self->tile_shear), (int)(index / self->tile_shear)};
    tile_coord = vmuli(tile_coord, self->tile_size);

    return (SDL_Rect) {
        tile_coord.x, tile_coord.y,
        self->tile_size.x, self->tile_size.y
    };
}

IVector spritesheet_get_resolution(const Spritesheet* self) {
    return self->resolution;
}

size_t spritesheet_get_tile_count(const Spritesheet* self) {
    return self->tile_count;
}

asset_id spritesheet_get_asset_id(Spritesheet* self) {
    return self->asset_id;
}

void spritesheet_set_asset_id(Spritesheet* self, asset_id id) {
    self->asset_id = id;
}
