#include "GuiApplication.h"

#include "UiInterfaceFactory.h"
#include "Window.h"
#include "WindowManager.h"
#include "DesktopManager.h"
#include "FontsManager.h"
#include "MainApplication.h"
#include "AbstractUiInterface.h"
#include "Widget.h"

#include "FileLogger.h"

GuiApplication::GuiApplication(std::unique_ptr<CommandLineArgs> args, std::unique_ptr<MainApplication> mainApp)
    : AbstractDesktopApp(),
      mDesktopManager(DesktopManager::Create(this))
{
    if (mainApp)
    {
        mMainApplication = std::move(mainApp);
    }
    else
    {
        mMainApplication = MainApplication::Create();
        mMainApplication->initialize(args ? std::move(args) : CommandLineArgs::Create());
    }
}

GuiApplication::~GuiApplication()
{

}

AbstractApp* GuiApplication::getMainApplication() const
{
    return mMainApplication.get();
}

AbstractUIInterface* GuiApplication::getUiInterface() const
{
    return mUiInterface.get();
}

void GuiApplication::initializeViews()
{
    mDesktopManager->getWindowManager()->getMainWindow()->setSize(800, 600);
}

void GuiApplication::setUiInterfaceBackend(UiInterfaceFactory::Backend backend)
{
    mUiInterfaceBackend = backend;
}

void GuiApplication::run()
{
    initializeViews();

    MLOG_INFO("Creating Window Interface");
    mUiInterface = UiInterfaceFactory::create(mDesktopManager.get(), mUiInterfaceBackend);

    mUiInterface->loop();

    MLOG_INFO("Window Interface Shut Down");

    mMainApplication->shutDown();
}
