/******************************************************************************
*	File		-	core_console.cpp
*	Author		-	Joey Pollack
*	Date		-	2022/02/10 (y/m/d)
*	Mod Date	-	2022/02/10 (y/m/d)
*	Description	-	console for the game mode
******************************************************************************/

#include "core_console.h"
#include <dearimgui/imgui.h>
#include <dearimgui/imgui_internal.h>   // To use the DockWindowXXX methods

#include <iostream>

namespace lunarium
{
    CoreConsole::CoreConsole()
        : Console("Core Console"), mDockIsInit(false)
    {

    }

    CoreConsole::~CoreConsole()
    {
    }

    bool CoreConsole::DoFrame()
    {
        InitDock();

        if (!mIsOpen)
            return false;
            

        ImGuiViewport* pView = ImGui::GetMainViewport();

        float myHeight =  pView->WorkSize.y / 3.0f;
        float y = pView->WorkPos.y + (myHeight * 2);
        
        float alpha = IsFocused() ? 0.75f : 0.5f;
         ImGui::SetNextWindowPos(ImVec2(pView->WorkPos.x, y), ImGuiCond_Always);
         ImGui::SetNextWindowSize(ImVec2(pView->WorkSize.x, myHeight), ImGuiCond_Always);
        ImGui::SetNextWindowBgAlpha(alpha);
        if (!ImGui::Begin(GetName(), &mIsOpen, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar
                                                | ImGuiWindowFlags_NoCollapse))
        {
            ImGui::End();
            return mIsOpen;
        }
        
        Console::DoFrame();
        
        ImGui::End();

        return mIsOpen;
    }

    void CoreConsole::InitDock()
    {
        ImGuiViewport* Viewport = ImGui::GetMainViewport();
        // ImGuiID mainID = ImGui::DockSpace(ImGui::GetID("Core Dockspace"));
        ImGuiID mainID = ImGui::DockSpaceOverViewport(Viewport, ImGuiDockNodeFlags_PassthruCentralNode);
        if (!ImGui::DockBuilderGetNode(mainID) || !mDockIsInit) 
        {
            mDockIsInit = true;
            ImGui::DockBuilderRemoveNode(mainID);
            ImGui::DockBuilderAddNode(mainID, ImGuiDockNodeFlags_DockSpace);
            ImGui::DockBuilderSetNodeSize(mainID, Viewport->Size);
            //ImGui::DockBuilderSetNodePos(mDockSpaces.Main, Viewport->WorkPos);

            ImGuiID bottom = ImGui::DockBuilderSplitNode(mainID, ImGuiDir_Down, 0.35f, nullptr, nullptr);
            ImGui::DockBuilderFinish(mainID);

            // Dock Panels
            //ImGui::DockBuilderDockWindow(GetName(), bottom);
        }
    }
}