#include <game/agame/background.hpp>
#include <window/window.hpp>

namespace Game::AGame {
    void Background::start() {
        mZIndex = -1; // Ensure background renders behind other entities
        mTex->setTiled(true); // Set the background texture to be tiled
        mTiledScale = 0.5f;
        int w, h;
        SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h);

        mTransform.scaleX *= 10.f;
        mTransform.scaleY *= 10.f;

        mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f;
        mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f;

        LOG("W: " << w << " H: " << h);

        mTransform.x = w / 2.f - (w / 3.f);
        mTransform.y = -h;

        mSound.~Sound();
    }

    void Background::update(float deltaTime) {
        if (!mIsActive) return;
        
        /*const bool* state = SDL_GetKeyboardState(nullptr);
        if (state[SDL_SCANCODE_P]) {
            mTransform.scaleX *= 2.f;
            mTransform.scaleY *= 2.f;
        }
        if (state[SDL_SCANCODE_L]) {
            mTransform.scaleX *= 0.5f;
            mTransform.scaleY *= 0.5f;
        }*/
        //mTransform.rotation += 1.f; // Rotate clockwise for testing
        //mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
        //mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing

        //Object::Camera::getInstance().move(1.f, 0.f);
    }

    void Background::onWindowResized(int newWidth, int newHeight) {
        // Re-center the background on window resize
        mTransform.x = newWidth / 2.f - (newWidth / 3.f);
        mTransform.y = -newHeight;
    }
}