#include "TileSet.hpp"
#include "Debug.hpp"
#include "Types.hpp"

#define PADDING 0.0f

TSE::TileSet::TileSet(Texture *tex, int x, int y)
{
    SetTexture(tex);
    SetCount(x,y);
}

void TSE::TileSet::SetCount(int x, int y)
{
    resx = x;
    resy = y;
}

void TSE::TileSet::SetTexture(Texture *tex)
{
    this->tex = tex;
}

void TSE::TileSet::GetSpriteAt(int x, int y, Sprite &s)
{
    if(x < 0 || x >= resx || y < 0 || y >= resy)
    {
        TSE_ERROR("The sprite you are trying to access is out of range");
        return;
    }

    Vector2 startpos = Vector2(((tex->size().x / resx) * (x + PADDING)) / tex->size().x, ((tex->size().y / resy) * (y + PADDING)) / tex->size().y);
    Vector2 endpos = Vector2(((tex->size().x / resx) * ((x + 1) - PADDING)) / tex->size().x, ((tex->size().y / resy) * ((y + 1) - PADDING)) / tex->size().y);

    s = Sprite(tex, Rect(startpos, endpos));
}

int TSE::TileSet::GetSpriteIdAt(int x, int y)
{
    if(x < 0 || x >= resx || y < 0 || y >= resy)
    {
        TSE_ERROR("The sprite you are trying to access is out of range");
        return -1;
    }

    return y * resx + x;
}

TSE::uint TSE::TileSet::GetTextueID()
{
    return tex->GetTextureId();
}

TSE::Vector2 TSE::TileSet::GetCount()
{
    return Vector2(resx, resy);
}
