#include <X11/Xlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// system global variables
Display *dpy;
Screen *main_screen;
Window root_window;
GC gc_root_window;


// user global variables
Window login_window;
GC gc_login_window;
XGCValues gcValues_gc_login_window;
XTextItem TextItem_login_window;
XEvent e;




int main(int argc, char *argv[])
{
    // system setup
    dpy = XOpenDisplay(":0.0");
    main_screen = XDefaultScreenOfDisplay(dpy);
    root_window = XDefaultRootWindow(dpy);
    gc_root_window = XDefaultGC(dpy, 0);
    int screen_width = atoi(argv[2]);
    int screen_height = atoi(argv[3]);

    if(argc == 4)
    {
        if(strcmp(argv[1], "-init") == 0)
        {
            char command[30] = "xrandr -s ";
            char num[6];
            sprintf(num, "%d", screen_width);
            strcat(command, num);
            strcat(command, "x");
            sprintf(num, "%d", screen_height);
            strcat(command, num);
            system(command);
            printf("%s\n", command);
            sleep(1);
        }
    }else
    {
        printf("please run \"./VLDM -init [screen_width] [screen_height]\" to get the correct resolution\n");
        return 0;
    }
    

    // seting up the login window
    login_window = XCreateSimpleWindow(dpy, root_window, 0,0, screen_width-40, screen_height-40, 20, XWhitePixel(dpy, 0), XBlackPixel(dpy, 0));
    gcValues_gc_login_window.font = XLoadFont(dpy, "variable");
    gc_login_window = XCreateGC(dpy, login_window, GCFont, &gcValues_gc_login_window);
    XMapWindow(dpy, login_window);

    // seting up text
    TextItem_login_window.chars = "Welcome.";
    TextItem_login_window.nchars = 8;
    TextItem_login_window.delta = 10;

    XDrawText(dpy, login_window, gc_login_window, screen_height/2, screen_width/2, &TextItem_login_window, 1);



    // Handling events
    XSelectInput(dpy, login_window, ButtonPressMask);

    XNextEvent(dpy, &e);




    // exit properly
    XCloseDisplay(dpy);
    return 0;
}