#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>

TCHAR szEvent[100] = _TEXT("Press a key or click inside the window.");

// Declaring the Window Procedure Function
LRESULT CALLBACK fnWinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
    TCHAR szAppName[] = _TEXT("Window Proc Example");
    WNDCLASS wndClass;
    HWND hwnd;
    MSG uMsg;

    // Creating Window Class
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndClass.hInstance = hInstance;
    // Assigned Window Procedure
    wndClass.lpfnWndProc = fnWinProc;
    wndClass.lpszClassName = szAppName;
    wndClass.lpszMenuName = NULL;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClass(&wndClass))
    {
        static TCHAR szBuff[256];
        wsprintf(szBuff, _TEXT("Cannot Register Window Class named \" %s \""), wndClass.lpszClassName);
        MessageBox(NULL, szBuff, _TEXT("ERROR"), MB_ICONERROR | MB_APPLMODAL);
        return 1;
    }

    hwnd = CreateWindow(szAppName,
                        szAppName,
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);

    if (!hwnd)
    {
        MessageBox(NULL, _TEXT("Window creation failed!"),
                   _TEXT("ERROR"), MB_ICONERROR);
        return 1;
    }

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    // MESSAGE LOOP
    while (GetMessage(&uMsg, NULL, 0, 0))
    {
        TranslateMessage(&uMsg);
        DispatchMessage(&uMsg);
    }

    return (int)uMsg.wParam;
}

// Defining Window Procedure
LRESULT CALLBACK fnWinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    // Creating switch on uMsg
    switch (uMsg)
    {
    // Handling cases
    case WM_KEYDOWN:
        wsprintf(
            szEvent,
            _TEXT("Virtual Key: %d"),
            (int)wParam);
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;

    case WM_CHAR:
        wsprintf(
            szEvent,
            _TEXT("Character: %c | Code: %d"),
            (TCHAR)wParam,
            (int)wParam);
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;

    case WM_LBUTTONDOWN:
        wsprintf(
            szEvent,
            _TEXT("Mouse clicked at X: %d, Y: %d"),
            GET_X_LPARAM(lParam), // Reading extra data given by windows
            GET_Y_LPARAM(lParam));
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        TextOut(hdc, 20, 20, szEvent, lstrlen(szEvent));
        EndPaint(hwnd, &ps);
        return 0;
    }

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    // Redirecting to Default Window Procedure to manage unhandled cases
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}