#include "Camera.h"

glm::vec3 CameraOffset;

float MovementSpeed;
float MouseSensitivity;
float Zoom;

Camera::Camera(GLFWwindow* Window)
	:	GameObject(glm::vec3(0.0f), glm::vec3(0.0f), glm::vec3(0.0f)),
	Window(Window)
{
	
}

glm::mat4 Camera::GetViewMatrix()
{
	return glm::lookAt(Position, Position + Front, Up);
}

void Camera::SetOffset(glm::vec3 Offset)
{
	this->Offset = Offset;
}

void Camera::MoveCamera(glm::vec3 Position)
{
	this->Position = Position + Offset;
	UpdateVectors();
}

void Camera::RotateCamera(float Yaw, float Pitch, GLboolean bConstrainPitch)
{
	Rotation.y += Yaw;
	Rotation.x += Pitch;

	if (bConstrainPitch)
	{
		if (Rotation.x > 89.0f)
			Rotation.x = 89.0f;
		if (Rotation.x < -89.0f)
			Rotation.x = -89.0f;
	}

	UpdateVectors();
}

void Camera::ZoomCamera(float ZoomAmount)
{
	Zoom -= (float)ZoomAmount;
	if (Zoom < 1.0f)
		Zoom = 1.0f;
	if (Zoom > 45.0f)
		Zoom = 45.0f;
}

void Camera::RenderScene(
    LevelMap& LevelMap,
    const std::vector<PointLight>& Lights,
    int ScreenWidth,
    int ScreenHeight)
{
    // Set viewport
    glViewport(0, 0, ScreenWidth, ScreenHeight);

    // Clear buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Calculate matrices
    glm::mat4 Projection = GetProjectionMatrix(ScreenWidth, ScreenHeight);
    glm::mat4 View = GetViewMatrix();
    glm::mat4 Model = glm::mat4(1.0f);

    // Verify matrices are valid
    if (isnan(Projection[0].x) || isnan(View[0].x)) {
        std::cerr << "Invalid camera matrices!" << std::endl;
        return;
    }
}

glm::mat4 Camera::GetProjectionMatrix(int ScreenWidth, int ScreenHeight) {
	return glm::perspective(
		glm::radians(Zoom),
		(float)ScreenWidth / (float)ScreenHeight,
		0.1f,
		100.0f
	);
}