#include "DeerStudio.h"

#include "Deer/Path.h"
#include "Deer/VoxelWorld.h"
#include "Deer/DataStore.h"
#include "Deer/Voxel.h"
#include "Deer/Scene.h"

#include "DeerRender/Application.h"
#include "DeerRender/GizmoRenderer.h"

#include "DeerStudio/EditorEngine.h"
#include "DeerStudio/Editor/Viewport.h"
#include "DeerStudio/Editor/Terrain/TerrainEditor.h"

// TMP
#include "DeerStudio/Editor/Icons.h"
#include "DeerStudio/Fonts.h"
#include "DeerStudio/Style.h"
// TMP

int main(int, char**) {
	Deer::DeerStudio::main();
}

namespace Deer {
    namespace DeerStudio {
		void main() {

			Log::init();
    		Application::initWindow();
			
			Path projectPath = Application::getWindow().folderDialog(nullptr);
			if (projectPath.empty()) return;
 
			DataStore::rootPath = projectPath;

			DataStore::loadVoxelsData();
			DataStore::loadVoxelsAspect();
			DataStore::generateTextureAtlas();
			DataStore::loadVoxelsShaders();

			EditorEngine::initialize();

			Icons::setupIcons();

			initializeFonts();
			SetupImGuiStyle();

			ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20));
				
			Application::setTickCallback(DeerStudio::onUpdate);
			Application::setRenderCallback(DeerStudio::onRender);
			Application::setEventCallback(DeerStudio::onEvent);

    		Application::run();

			EditorEngine::deinitialize();

			Application::shutdownWindow();
			Log::shutdown();
		}

        void onUpdate() {
		    if (VoxelWorld::isInitialized())
		    	VoxelWorld::bakeNextChunk();	
        }

        void onRender() {
		    static bool opt_fullscreen = true;
		    static bool opt_padding = false;
		    static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;

		    ImGuiWindowFlags window_flags =
		        ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;

		    {
		    	const ImGuiViewport* viewport = ImGui::GetMainViewport();
		    	ImGui::SetNextWindowPos(viewport->WorkPos);
		    	ImGui::SetNextWindowSize(viewport->WorkSize);
		    	ImGui::SetNextWindowViewport(viewport->ID);
		    	window_flags |= ImGuiWindowFlags_NoTitleBar |
		    	                ImGuiWindowFlags_NoCollapse |
		    	                ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
		    	window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus |
		    	                ImGuiWindowFlags_NoNavFocus;
		    	window_flags |= ImGuiWindowFlags_NoBackground;
		    }

		    static bool p_open = true;
		    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
		    ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
		    ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
		    ImGui::Begin("DockSpace Demo", &p_open, window_flags);
		    ImGui::PopStyleVar();
		    ImGui::PopStyleVar(2);

		    ImGuiID dockspace_id = ImGui::GetID("DockSpace Demo");
		    ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);

			ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10));
		    if (ImGui::BeginMenuBar()) {
				if (ImGui::BeginMenu("Pannel")) {
					onPannelMenuBar();	
					ImGui::EndMenu();
				}
		    	ImGui::EndMenuBar();
		    }
			ImGui::PopStyleVar();

		    TerrainEditor::onImGui();
		    //viewport_onImGui();
		    EditorEngine::render();

		    Scene::gizmoRenderer.refresh();
		    ImGui::End();
        }

	    void onEvent(Event& e) {
	    	//viewport_onEvent(e);

			EventDispatcher ed(e);

			ed.dispatch<WindowCloseEvent>(onWindowCloseEvent);
	    }

		bool onWindowCloseEvent(WindowCloseEvent&) {
			Application::shutdown();
			return true;
		}
    }
}  // namespace Deer