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

////////////////////////////////////////////////////////////////////////////////////
//	Node Editor View

#include "NodeEditorView.h"
#include "imgui.h"

namespace ed = ax::NodeEditor;
extern void BluePrintsStart();
extern void BluePrintsStop();
extern void BluePrintsFrame();

namespace GameEngine {

void NodeEditorView::AddShowMenuItem()
{
    ImGui::MenuItem("Show Node Editor", NULL, &m_open);
}

void NodeEditorView::Update()
{
    if (m_open)
    {
        if (ImGui::Begin("Node Editor", &m_open))
            Show();
        ImGui::End();
    }
}

void NodeEditorView::Initialize()
{
    ed::Config config;
    config.SettingsFile = "Simple.json";
    m_Context = ed::CreateEditor(&config);

    //BluePrintsStart();
}

void NodeEditorView::Shutdown()
{
    //BluePrintsStop();

    ed::DestroyEditor(m_Context);
}

void NodeEditorView::Show()
{
    auto& io = ImGui::GetIO();
    float deltaTime = io.DeltaTime;
    ImVec2 canvas_p0 = ImGui::GetCursorScreenPos();      // ImDrawList API uses screen coordinates!
    ImVec2 canvas_sz = ImGui::GetContentRegionAvail();   // Resize canvas to what's available
    if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f;
    if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f;
    ImVec2 canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
    ImGui::SetNextWindowPos(canvas_p0);
    ImGui::SetNextWindowSize(canvas_sz);
    ImGui::Begin("Content", nullptr, ImGuiWindowFlags_NoTitleBar |
        ImGuiWindowFlags_NoResize |
        ImGuiWindowFlags_NoMove |
        ImGuiWindowFlags_NoScrollbar |
        ImGuiWindowFlags_NoScrollWithMouse |
        ImGuiWindowFlags_NoSavedSettings |
        ImGuiWindowFlags_NoBringToFrontOnFocus);

    //BluePrintsFrame();

    ed::SetCurrentEditor(m_Context);
    ed::Begin("My Editor", canvas_sz);// ImVec2(0.0, 0.0f));
    int uniqueId = 1;
    // Start drawing nodes.
    ed::BeginNode(uniqueId++); 
        ImGui::Text("Node A");
        ed::BeginPin(uniqueId++, ed::PinKind::Input);
            ImGui::Text("-> In");
        ed::EndPin();
        ImGui::SameLine();
        ed::BeginPin(uniqueId++, ed::PinKind::Output);
            ImGui::Text("Out ->");
        ed::EndPin();
    ed::EndNode();
    ed::End();
    ed::SetCurrentEditor(nullptr);

    ImGui::End();
}

} // GameEngine namespace
