#include "Image.hpp"

#include "Renderable.hpp"
#include "MeshContainer.hpp"
#include "Debug.hpp"
#include "elements/Transformable.hpp"

TSE::Image::Image()
{
}

void TSE::Image::SetSprite(Sprite &s)
{
    if(!baseObject->HasBehaviourScript(RENDERABLE) || !baseObject->HasBehaviourScript(MESH_CONTAINER))
    {
        TSE_WARNING("Dependencies not found for Image BehaviourScript.");
        return;
    }
    if(&s == nullptr) return;
    spr = s;
    sprPtr = &spr;
    Renderable* r = (Renderable*)baseObject->GetBehaviourScript(RENDERABLE);
    r->GetMaterial()->SetValue<ITexture*>("mainTex", s.GetTexture());
    MeshContainer* mc = (MeshContainer*)baseObject->GetBehaviourScript(MESH_CONTAINER);
    s.GetUVs(mc->GetMesh()->uvs);
    lastrect = s.GetUVRect();
}

TSE::Sprite &TSE::Image::GetSprite()
{
    return spr;
}

TSE::Sprite *TSE::Image::GetSpritePtr()
{
    return sprPtr;
}

void TSE::Image::OnUpdate()
{
    if(lastrect.p1 != spr.GetUVRect().p1 || lastrect.p2 != spr.GetUVRect().p2)
    {
        lastrect = spr.GetUVRect();
        MeshContainer* mc = (MeshContainer*)baseObject->GetBehaviourScript(MESH_CONTAINER);
        spr.GetUVs(mc->GetMesh()->uvs);
    }
}
