#include "OpenGLRenderAPI.h"
#include "Plattform/OpenGL/OpenGLBuffer.h"
#include "DeerRender/Render/VertexArray.h"
#include "Deer/Application.h"

#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_glfw.h"

namespace Deer {
	void OpenGLRenderAPI::init() {
		//glEnable(GL_BLEND);
		glEnable(GL_DEPTH_TEST);

		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
		glFrontFace(GL_CW);

		glDepthFunc(GL_LESS);
		glfwWindowHint(GLFW_SAMPLES, 4);
		glEnable(GL_MULTISAMPLE);

		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable( GL_BLEND );
		
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	}

	void OpenGLRenderAPI::clearColor(const glm::vec4& color) {
		glClearColor(color.r, color.g, color.b, color.a);
	}

	void OpenGLRenderAPI::setDepthBuffer(bool enable) {
		if (enable)
			glEnable(GL_DEPTH_TEST);
		else
			glDisable(GL_DEPTH_TEST);
	}

	void OpenGLRenderAPI::clear() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}

	void OpenGLRenderAPI::setBackfaceCulling(bool value) {
		if (value)
			glEnable(GL_CULL_FACE);
		else
			glDisable(GL_CULL_FACE);
	}

	void OpenGLRenderAPI::drawIndex(VertexArray& vertexArray) {
		const Ref<IndexBuffer>& ib = vertexArray.getIndexBuffer();
		glDrawElements(GL_TRIANGLES, ib->getCount(), getOpenGLIndexDataType(ib->getIndexDataType()), nullptr);
	}

	void OpenGLRenderAPI::drawLines(VertexArray& vertexArray) {
		const Ref<IndexBuffer>& ib = vertexArray.getIndexBuffer();
		glDrawElements(GL_LINES, ib->getCount(), getOpenGLIndexDataType(ib->getIndexDataType()), nullptr);
	}

	void OpenGLRenderAPI::unbindFrameBuffer() {
		unsigned int width = Application::s_application->m_window->getWitdth();
		unsigned int height = Application::s_application->m_window->getHeight();
		glViewport(0, 0, width, height);
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
	}
}