/******************************************************************************
*	File		-	mainPanel.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/11/01 (y/m/d)
*	Mod Date	-	2021/11/01 (y/m/d)
*	Description	-	The main window for the editor.
******************************************************************************/

#include "mainPanel.h"

#include "../editor.h"
#include <core/core.h>
#include <dearimgui/imgui.h>
#include <utils/logger.h>

namespace lunarium
{
    MainPanel* MainPanel::mpInstance = nullptr;
    MainPanel::MainPanel()
        : Panel(PT_MAIN, true), mpEditor(nullptr)
    {
    }
    
    
    MainPanel& MainPanel::GetInstance()
    {
        if (!mpInstance)
        {
            mpInstance = new MainPanel;
        }
    
        return *mpInstance;
    }
    
    
    void MainPanel::FreeInstance()
    {
        delete mpInstance;
        mpInstance = nullptr;
    }

    void MainPanel::SetEditor(Editor* e)
    {
        mpEditor = e;
    }

    bool MainPanel::DoFrame()
    {
        if (!mIsOpen)
            return false;
            

        DoMainMenu();

        Core::MainWindow().GetFramebufferSize(&mWidth, &mHeight);
        Core::MainWindow().GetPosition(&mX, &mY);

        float double_frame_height = ImGui::GetFrameHeight() * 2;

        ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
        ImGui::SetNextWindowSize(ImVec2(mWidth, mHeight - double_frame_height), ImGuiCond_Always);
        ImGui::SetNextWindowPos(ImVec2(mX, mY + ImGui::GetFrameHeight()), ImGuiCond_Always);
        if (!ImGui::Begin("Lunarium Editor", &mIsOpen, ImGuiWindowFlags_NoCollapse 
                                 | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
                                 | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBringToFrontOnFocus))
        {
            ImGui::End();
            return false;
        }
        
        // ImGui::DockSpace(ImGui::GetID("LEDockSpace"));
        ImGui::PopStyleVar();
        ImGui::End();

        // STATUS BAR
        const ImGuiViewport* viewport = ImGui::GetMainViewport();
        ImGui::SetNextWindowViewport(viewport->ID);
        ImGui::SetNextWindowSize(ImVec2(mWidth, ImGui::GetFrameHeight() - 12.0f), ImGuiCond_Always);
        ImGui::SetNextWindowPos(ImVec2(mX, mY + (mHeight - ImGui::GetFrameHeight() - 8.0f)), ImGuiCond_Always);
        ImGui::Begin("status bar", &mIsOpen, ImGuiWindowFlags_NoCollapse 
                                 | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
                                 | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking );

        if (ImGui::BeginTable("status", 3, ImGuiTableFlags_BordersInnerV))
        {
            ImGui::AlignTextToFramePadding();
            ImGui::TableNextColumn();
            ImGui::Text("Lunarium");
            ImGui::TableNextColumn();
            ImGui::Text("Testing");
            ImGui::TableNextColumn();
            ImGui::Text("Status Bar");
            ImGui::EndTable();
        }

        ImGui::End();
        
        return true;
    }

    void MainPanel::DoMainMenu()
    {
        ImGui::BeginMainMenuBar();
        
            // File
            if (ImGui::BeginMenu("File"))
            {
                if (ImGui::MenuItem("New Project"))
                {
                    mpEditor->NewProject();
                }

                if (ImGui::MenuItem("Open Project"))
                {
                    mpEditor->OpenProject();
                }

                if (ImGui::MenuItem("Save Project"))
                {
                    mpEditor->SaveProject();
                }

                if (ImGui::MenuItem("Save Project As"))
                {
                    mpEditor->SaveAs();
                }

                ImGui::Separator();

                if (ImGui::MenuItem("Exit"))
                {
                    mpEditor->Exit();
                }
                ImGui::EndMenu();
            }

            if (ImGui::BeginMenu("Windows"))
            {
                
                ImGui::EndMenu();
            }

            if (ImGui::BeginMenu("Help"))
            {
                if (ImGui::MenuItem("About"))
                {
                    mpEditor->ShowAboutPanel();
                }
                
                ImGui::EndMenu();
            }

        ImGui::EndMainMenuBar();
    }
}