#pragma once
#include "Window.h"
#include "Graphics.h"
#include "Logger.h"
#include "CoreHandler.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_dx11.h"
#include "imgui/imgui_impl_win32.h"

Logger logger("log.txt");

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    logger.start();

    // the handle for the window, filled by a function
    HWND hWnd = nullptr;

    // register window
    Window window(hWnd, hInstance, nCmdShow, 1280, 800, logger);

    // init imgui
    ImGui::CreateContext();
    ImGui_ImplWin32_Init(hWnd);

    // init core handler for interactive process, i.e. retrieve IO and process graphics
    CoreHandler coreHandler(window, logger);
    coreHandler.InitCoreHandler(window, logger);

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while (true)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            // translate keystroke messages into the right format
            TranslateMessage(&msg);

            // send the message to the WindowProc function
            DispatchMessage(&msg);

            // check if message to quit
            if (msg.message == WM_QUIT)
            {
                break;
            }
        }
        else
        {
            // Retrieve IO and process graphics
            coreHandler.CoreProcess(window, window.GetGfx(), hWnd, logger);
        }
    }

    logger.end();

    // Close coreHandler
    coreHandler.CloseCoreHandler();

    // close imgui
    ImGui_ImplWin32_Shutdown();
    ImGui::DestroyContext();

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;
}