#include <Entity/PhysicsEntity/CameraEntity/CameraEntity.hpp>
#include <World/World.hpp>
#include <Vk/ShaderConst.hpp>

CameraEntity::CameraEntity(
    World &world,
    std::string typeId,
    uint64_t id,
    glm::dvec3 pos,
    glm::vec3 vel
) : PhysicsEntity(world, typeId, id, pos, vel),
    phi(0.0f),
    theta(0.0f)
{}

glm::mat4 CameraEntity::getCameraTransform()
{
    return glm::rotate(glm::mat4(1.0f), this->phi, glm::vec3(1.0f, 0.0f, 0.0f)) *
        glm::rotate(glm::mat4(1.0f), this->theta, glm::vec3(0.0f, 1.0f, 0.0f)) *
        glm::lookAt(
            glm::vec3(0.0f),
            glm::vec3(0.0f, 1.0f, 0.0f),
            glm::vec3(0.0f, 0.0f, 1.0f)
    );
}

glm::mat4 CameraEntity::getSkyboxTransform()
{
    const float solarOffset = 0.25f;

    return glm::rotate(glm::mat4(1.0f), world->getSolarAngle(), glm::vec3(
        -std::sin(solarOffset),
        -std::cos(solarOffset),
        0.0f
    )) * glm::rotate(glm::mat4(1.0f), 1.5708f, glm::vec3(0.0f, 0.0f, -1.0f));
}

Side CameraEntity::getSideFacing2D()
{
    static int sideForOrientation[] = {2, 0, 3, 1};
    int orientation = (int) std::floor(this->theta * 2.0f / 3.1415f + 0.5f) & 3;

    return Side(sideForOrientation[orientation]);
}

bool CameraEntity::isUsingItemDirectly()
{
    return false;
}