#include "client/render/Camera.h"

#include "blitz/game/Player.h"
#include "blitz/misc/Maths.h"
#include "imgui.h"

namespace blitz {
namespace render {

static const float eyeHeight = 1.25f;

void Camera::Update(float delta) {
	int windowWidth = ImGui::GetIO().DisplaySize.x;
	int windowHeight = ImGui::GetIO().DisplaySize.y;

	if (windowWidth != m_LastWindowSize.x || windowHeight != m_LastWindowSize.y) {
		m_LastWindowSize = {windowWidth, windowHeight};

		m_PerspectiveMatrix =
			maths::Perspective(80.0f / 180.0f * maths::PI, static_cast<float>(windowWidth) / windowHeight, 0.1f, 160.0f);
	}
}

Mat4f Camera::GetViewMatrix() const {
	Vec3f front = {
		std::cos(m_Player->GetYaw()) * std::cos(m_Player->GetPitch()),
		std::sin(m_Player->GetPitch()),
		std::sin(m_Player->GetYaw()) * std::cos(m_Player->GetPitch()),
	};

	return maths::Look(m_Player->GetPosition() + Vec3f{0, eyeHeight, 0}, front, {0, 1, 0});
}

}  // namespace render
}  // namespace blitz
