#include <iostream>

#include "client/rendering/wrapper/Window.h"

Window::Window(const Size& size, const char* windowName) : window(nullptr) {
    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, 0);
    glfwWindowHint(GLFW_RESIZABLE, 1);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    window = glfwCreateWindow(size.width, size.height, windowName, nullptr, nullptr);
    if(window == nullptr) {
        std::cout << "could not create window\n";
        return;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glfwSetWindowUserPointer(window, this);
}

Window::~Window() {
    if(window != nullptr) {
        glfwDestroyWindow(window);
    }
}

bool Window::hasError() const {
    return window == nullptr;
}

void Window::show() {
    glfwShowWindow(window);
}

bool Window::shouldClose() const {
    return glfwWindowShouldClose(window);
}

void Window::swapBuffers() {
    glfwSwapBuffers(window);
}

void Window::setFramebufferSizeCallback(GLFWframebuffersizefun f) {
    glfwSetFramebufferSizeCallback(window, f);
}

void Window::setKeyCallback(GLFWkeyfun f) {
    glfwSetKeyCallback(window, f);
}

void Window::setMouseButtonCallback(GLFWmousebuttonfun f) {
    glfwSetMouseButtonCallback(window, f);
}

void Window::setCursorPosCallback(GLFWcursorposfun f) {
    glfwSetCursorPosCallback(window, f);
}