#include <game/agame/friendly.hpp>
#include <game/gamemanager.hpp>
#include <game/agame/player.hpp>
#include <object/components/boxcollider.hpp>
#include <algorithm>
#include <cmath>
#include <utils.hpp>
#include <window/window.hpp>

namespace Game::AGame {
    void Friendly::start() {
        mZIndex = 20;
        addComponent<Object::Components::BoxCollider>();
        LOG("Zaveznik zagnan: " << getName());
        
        // Initialize random movement
        const float angle = static_cast<float>(Utils::getUtils().rirng32(0, 360)) * 3.14159f / 180.f;
        const float speed = 20.f + static_cast<float>(Utils::getUtils().rirng32(0, 30));
        mMoveSpeedX = std::cos(angle) * speed;
        mMoveSpeedY = std::sin(angle) * speed;
        mDirectionChangeTimer = 0.f;
    }

    void Friendly::update(float deltaTime) {
        (void)deltaTime;
        
        // Semi-random movement with periodic direction changes
        mDirectionChangeTimer += deltaTime;
        if (mDirectionChangeTimer > 2.0f) {
            const float angle = static_cast<float>(Utils::getUtils().rirng32(0, 360)) * 3.14159f / 180.f;
            const float speed = 20.f + static_cast<float>(Utils::getUtils().rirng32(0, 30));
            mMoveSpeedX = std::cos(angle) * speed;
            mMoveSpeedY = std::sin(angle) * speed;
            mDirectionChangeTimer = 0.f;
        }
        
        // Move friendly
        mTransform.x += mMoveSpeedX * deltaTime;
        mTransform.y += mMoveSpeedY * deltaTime;
        
        // Clamp to land section
        const float landBoundaryX = GameManager::getSharedData<float>("terrainLandBoundaryX");
        const float entityWidth = getTexture() ? getTexture()->getWidth() * mTransform.adjustedScaleX() : 0.f;
        const float entityHeight = getTexture() ? getTexture()->getHeight() * mTransform.adjustedScaleY() : 0.f;
        const float halfWidth = entityWidth / 2.f;
        const float halfHeight = entityHeight / 2.f;
        
        // Get window dimensions for boundary calculations
        int w = 0, h = 0;
        SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h);
        const float leftEdge = -w / 2.f + 25.f;
        
        if (mTransform.x - halfWidth < leftEdge) {
            mTransform.x = leftEdge + halfWidth;
            mMoveSpeedX = std::abs(mMoveSpeedX);
        }
        if (mTransform.x + halfWidth > landBoundaryX - 25.f) {
            mTransform.x = landBoundaryX - 25.f - halfWidth;
            mMoveSpeedX = -std::abs(mMoveSpeedX);
        }
        if (mTransform.y - halfHeight < -h / 2.f + 25.f) {
            mTransform.y = -h / 2.f + 25.f + halfHeight;
            mMoveSpeedY = std::abs(mMoveSpeedY);
        }
        if (mTransform.y + halfHeight > h / 2.f - 25.f) {
            mTransform.y = h / 2.f - 25.f - halfHeight;
            mMoveSpeedY = -std::abs(mMoveSpeedY);
        }
    }

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

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