project(editScene)
set(CMAKE_CXX_STANDARD 17)

find_package(OGRE REQUIRED COMPONENTS Bites Overlay MeshLodGenerator CONFIG)
find_package(flecs REQUIRED CONFIG)
find_package(nlohmann_json REQUIRED)
find_package(SDL2 REQUIRED)
find_package(Jolt REQUIRED)
find_package(OgreProcedural REQUIRED CONFIG)

# Build RecastNavigation from copied source (RTTI-compatible)
add_subdirectory(recastnavigation)

# Collect all source files
set(EDITSCENE_SOURCES
	main.cpp
	EditorApp.cpp
	GameMode.cpp
	systems/EditorUISystem.cpp
	systems/SceneSerializer.cpp
	systems/PhysicsSystem.cpp
	systems/BuoyancySystem.cpp
	systems/EditorSunSystem.cpp
	systems/EditorSkyboxSystem.cpp
	systems/EditorWaterPlaneSystem.cpp
	systems/LightSystem.cpp
	systems/CameraSystem.cpp
	systems/LodSystem.cpp
	systems/StaticGeometrySystem.cpp
	systems/ProceduralTextureSystem.cpp
	systems/ProceduralMaterialSystem.cpp
	systems/ProceduralMeshSystem.cpp
	systems/CellGridSystem.cpp
	systems/NormalDebugSystem.cpp
	systems/RoomLayoutSystem.cpp
	systems/FurnitureLibrary.cpp
	systems/StartupMenuSystem.cpp
	systems/PlayerControllerSystem.cpp
	systems/CharacterSlotSystem.cpp
	systems/AnimationTreeSystem.cpp
	systems/BehaviorTreeSystem.cpp
	systems/NavMeshSystem.cpp
	recast/TileCacheNavMesh.cpp
	recast/PartitionedMesh.cpp
	recast/fastlz.c
	systems/CharacterSystem.cpp
	systems/SmartObjectSystem.cpp
	components/SmartObjectModule.cpp
	ui/SmartObjectEditor.cpp
	components/GoapPlannerModule.cpp
	systems/GoapRunnerSystem.cpp
	systems/PathFollowingSystem.cpp
	systems/GoapPlannerSystem.cpp
	components/GoapRunnerModule.cpp
	components/PathFollowingModule.cpp
	ui/GoapRunnerEditor.cpp
	ui/PathFollowingEditor.cpp
	ui/GoapPlannerEditor.cpp
	systems/ActuatorSystem.cpp
	ui/ActuatorEditor.cpp
	components/ActuatorModule.cpp
	systems/EventBus.cpp
	systems/EventHandlerSystem.cpp
	ui/EventHandlerEditor.cpp
	components/EventHandlerModule.cpp
	systems/PrefabSystem.cpp
	ui/PrefabInstanceEditor.cpp

	systems/ItemSystem.cpp
	components/ItemModule.cpp
	components/InventoryModule.cpp
	ui/ItemEditor.cpp
	ui/InventoryEditor.cpp

	ui/TransformEditor.cpp
	ui/RenderableEditor.cpp
	ui/PhysicsColliderEditor.cpp
	ui/RigidBodyEditor.cpp
	ui/LightEditor.cpp
	ui/CameraEditor.cpp
	ui/LodEditor.cpp
	ui/LodSettingsEditor.cpp
	ui/StaticGeometryEditor.cpp
	ui/StaticGeometryMemberEditor.cpp
	ui/ProceduralTextureEditor.cpp
	ui/ProceduralMaterialEditor.cpp
	ui/PrimitiveEditor.cpp
	ui/TriangleBufferEditor.cpp
	ui/CharacterSlotsEditor.cpp
	ui/AnimationTreeEditor.cpp
	ui/AnimationTreeTemplateEditor.cpp
	ui/CharacterEditor.cpp
	ui/CellGridEditor.cpp
	ui/LotEditor.cpp
	ui/DistrictEditor.cpp
	ui/TownEditor.cpp
	ui/RoofEditor.cpp
	ui/RoomEditor.cpp


	ui/ClearAreaEditor.cpp
	ui/FurnitureTemplateEditor.cpp
	ui/StartupMenuEditor.cpp
	ui/PlayerControllerEditor.cpp
	ui/BuoyancyInfoEditor.cpp
	ui/GoapBlackboardEditor.cpp
	ui/BehaviorTreeEditor.cpp
	ui/InlineBehaviorTreeEditor.cpp
	ui/NavMeshEditor.cpp
	ui/ActionDatabaseEditor.cpp
	ui/ActionDatabaseSingletonEditor.cpp
	ui/ActionDebugEditor.cpp
	ui/ComponentRegistration.cpp
	components/GoapBlackboard.cpp
	components/GoapExpression.cpp
	components/GoapGoal.cpp
	components/ActionDatabase.cpp
	components/ActionDatabaseModule.cpp
	components/ActionDebugModule.cpp
	components/GoapBlackboardModule.cpp
	components/NavMeshModule.cpp
	components/LightModule.cpp
	components/CameraModule.cpp
	components/LodModule.cpp
	components/StaticGeometryModule.cpp
	components/ProceduralTextureModule.cpp
	components/ProceduralMaterialModule.cpp
	components/PrimitiveModule.cpp
	components/TriangleBufferModule.cpp
	components/CharacterSlotsModule.cpp
	components/AnimationTreeModule.cpp
	components/AnimationTreeTemplateModule.cpp
	components/AnimationTree.cpp
	components/CharacterModule.cpp
	components/CellGridModule.cpp
	components/CellGridEditorsModule.cpp
	components/CellGrid.cpp
	components/StartupMenuModule.cpp
	components/PlayerControllerModule.cpp
	systems/DialogueSystem.cpp
	lua/LuaDialogueApi.cpp
	components/Formula.cpp
	components/CharacterClassDatabase.cpp
	components/CharacterClassComponent.cpp
	components/CharacterClassModule.cpp
	components/CharacterClassOverrideModule.cpp
	systems/CharacterClassSystem.cpp
	ui/CharacterClassEditor.cpp
	ui/CharacterClassOverrideEditor.cpp
	ui/CharacterClassDatabaseEditor.cpp
	components/BuoyancyInfoModule.cpp
	components/WaterPhysicsModule.cpp
	components/WaterPlaneModule.cpp
	components/SunModule.cpp
	components/SkyboxModule.cpp
	camera/EditorCamera.cpp
	gizmo/Gizmo.cpp
	gizmo/Cursor3D.cpp
	physics/physics.cpp
	lua/LuaState.cpp
	lua/LuaEntityApi.cpp
	lua/LuaComponentApi.cpp
	lua/LuaEventApi.cpp
	lua/LuaActionApi.cpp
	lua/LuaBehaviorTreeApi.cpp
	lua/LuaGameModeApi.cpp
	lua/LuaCharacterClassApi.cpp
)

set(EDITSCENE_HEADERS
	EditorApp.hpp
	components/Transform.hpp
	components/Renderable.hpp
	components/EntityName.hpp
	components/Relationship.hpp
	components/PhysicsCollider.hpp
	components/RigidBody.hpp
	components/BuoyancyInfo.hpp
	components/WaterPhysics.hpp
	components/WaterPlane.hpp
	components/Sun.hpp
	components/Skybox.hpp
	components/Light.hpp
	components/Camera.hpp
	components/Lod.hpp
	components/LodSettings.hpp
	components/StaticGeometry.hpp
	components/StaticGeometryMember.hpp
	components/ProceduralTexture.hpp
	components/ProceduralMaterial.hpp
	components/Primitive.hpp
	components/TriangleBuffer.hpp
	components/CharacterSlots.hpp
	components/AnimationTree.hpp
	components/Character.hpp
	components/CellGrid.hpp
	components/StartupMenu.hpp
	components/PlayerController.hpp
	systems/DialogueSystem.hpp
	lua/LuaDialogueApi.hpp
	components/Formula.hpp
	components/CharacterClassDatabase.hpp
	components/CharacterClassComponent.hpp
	ui/CharacterClassEditor.hpp
	ui/CharacterClassOverrideEditor.hpp
	ui/CharacterClassDatabaseEditor.hpp
	systems/CharacterClassSystem.hpp
	systems/StartupMenuSystem.hpp
	systems/PlayerControllerSystem.hpp
	systems/EditorUISystem.hpp
	systems/CellGridSystem.hpp
	systems/NormalDebugSystem.hpp
	systems/RoomLayoutSystem.hpp
	systems/FurnitureLibrary.hpp
	systems/ProceduralMaterialSystem.hpp
	systems/ProceduralMeshSystem.hpp
	systems/CharacterSlotSystem.hpp
	systems/AnimationTreeSystem.hpp
	systems/BehaviorTreeSystem.hpp
	systems/NavMeshSystem.hpp
	recast/TileCacheNavMesh.hpp
	recast/PartitionedMesh.hpp
	recast/fastlz.h
	systems/CharacterSystem.hpp
	systems/SmartObjectSystem.hpp
	components/SmartObject.hpp
	ui/SmartObjectEditor.hpp
	components/GoapPlanner.hpp
	components/PathFollowing.hpp
	components/GoapRunner.hpp
	ui/GoapPlannerEditor.hpp
	ui/GoapRunnerEditor.hpp
	ui/PathFollowingEditor.hpp
	systems/PrefabSystem.hpp
	systems/GoapRunnerSystem.hpp
	systems/PathFollowingSystem.hpp
	systems/GoapPlannerSystem.hpp
	components/Actuator.hpp
	ui/ActuatorEditor.hpp
	systems/EventBus.hpp
	components/EventHandler.hpp
	systems/EventHandlerSystem.hpp
	ui/EventHandlerEditor.hpp
	components/PrefabInstance.hpp
	ui/PrefabInstanceEditor.hpp

	systems/ItemSystem.hpp
	components/Item.hpp
	components/Inventory.hpp
	ui/ItemEditor.hpp
	ui/InventoryEditor.hpp

	systems/ProceduralTextureSystem.hpp
	systems/StaticGeometrySystem.hpp
	systems/SceneSerializer.hpp
	systems/PhysicsSystem.hpp
	systems/BuoyancySystem.hpp
	systems/EditorSunSystem.hpp
	systems/EditorSkyboxSystem.hpp
	systems/EditorWaterPlaneSystem.hpp
	systems/LightSystem.hpp
	systems/CameraSystem.hpp
	systems/LodSystem.hpp
	ui/ComponentEditor.hpp
	ui/ComponentRegistry.hpp
	ui/ComponentRegistration.hpp
	ui/TransformEditor.hpp
	ui/RenderableEditor.hpp
	ui/PhysicsColliderEditor.hpp
	ui/RigidBodyEditor.hpp
	ui/LightEditor.hpp
	ui/CameraEditor.hpp
	ui/LodEditor.hpp
	ui/LodSettingsEditor.hpp
	ui/StaticGeometryEditor.hpp
	ui/StaticGeometryMemberEditor.hpp
	ui/ProceduralTextureEditor.hpp
	ui/ProceduralMaterialEditor.hpp
	ui/PrimitiveEditor.hpp
	ui/TriangleBufferEditor.hpp
	ui/CharacterSlotsEditor.hpp
	ui/AnimationTreeEditor.hpp
	ui/AnimationTreeTemplateEditor.hpp
	ui/CharacterEditor.hpp
	ui/CellGridEditor.hpp
	ui/LotEditor.hpp
	ui/DistrictEditor.hpp
	ui/TownEditor.hpp
	ui/RoofEditor.hpp
	ui/RoomEditor.hpp


	ui/ClearAreaEditor.hpp
	ui/FurnitureTemplateEditor.hpp
	ui/StartupMenuEditor.hpp
	ui/PlayerControllerEditor.hpp
	ui/BuoyancyInfoEditor.hpp
	ui/GoapBlackboardEditor.hpp
	ui/GoapBlackboardComponentEditor.hpp
	ui/BehaviorTreeEditor.hpp
	ui/InlineBehaviorTreeEditor.hpp
	ui/NavMeshEditor.hpp
	ui/NavMeshGeometrySourceEditor.hpp
	ui/ActionDatabaseEditor.hpp
	ui/ActionDatabaseSingletonEditor.hpp
	ui/ActionDebugEditor.hpp
	components/GoapBlackboard.hpp
	components/GoapExpression.hpp
	components/NavMesh.hpp
	components/BehaviorTree.hpp
	components/GoapAction.hpp
	components/GoapGoal.hpp
	components/ActionDatabase.hpp
	components/ActionDebug.hpp
	camera/EditorCamera.hpp
	gizmo/Gizmo.hpp
	gizmo/Cursor3D.hpp
	physics/physics.h
	lua/LuaState.hpp
	lua/LuaEntityApi.hpp
	lua/LuaComponentApi.hpp
	lua/LuaEventApi.hpp
	lua/LuaActionApi.hpp
	lua/LuaBehaviorTreeApi.hpp
	lua/LuaGameModeApi.hpp
	lua/LuaCharacterClassApi.hpp
)

add_executable(editSceneEditor ${EDITSCENE_SOURCES} ${EDITSCENE_HEADERS})

# Define JPH_DEBUG_RENDERER for physics debug drawing
target_compile_definitions(editSceneEditor PRIVATE JPH_DEBUG_RENDERER)

target_link_libraries(editSceneEditor
	OgreMain
	OgreBites
	OgreOverlay
	OgreMeshLodGenerator
	flecs::flecs_static
	nlohmann_json::nlohmann_json
	Jolt::Jolt
	OgreProcedural::OgreProcedural
	RecastNavigation::Recast
	RecastNavigation::Detour
	RecastNavigation::DetourTileCache
	RecastNavigation::DetourCrowd
	RecastNavigation::DebugUtils
	lua
)

target_include_directories(editSceneEditor PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/recastnavigation/Recast/Include
	${CMAKE_CURRENT_SOURCE_DIR}/recastnavigation/Detour/Include
	${CMAKE_CURRENT_SOURCE_DIR}/recastnavigation/DetourTileCache/Include
	${CMAKE_CURRENT_SOURCE_DIR}/recastnavigation/DetourCrowd/Include
	${CMAKE_CURRENT_SOURCE_DIR}/recastnavigation/DebugUtils/Include
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
	${CMAKE_SOURCE_DIR}/src/lua/lpeg-1.1.0
)

# ---------------------------------------------------------------------------
# Tests: Lua API standalone tests
# ---------------------------------------------------------------------------
# These standalone tests verify the Lua API functions work correctly.
# They do not require OGRE or Flecs - only Lua and the core component types.

# Test: Entity Lua API
add_executable(entity_lua_test
	tests/entity_lua_test.cpp
	tests/lua_test_stubs.cpp
)

target_link_libraries(entity_lua_test
	lua
)

target_include_directories(entity_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Test: Component Lua API
add_executable(component_lua_test
	tests/component_lua_test.cpp
	tests/lua_test_stubs.cpp
)

target_link_libraries(component_lua_test
	lua
)

target_include_directories(component_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Test: Event Lua API
add_executable(event_lua_test
	tests/event_lua_test.cpp
	tests/lua_test_stubs.cpp
)

target_link_libraries(event_lua_test
	lua
)

target_include_directories(event_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Test: ActionDatabase Lua API
add_executable(action_db_lua_test
	tests/action_db_lua_test.cpp
	components/ActionDatabase.cpp
	components/GoapBlackboard.cpp
	components/GoapGoal.cpp
	components/GoapExpression.cpp
	lua/LuaActionApi.cpp
)

target_link_libraries(action_db_lua_test
	lua
	flecs::flecs_static
	nlohmann_json::nlohmann_json
)

target_include_directories(action_db_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Test: Behavior Tree Lua API
add_executable(behavior_tree_lua_test
	tests/behavior_tree_lua_test.cpp
	lua/LuaBehaviorTreeApi.cpp
	lua/LuaGameModeApi.cpp
	lua/LuaEntityApi.cpp
	GameMode.cpp
	components/GoapBlackboard.cpp
)

target_link_libraries(behavior_tree_lua_test
	lua
	flecs::flecs_static
	OgreMain
)

target_include_directories(behavior_tree_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

target_compile_definitions(behavior_tree_lua_test PRIVATE flecs_STATIC)

# Test: EventParams C++ API (standalone, no Lua dependency)
add_executable(event_params_test
	tests/event_params_test.cpp
)

target_link_libraries(event_params_test
	lua
)

target_include_directories(event_params_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Test: Game Mode Lua API
add_executable(game_mode_lua_test
	tests/game_mode_lua_test.cpp
	tests/lua_test_stubs.cpp
)

target_link_libraries(game_mode_lua_test
	lua
)

target_include_directories(game_mode_lua_test PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_SOURCE_DIR}/tests
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
)

# Copy local resources (materials, etc.)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/resources")
	file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/resources"
		DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# Copy resources from main build
add_custom_command(TARGET editSceneEditor POST_BUILD
	COMMAND ${CMAKE_COMMAND} -E copy_directory
	"${CMAKE_BINARY_DIR}/resources"
	"${CMAKE_CURRENT_BINARY_DIR}/resources"
	COMMAND ${CMAKE_COMMAND} -E copy_directory
	"${CMAKE_BINARY_DIR}/characters"
	"${CMAKE_CURRENT_BINARY_DIR}/characters"
	COMMAND ${CMAKE_COMMAND} -E copy_directory
	"${CMAKE_BINARY_DIR}/lua-scripts"
	"${CMAKE_CURRENT_BINARY_DIR}/lua-scripts"
	COMMAND ${CMAKE_COMMAND} -E copy
	"${CMAKE_CURRENT_SOURCE_DIR}/resources.cfg"
	"${CMAKE_CURRENT_BINARY_DIR}/resources.cfg"
	# Re-copy editScene-specific resources so they aren't overwritten
	COMMAND ${CMAKE_COMMAND} -E copy_directory
	"${CMAKE_CURRENT_SOURCE_DIR}/resources"
	"${CMAKE_CURRENT_BINARY_DIR}/resources"
	COMMENT "Copying resources to editSceneEditor build directory"
)
