#include <windows.h>
#include "../deps/include/glad/glad.h"

LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int main() {
    WNDCLASSEX wcex = {0};
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_CLASSDC;
    wcex.lpfnWndProc = wndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = GetModuleHandle(NULL);
    wcex.hIcon = NULL;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = 0;
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "OpenGL";
    wcex.hIconSm = NULL;

    if (!RegisterClassEx(&wcex)) {
        MessageBox(NULL, "Failed to register window class", "Error", MB_ICONERROR);
        return -1;
    }

    HWND hwnd = CreateWindow("OpenGL", "OpenGL", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, GetModuleHandle(NULL), NULL);

    if (hwnd == NULL) {
        MessageBox(NULL, "Failed to create window", "Error", MB_ICONERROR);
        return -1;
    }

    HDC hdc = GetDC(hwnd);
    if (hdc == NULL) {
        MessageBox(NULL, "Failed to get device context", "Error", MB_ICONERROR);
        return -1;
    }

    PIXELFORMATDESCRIPTOR pfd = {0};
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.cStencilBits = 8;

    int format = ChoosePixelFormat(hdc, &pfd);
    if (format == 0) {
        MessageBox(NULL, "Failed to choose pixel format", "Error", MB_ICONERROR);
        return -1;
    }

    if (!SetPixelFormat(hdc, format, &pfd)) {
        MessageBox(NULL, "Failed to set pixel format", "Error", MB_ICONERROR);
        return -1;
    }

    HGLRC hrc = wglCreateContext(hdc);
    if (hrc == NULL) {
        MessageBox(NULL, "Failed to create OpenGL context", "Error", MB_ICONERROR);
        return -1;
    }

    if (!wglMakeCurrent(hdc, hrc)) {
        MessageBox(NULL, "Failed to make OpenGL context current", "Error", MB_ICONERROR);
        return -1;
    }

    if (!gladLoadGLLoader((GLADloadproc)wglGetProcAddress)) {
        MessageBox(NULL, "Failed to load OpenGL functions", "Error", MB_ICONERROR);
        return -1;
    }

    MSG msg;
    while (1) {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            if (msg.message == WM_QUIT) {
                break;
            }
        } else {
            glClear(GL_COLOR_BUFFER_BIT);
            glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
            SwapBuffers(hdc);
        }
    }

    wglDeleteContext(hrc);
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
    UnregisterClass("OpenGL", GetModuleHandle(NULL));

    return 0;
}

