/******************************************************************************
*	File		-	luaConsole.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/09/17 (y/m/d)
*	Mod Date	-	2021/09/17 (y/m/d)
*	Description	-	GUI window for the LUA console
******************************************************************************/

#include "luaConsole.h"
#include <core/core.h>
#include <input/inputManager.h>
#include <window/window.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <cstring>

namespace lunarium
{
    LuaConsole* LuaConsole::mpInstance = nullptr;
    LuaConsole::LuaConsole()
        : mbShow(false), mbNewCommand(false)
    {
        memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);
    }
    
    LuaConsole& LuaConsole::GetInstance()
    {
        if (!mpInstance)
        {
            mpInstance = new LuaConsole;
        }

        return *mpInstance;
    }
    
    void LuaConsole::FreeInstance()
    {
        delete mpInstance;
        mpInstance = nullptr;
    }
    
    OpRes LuaConsole::Initialize()
    {
        return OpRes::OK();
    }
    
    void LuaConsole::Show()
    {
        // TODO: LuaConsole::Show
        if (!mbShow)
            return;

        int x, y;
        Core::MainWindow().GetPosition(&x, &y);
        int width, height;
        Core::MainWindow().GetFramebufferSize(&width, &height);
        
        ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_Always);
        ImGui::SetNextWindowSize(ImVec2(width / 3.0f, height / 3.0f), ImGuiCond_Always);
        if (!ImGui::Begin("LUA Console", &mbShow, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
                                                  | ImGuiWindowFlags_NoSavedSettings))
        {
            ImGui::End();
            return;
        }

        ImGui::BeginChild("history", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
        for (int i = 0; i < mCommandHistory.size(); i++)
        {
            const char* msg = mCommandHistory[i].c_str();
            int len = strlen(msg);
            
            ImGui::TextUnformatted(msg);
        }

            
        ImGui::EndChild();
        ImGui::Separator();
        ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE);

        if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
            ImGui::SetScrollHereY(1.0f);
        ImGui::End();

        if (Core::Input().IsKeyPressed(KeyCode::RETURN) && strlen(mBuffer) > 1)
        {
            mCommandHistory.push_back(mBuffer);
            memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);
            mbNewCommand = true;
        }
    }

    void LuaConsole::SetShow(bool show)
    {
        mbShow = show;
    }
    
    bool LuaConsole::IsShown() const
    {
        return mbShow;
    }
    
    const std::vector<std::string>* LuaConsole::GetCommandHistory() const
    {
        return &mCommandHistory;
    }
    
    std::string LuaConsole::GetLastCommand()
    {
        return mCommandHistory.back();
    }
    
    bool LuaConsole::GetNewCommand(std::string& command)
    {
        if (mbNewCommand)
        {
            command = mCommandHistory.back();
            mbNewCommand = false;
            return true;
        }

        return false;
    }
}