/*
	GameEngine and Editor
	by John Ryland
	Copyright (c) 2023
*/

////////////////////////////////////////////////////////////////////////////////////
//	Editor UI

// Dear ImGui: standalone example application for Glfw + Vulkan
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
// Read online: https://github.com/ocornut/imgui/tree/master/docs

// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app.
// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h.
//   You will use those if you want to use this rendering backend in your engine/app.
// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by
//   the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code.
// Read comments in imgui_impl_vulkan.h.

#include "Application.h"
#include "Credits.h"
#include "GameEditorWindow.h"
#include "GameState.h"

/*

Object tree:

Selectable

Editor types:

bool -> checkbox
enum -> combo / listbox / radio buttons
string -> input text
int -> input int / slider int
float -> input float / slider float
double -> input double / slider double / input scientific
ratio -> slider float
angle -> slider angle
color
color with alpha

*/

// Moved here because of the 'using' namespace
#include "SchemaEdit.h"

#include "Library.h"


// Initial window size hack
#include "imgui_internal.h"
void GetWindowSize(int& width, int& height)
{
    if (!ImGui::GetCurrentContext())
        ImGui::CreateContext();
    if (GImGui->IO.IniFilename)
        ImGui::LoadIniSettingsFromDisk(GImGui->IO.IniFilename);
/*
    auto* settings = ImGui::FindWindowSettingsByID("DockSpaceViewport_11111111");
    if (settings)
    {
        width = settings->Size.x;
        height = settings->Size.y;
    }
*/
}


#include "PluginManager.h"

int main(int argc, char* argv[])
{
    printf("Hi\n");

    GameEngine::InitializeAcknowledgements();

/*
    auto tmp1 = Utilities::make_fixed_string("blah");
    auto tmp2 = Utilities::make_fixed_string("123");
    auto tmp3 = tmp1 + " " + tmp2 + " foo " + " bar ";
    printf("test: -%s-\n", tmp3.c_str());
*/

    if (argc > 1)
    {
        XmlTag root;
        root.tag = "root";
        if (ParseXmlFile(argv[1], &root))
        {
            DynSchema dynSchema;
            ConvertXmlToSchema(&root, &dynSchema);
            std::string fileOut = argv[1];
            fileOut += ".bin";
            WriteSchemeFile(&dynSchema, fileOut.c_str());
            Schema* schema = GameRuntime::LoadSchemaFromFile(fileOut.c_str());
            DumpSchema(schema);

            ApplicationFramework::Application app;

            int width = 1024;
            int height = 768;
            GetWindowSize(width, height);

            if (width <= 0)
                width = 640;
            if (height <= 0)
                height = 480;
 
            //ApplicationFramework::Window testWin(app, width, height, "Test Window");
            GameEngine::GameEditorWindow gameEditorWindow(app, width, height, "GameEditor");
            //ApplicationFramework::MainWindow mainWindow(app, width, height, "Main Window");

            gameEditorWindow.SetSchema(schema);

            app.MainLoop();

            GameRuntime::DestroySchema(schema);
        }
        else
        {
            printf("Couldn't open file: %s\n", argv[1]);
        }
    }
    else
    {
        printf("Usage: %s schema-file-name\n", argv[0]);
    }

    return 0;
}
