#pragma once
#ifndef fps_H
#define fps_H
#include <GLFW/glfw3.h>
#include "include/rtrTextRendering.h"
#include <glm/glm.hpp> // GLM is a header only library for math in OpenGL
#include <glm/gtc/matrix_transform.hpp>

namespace fps
{
	class FrameCounter
	{
		private:
			rtrText::freeTypeFont font = rtrText::freeTypeFont("fonts\\arial.ttf", 20);
			rtrShader::Shader textShader = rtrShader::Shader("shaders\\text.vert", "shaders\\text.frag");

		public:
			int counterForFPS = 0;
			int counter = 0;
			float fpsCheckpoint = 0;
			int fps = 0;
			float lastTimeStamp = 0;
			float currentTimeStamp = 0;
			float passedTimeSinceLastFrame = 0;
			glm::vec2 resolution;
			bool active = true;
			glm::vec3 textColor = glm::vec3(0, 1, 0);

			FrameCounter(glm::vec2 resolutionInput): resolution(resolutionInput)
			{
			}

			void update()
			{
				lastTimeStamp = currentTimeStamp;
				currentTimeStamp = (float)glfwGetTime();
				passedTimeSinceLastFrame = currentTimeStamp - lastTimeStamp;
				counter++;
				counterForFPS++;
				if ((float)glfwGetTime() - fpsCheckpoint > 1.0)
				{
					fpsCheckpoint = (float)glfwGetTime();
					fps = (int)counterForFPS;
					counterForFPS = 0;
				}
			}

			void draw()
			{
				if (active)
				{
					textShader.use();
					glDisable(GL_DEPTH_TEST);
					glm::vec2 pos(0.001 * resolution.x, 0.001 * resolution.y);
					font.render(textShader, "FPS: " + std::to_string(fps), pos.x, pos.y, 1, textColor, resolution.x, resolution.y);
					glEnable(GL_DEPTH_TEST);
				}
			}

	};
}
#endif