#include <game/agame/trash.hpp>
#include <game/agame/player.hpp>
#include <object/components/boxcollider.hpp>
#include <algorithm>

namespace Game::AGame {
    void Trash::start() {
        mZIndex = 20;
        addComponent<Object::Components::BoxCollider>();
    }

    void Trash::update(float deltaTime) {
        (void)deltaTime;
        if (mSeaOnly) {
            const float landBoundaryX = GameManager::getSharedData<float>("terrainLandBoundaryX");
            const float margin = 25.f;
            const float halfWidth = getTexture() ? getTexture()->getWidth() * mTransform.adjustedScaleX() / 2.f : 0.f;
            if (mTransform.x - halfWidth < landBoundaryX + margin) {
                mTransform.x = landBoundaryX + margin + halfWidth;
            }
        }
        return;
    }

    void Trash::onCollisionEnter(Object::Entity* other) {
        auto* player = dynamic_cast<Player*>(other);
        if (!player || !player->isShipMode()) {
            return;
        }

        GameManager::setSharedData("trashActiveCount", std::max(0, GameManager::getSharedData<int>("trashActiveCount") - 1));
        GameManager::setSharedData("gameScore", GameManager::getSharedData<int>("gameScore") + 25);
        GameManager::destroyEntity(this);
    }
}