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

////////////////////////////////////////////////////////
//                                                    //
//   Composant hitbox, contient toutes les données    //
// necessaires pour gérer les collision efficacement. //
//                                                    //
////////////////////////////////////////////////////////

#ifndef HITBOX_HPP
#define HITBOX_HPP

#include "ECS.hpp"
#include "TransformComponent.hpp"
#include "../Vector2D.hpp"

#ifdef DEBUG_MODE
    #include "../Game.hpp"
#endif

class HitboxComponent : public Component {
  public:
    HitboxComponent() = default;

    HitboxComponent(Vector2D<float> sc, Vector2D<float> pos) : position(pos), scale(sc)
    {}

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

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

#ifdef DEBUG_MODE
    void draw() override
    {
        if(hitboxActivated)
        {
            SDL_SetRenderDrawColor(Game::renderer, 20, 200, 18, 255);
            SDL_RenderDrawRect(Game::renderer, &hitboxR);
            SDL_SetRenderDrawColor(Game::renderer, 20, 20, 18, 255);
        }
    }
#endif

    Vector2D<float> position{Vector2D<float>(1.0f, 1.0f)}; // Dans l'entitée (0 -> 1) 0=>tout a gauche de l'entitée, 1=>tout a droite et pareil pour le Y
    Vector2D<float> scale{Vector2D<float>(1.0f, 1.0f)}; // Par rapport à la taille initiale (0 -> 1)
    SDL_Rect hitboxR;

    bool hitboxActivated {true};

  private:
    TransformComponent *m_transform;
};

#endif
