cmake_minimum_required(VERSION 3.15)
project(OnlineRpg VERSION 1.0.0 LANGUAGES C CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 包含目录
include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/include/core
    ${PROJECT_SOURCE_DIR}/include/common
    ${PROJECT_SOURCE_DIR}/include/server
    ${PROJECT_SOURCE_DIR}/include/client
)

# 平台特定设置
if(WIN32)
    # Windows 平台
    add_definitions(-D_WIN32_WINNT=0x0601)  # Windows 7+
    set(PLATFORM_LIBS ws2_32)
    message(STATUS "Building for Windows")
else()
    # Linux/Unix 平台
    set(PLATFORM_LIBS pthread)
    message(STATUS "Building for Unix/Linux")
endif()

# SQLite3 设置
# 如果系统没有安装SQLite3，可以使用amalgamation版本
find_package(SQLite3)
if(SQLite3_FOUND)
    message(STATUS "Found SQLite3: ${SQLite3_INCLUDE_DIRS}")
    include_directories(${SQLite3_INCLUDE_DIRS})
    set(SQLITE_LIBS ${SQLite3_LIBRARIES})
    set(SQLITE_SRC "")
else()
    message(STATUS "SQLite3 not found, using amalgamation")
    # 使用amalgamation版本
    include_directories(${PROJECT_SOURCE_DIR}/lib)
    set(SQLITE_LIBS "")
    # 将sqlite3.c添加到源文件列表
    set(SQLITE_SRC ${PROJECT_SOURCE_DIR}/lib/sqlite3.c)
    message(STATUS "Using SQLite amalgamation: ${SQLITE_SRC}")
endif()

# 核心库 (Core: 多态、模板+链表)
# 注意: HistoryLog.h 是纯模板类，不需要.cpp文件
set(CORE_SOURCES
    src/core/ICharacter.cpp
    src/core/ISkill.cpp
    src/core/Warrior.cpp
    src/core/Mage.cpp
    src/core/Skills.cpp
)

# 通用库 (Common: 网络封装、协议)
set(COMMON_SOURCES
    src/common/SocketWrapper.cpp
    src/common/Protocol.cpp
    src/common/Database.cpp
    ${SQLITE_SRC}
)

# 服务器源文件
set(SERVER_SOURCES
    src/server/GameServer.cpp
    src/server/ClientHandler.cpp
    src/server/GameLobby.cpp
    src/server/BattleRoom.cpp
    src/server/BattleRoomManager.cpp
    src/server/ServerMain.cpp
)

# 客户端源文件
set(CLIENT_SOURCES
    src/client/GameClient.cpp
    src/client/ClientMain.cpp
)

# 构建核心库
add_library(core STATIC ${CORE_SOURCES})

# 构建通用库
add_library(common STATIC ${COMMON_SOURCES})
target_link_libraries(common ${SQLITE_LIBS} ${PLATFORM_LIBS})

# 构建服务器可执行文件
add_executable(server ${SERVER_SOURCES})
target_link_libraries(server core common ${SQLITE_LIBS} ${PLATFORM_LIBS})

# 构建客户端可执行文件
add_executable(client ${CLIENT_SOURCES})
target_link_libraries(client core common ${PLATFORM_LIBS})

# 编译选项
if(MSVC)
    # Visual Studio 编译器选项
    target_compile_options(core PRIVATE /W4)
    target_compile_options(common PRIVATE /W4)
    target_compile_options(server PRIVATE /W4)
    target_compile_options(client PRIVATE /W4)
else()
    # GCC/Clang 编译器选项
    target_compile_options(core PRIVATE -Wall -Wextra -Wpedantic)
    target_compile_options(common PRIVATE -Wall -Wextra -Wpedantic)
    target_compile_options(server PRIVATE -Wall -Wextra -Wpedantic)
    target_compile_options(client PRIVATE -Wall -Wextra -Wpedantic)
endif()

# 安装规则
install(TARGETS server client
    RUNTIME DESTINATION bin
)

# 打印构建信息
message(STATUS "========================================")
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "========================================")
