#include "EditorLayer.h"
#include <imgui/imgui.h>
#include <iostream>
#include <glm/glm.hpp>
#include "Panels/Viewport.h"
#include "Panels/Console.h"
#include "Panels/Hierarchy.h"
#include "Panels/Inspector.h"
#include "Panels/Log.h"
#include "Panels/ProjectManager.h"
#include "Panels/Profiler.h"
namespace Runic {


	void EditorLayer::ShowMenuFile()
	{
		ImGui::MenuItem("(dummy menu)", NULL, false, false);
		if (ImGui::MenuItem("New")) {}
		if (ImGui::MenuItem("Open", "Ctrl+O")) {}
		if (ImGui::MenuItem("Save", "Ctrl+S")) {}
		if (ImGui::MenuItem("Save As..")) {}

		ImGui::Separator();
		if (ImGui::MenuItem("Quit", "Alt+F4")) { Runic::Application::Get().Close(); }
	}


	void EditorLayer::ShowAppMainMenuBar()
	{
		if (ImGui::BeginMainMenuBar())
		{
			if (ImGui::BeginMenu("File"))
			{
				ShowMenuFile();
				ImGui::EndMenu();
			}
			if (ImGui::BeginMenu("Edit"))
			{
				if (ImGui::MenuItem("Undo", "CTRL+Z")) {}
				if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {}  // Disabled item
				ImGui::Separator();
				if (ImGui::MenuItem("Cut", "CTRL+X")) {}
				if (ImGui::MenuItem("Copy", "CTRL+C")) {}
				if (ImGui::MenuItem("Paste", "CTRL+V")) {}
				ImGui::EndMenu();
			}
			if (ImGui::BeginMenu("Asset"))
			{
				if (ImGui::MenuItem("PlaceHolder", "PlaceHolder")) {}
				ImGui::Separator();
				ImGui::EndMenu();
			}
			if (ImGui::BeginMenu("GameObject"))
			{
				if (ImGui::MenuItem("Create Empty", "PlaceHolder")) {
					Entity entity = m_ActiveScene->CreateEntity("Empty Entity");
				}
				if (ImGui::BeginMenu("3D Object", "PlaceHolder")) {
					if (ImGui::MenuItem("Cube",	"PlaceHolder")) {

						{

							Entity entity = m_ActiveScene->CreateEntity("Cube");
							auto mat = AssetManager::GetMaterial("DefaultMaterial").lock();
							auto mesh = AssetManager::GetMesh("RunicCube").lock();
							entity.AddComponent<MeshRendererComponent>(mat,mesh);
							const Entity world =  m_ActiveScene->GetWorld();
							entity.AddComponent<ParentComponent>(world);

							world.GetComponent<ChildrenComponent>().Children.insert(entity);
						}
					}
					if (ImGui::MenuItem("Sphere", "PlaceHolder")) {
					
						{
							Entity entity = m_ActiveScene->CreateEntity("Sphere");
							auto mat = AssetManager::GetMaterial("DefaultMaterial").lock();
							auto mesh = AssetManager::GetMesh("RunicSphere").lock();

							entity.AddComponent<MeshRendererComponent>(mat, mesh);
							const Entity world = m_ActiveScene->GetWorld();
							entity.AddComponent<ParentComponent>(world);

							world.GetComponent<ChildrenComponent>().Children.insert(entity);
						}
					}
					if (ImGui::MenuItem("Plane", "PlaceHolder")) {
						{
							Entity entity = m_ActiveScene->CreateEntity("Plane");
							auto mat = AssetManager::GetMaterial("DefaultMaterial").lock();
							auto mesh = AssetManager::GetMesh("RunicPlane").lock();

							entity.AddComponent<MeshRendererComponent>(mat, mesh);
							const Entity world = m_ActiveScene->GetWorld();
							entity.AddComponent<ParentComponent>(world);
							world.GetComponent<ChildrenComponent>().Children.insert(entity);
						}

					}
					ImGui::EndMenu();
				}
				ImGui::Separator();
				if (ImGui::BeginMenu("Light", "PlaceHolder")) {
					if (ImGui::MenuItem("Directional Light", "PlaceHolder")) {

						{
							Entity entity = m_ActiveScene->CreateEntity("Directional Light");
							auto& lc = entity.AddComponent<LightComponent>();
							lc.Type = LightType::Directional;
							const Entity world = m_ActiveScene->GetWorld();
							entity.AddComponent<ParentComponent>(world);

							world.GetComponent<ChildrenComponent>().Children.insert(entity);
						}
					}
					if (ImGui::MenuItem("Point Light", "PlaceHolder")) {

						{
							Entity entity = m_ActiveScene->CreateEntity("Point Light");
							auto& lc =  entity.AddComponent<LightComponent>();
							lc.Type = LightType::Point;
							const Entity world = m_ActiveScene->GetWorld();
							entity.AddComponent<ParentComponent>(world);

							world.GetComponent<ChildrenComponent>().Children.insert(entity);

						}
					}
					//if (ImGui::MenuItem("Spot Light", "PlaceHolder")) {
					//
					//}
					ImGui::EndMenu();
				}
				ImGui::EndMenu();
			}
			if (ImGui::BeginMenu("Window"))
			{
				if (ImGui::MenuItem("Hierarchy", "PlaceHolder")) {		m_Panels["Hierarchy"]->Show(); }
				if (ImGui::MenuItem("Scene", "PlaceHolder")) {			m_Panels["Scene"]->Show(); }
				if (ImGui::MenuItem("Inspector", "PlaceHolder")) {		m_Panels["Inspector"]->Show(); }
				if (ImGui::MenuItem("Console", "PlaceHolder")) {		m_Panels["Console"]->Show(); }
				if (ImGui::MenuItem("Log", "PlaceHolder")) {			m_Panels["Log"]->Show(); }
				if (ImGui::MenuItem("Project", "PlaceHolder")) {		m_Panels["Project"]->Show(); }
				if (ImGui::MenuItem("Profiler", "PlaceHolder")) { m_Panels["Profiler"]->Show(); }

				ImGui::Separator();
				ImGui::EndMenu();
			}
			
		}ImGui::EndMainMenuBar();
	}



	EditorLayer::EditorLayer()
		: Layer("EditorLayer")
	{

	}

	void EditorLayer::OnImGuiRender()
	{
		
		static bool dockspaceOpen = true;
		static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;
		static bool opt_fullscreen_persistant = true;
		static bool show_log = true;
		static bool show_console = true;

		bool opt_fullscreen = opt_fullscreen_persistant;

		ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
		if (opt_fullscreen)
		{
			ImGuiViewport* viewport = ImGui::GetMainViewport();
			ImGui::SetNextWindowPos(viewport->Pos);
			ImGui::SetNextWindowSize(viewport->Size);
			ImGui::SetNextWindowViewport(viewport->ID);
			ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
			ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
			window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
			window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
		}

		if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode)
			window_flags |= ImGuiWindowFlags_NoBackground;
		ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
		ImGui::Begin("DockSpace", &dockspaceOpen, window_flags);
		ImGui::PopStyleVar();

		if (opt_fullscreen)
			ImGui::PopStyleVar(2);

		// DockSpace
		ImGuiIO& io = ImGui::GetIO();
		if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
		{
			ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
			ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
		}

		ShowAppMainMenuBar();

		for (auto [_, p] : m_Panels)
			p->OnRender();
		
		ImGui::End();


		
	}

	void EditorLayer::OnAttach()
	{
		//lib.Load("Assets/Shaders/Grid.vs");
		//File f = File("Assets/Shaders/Grid.vs");
		//m_file_paths.push_back(f);

		m_ActiveScene = CreateRef<Scene>();
		SceneManager::SetActiveScene(m_ActiveScene);



		m_Panels["Scene"] = CreateRef<EditorViewport>();	
		m_Panels["Hierarchy"] = (CreateRef<EditorAppHierarchy>());
		m_Panels["Project"] = (CreateRef<ProjectManager>());
		m_Panels["Log"] =(CreateRef<EditorAppLog>());
		m_Panels["Console"] = (CreateRef<EditorAppConsole>());
		m_Panels["Inspector"] = (CreateRef<EditorAppInspector>());
		m_Panels["Profiler"] = (CreateRef<EditorAppProfiler>());


	}

	void EditorLayer::OnDetach()
	{
	}

	void EditorLayer::OnUpdate(Timestep ts)
	{
		for (auto [_, p] : m_Panels)
			p->OnUpdate(ts);


	}

	void EditorLayer::OnEvent(Runic::Event & e)
	{
		for (auto[_,p] : m_Panels)
			p->OnEvent(e);

	}
}