#include "Win32UIInterface.h"

#include "DesktopManager.h"
#include "DirectXInterface.h"
#include "DirectX2dInterface.h"

#include <d3d12.h>
#include <dxgi.h>
#include <dxgi1_6.h>
#include <Windows.h>

Win32UIInterface::Win32UIInterface(DesktopManager* desktopManager, bool useHardware)
    :   AbstractUIInterface(desktopManager, useHardware),
        mWindowInterface(std::make_unique<Win32WindowInterface>())
{

}

void Win32UIInterface::initialize()
{
    if (mUseHardwareRendering)
    {
        initializeHardwareRendering();
    }

    const auto num_windows = mDesktopManager->getWindowManager()->getNumWindows();
    for (std::size_t idx = 0; idx < num_windows; idx++)
    {
        addWindow(mDesktopManager->getWindowManager()->getWindow(idx));
    }
    showWindow(mDesktopManager->getWindowManager()->getMainWindow());
}

void Win32UIInterface::loop()
{
    initialize();

    // Run the message loop.
    MSG msg = { };
    while (::GetMessage(&msg, NULL, 0, 0))
    {
        if (msg.message == WM_QUIT)
        {
            break;
        }

        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }
}

void Win32UIInterface::shutDown()
{

}

void Win32UIInterface::showWindow(mt::Window* window)
{
    mWindowInterface->show(window);
}

void Win32UIInterface::addWindow(mt::Window* window)
{
    mWindowInterface->add(window, mDesktopManager, mDxInterface.get());
}

void Win32UIInterface::initializeHardwareRendering()
{
    mDxInterface = std::make_unique<DirectXInterface>();
    mDxInterface->initialize();
}