#include <object/entity.hpp>
#include <renderer/renderer.hpp>
#include <renderer/texture.hpp>
#include <object/camera.hpp>
#include <window/window.hpp>
#include <cmath>

namespace Game::Object {
    Entity::~Entity() = default;

    Entity::Entity(const Entity& other) : mName(other.mName), mTex(other.mTex), mTransform(other.mTransform), mIsActive(other.mIsActive) {
        LOG("Copied Entity: " << mName);
    }
    
    Entity& Entity::operator=(const Entity& other) {
        if (this != &other) {
            mName = other.mName;
            mTex = other.mTex;
            mTransform = other.mTransform;
            mIsActive = other.mIsActive;
        }
        return *this;
    }

    Entity::Entity(Entity&& other) noexcept : mName(std::move(other.mName)), mTex(other.mTex), mTransform(other.mTransform), mIsActive(other.mIsActive) {
        other.mTex = nullptr;
        LOG("Moved Entity: " << mName);
    }

    Entity& Entity::operator=(Entity&& other) noexcept {
        if (this != &other) {
            mName = std::move(other.mName);
            mTex = other.mTex;
            mTransform = other.mTransform;
            mIsActive = other.mIsActive;
            other.mTex = nullptr;
        }
        return *this;
    }

    void Entity::render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config) {
        if (!mIsActive || !mTex) return; // Don't render if not active or if there's no texture

        if (!mTex->isTiled()) {
            float w, h;
            SDL_GetTextureSize(mTex->getSDLTexture(), &w, &h);

            SDL_FRect dst;
            dst.w = w * mTransform.scaleX * UNIVERSAL_SCALE_COEFFICIENT; // 1.f is HUGE, so this is just a constant to make the default scale more reasonable
            dst.h = h * mTransform.scaleY * UNIVERSAL_SCALE_COEFFICIENT;

            // Top-left origin; Account for camera position (center the camera on the screen)
            dst.x = mTransform.x - config.camX + config.screenW / 2.f;
            dst.y = mTransform.y - config.camY + config.screenH / 2.f;

            SDL_FPoint center;
            center.x = dst.w / 2.f;
            center.y = dst.h / 2.f;

            SDL_RenderTextureRotated(
                renderer->getSDLRenderer(),
                mTex->getSDLTexture(),
                nullptr,
                &dst,
                mTransform.rotation,
                &center,
                mIsFlipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE
            );
        } else {
            // Tiled rendering - render the texture repeatedly to fill the area defined by the entity's transform
            // We assume that we always render scaleX by scaleY tiles, and that the texture should be rendered at its original size (i.e., the scaleX and scaleY of the entity only affect how many times the texture is tiled, not the size of each tile)
            float tileW, tileH;
            SDL_GetTextureSize(mTex->getSDLTexture(), &tileW, &tileH);

            SDL_FRect dst;
            dst.w = tileW * mTiledScale; // Tile size is the original texture size multiplied by the universal scale coefficient (ignoring the entity's scale, since that only affects how many times the texture is tiled, not the size of each tile)
            dst.h = tileH * mTiledScale;

            // Top-left origin; Account for camera position (center the camera on the screen)
            dst.x = mTransform.x - config.camX + config.screenW / 2.f;
            dst.y = mTransform.y - config.camY + config.screenH / 2.f;

            for (int i = 0; i < mTransform.scaleX; i++) {
                for (int j = 0; j < mTransform.scaleY; j++) {
                    SDL_RenderTextureRotated(
                        renderer->getSDLRenderer(),
                        mTex->getSDLTexture(),
                        nullptr,
                        &dst,
                        mTransform.rotation,
                        nullptr, // No rotation center since each tile is rendered independently
                        mIsFlipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE
                    );
                    dst.y += dst.h; // Move down for the next tile in the column
                }
                dst.y = mTransform.y - config.camY + config.screenH / 2.f; // Reset y to the top of the column
                dst.x += dst.w; // Move right for the next column of tiles
            }
        }
    }
}