/********************\
|  Copyright 2024,   |
|       Ulysse Cura  |
\********************/

////////////////////////////////////////////
//                                        //
//   Composant sprite, gère ce qu'il faut //
// pour afficher le sprite.               //
//                                        //
////////////////////////////////////////////

#ifndef SPRITE_HPP
#define SPRITE_HPP

#include <string>
#include "ECS.hpp"
#include "TransformComponent.hpp"
#include "../Game.hpp"
#include "../TextureManager.hpp"

using std::string;

class SpriteComponent : public Component {
  public:
    SpriteComponent(const string &path)
    {
        setTexture(path);
    }

    void init() override
    {
        m_transform = &entity->getComponent<TransformComponent>();

        m_srcR.x = m_srcR.y = 0;
        m_srcR.w = m_transform->dimension.x;
        m_srcR.h = m_transform->dimension.y;
    }

    void update() override
    {
        m_dstR.x = static_cast<int>(m_transform->position.x);
        m_dstR.y = static_cast<int>(m_transform->position.y);
        m_dstR.w = m_transform->dimension.x * m_transform->scale;
        m_dstR.h = m_transform->dimension.y * m_transform->scale;
    }

    void draw() override
    {
        Game::textureManager.Draw(m_texture, m_srcR, m_dstR, flipSprite);
    }

    void setTexture(const string &path)
    {
        m_texture = Game::textureManager.LoadTexture(path);
    }

    SDL_Rect getSrcRect()
    {
        return m_srcR;
    }

    void setSrcRect(SDL_Rect srcR)
    {
        m_srcR = srcR;
    }

    bool flipSprite {false};

  private:
    TransformComponent *m_transform;
    SDL_Texture *m_texture;
    SDL_Rect m_srcR, m_dstR;
};

#endif
