-- ==============================================
-- Extra2D - 2D Game Engine
-- Build System: Xmake
-- 
-- 支持平台:
-- - Windows (MinGW)
-- - Linux
-- - macOS
-- - Nintendo Switch
-- 
-- 窗口后端: SDL2 / GLFW
-- ==============================================

-- 项目元信息
set_project("Extra2D")
set_version("1.2.0")
set_license("MIT")

-- 语言和编码设置
set_languages("c++17")
set_encodings("utf-8")

-- 构建模式
add_rules("mode.debug", "mode.release")

-- ==============================================
-- 构建选项
-- ==============================================

option("debug_logs")
    set_default(false)
    set_showmenu(true)
    set_description("Enable debug logging")
option_end()

option("window_backend")
    set_default("sdl2")
    set_showmenu(true)
    set_description("Window backend: sdl2 or glfw")
    set_values("sdl2", "glfw")
option_end()

option("render_backend")
    set_default("opengl")
    set_showmenu(true)
    set_description("Render backend: opengl or vulkan")
    set_values("opengl", "vulkan")
option_end()

-- ==============================================
-- 平台检测与配置
-- ==============================================

local host_plat = os.host()
local target_plat = get_config("plat") or host_plat
local supported_plats = {mingw = true, windows = true, linux = true, macosx = true, switch = true}

-- 自动选择平台
if not supported_plats[target_plat] then
    if host_plat == "windows" then
        target_plat = "mingw"
    elseif host_plat == "linux" then
        target_plat = "linux"
    elseif host_plat == "macosx" then
        target_plat = "macosx"
    else
        error("Unsupported platform: " .. target_plat .. ". Supported platforms: mingw, windows, linux, macosx, switch")
    end
end

set_plat(target_plat)

-- 设置架构
if target_plat == "switch" then
    set_arch("arm64")
elseif target_plat == "mingw" or target_plat == "windows" then
    set_arch("x86_64")
elseif target_plat == "linux" then
    set_arch("x86_64")
elseif target_plat == "macosx" then
    set_arch("x86_64")
end

-- ==============================================
-- 加载工具链配置
-- ==============================================

if target_plat == "switch" then
    includes("xmake/toolchains/switch.lua")
    set_toolchains("switch")
elseif target_plat == "mingw" then
    set_toolchains("mingw")
end

-- ==============================================
-- 添加依赖包
-- ==============================================

if target_plat ~= "switch" then
    add_requires("glm")
    add_requires("nlohmann_json")
    
    -- 窗口后端依赖
    local backend = get_config("window_backend") or "sdl2"
    if backend == "glfw" then
        add_requires("glfw")
    else
        local sdl2_configs = {
            configs = {
                wayland = false
            }
        }
        if target_plat == "linux" then
            local is_wayland = os.getenv("XDG_SESSION_TYPE") == "wayland"
            if is_wayland then
                sdl2_configs.configs.wayland = true
            end
        end
        add_requires("libsdl2", sdl2_configs)
    end
end


-- ==============================================
-- 加载构建目标
-- ==============================================

-- 加载引擎库定义
includes("xmake/engine.lua")

-- 定义引擎库
define_extra2d_engine()

-- ==============================================
-- Shader文件安装函数
-- ==============================================

function install_shaders(target)
    local plat = get_config("plat") or os.host()
    local targetdir = target:targetdir()
    
    local shader_src = "Extra2D/shaders"
    
    if plat == "switch" then
        local romfs_dir = path.join(targetdir, "romfs")
        local shader_dest = path.join(romfs_dir, "shaders")
        
        os.rm(shader_dest)
        os.cp(shader_src, shader_dest)
        print("Shaders installed to romfs: " .. shader_dest)
    else
        local shader_dest = path.join(targetdir, "shaders")
        
        os.rm(shader_dest)
        os.cp(shader_src, shader_dest)
        print("Shaders installed to: " .. shader_dest)
    end
end

-- ==============================================
-- 示例程序
-- ==============================================

-- 基础示例
target("demo_basic")
    set_kind("binary")
    set_default(false)
    
    add_deps("extra2d")
    add_files("examples/basic/main.cpp")
    
    -- 平台配置
    local plat = get_config("plat") or os.host()
    local backend = get_config("window_backend") or "sdl2"
    if plat == "mingw" or plat == "windows" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("opengl32", "glu32", "winmm", "imm32", "version", "setupapi")
    elseif plat == "linux" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("GL", "dl", "pthread")
    elseif plat == "macosx" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_frameworks("OpenGL", "Cocoa", "IOKit", "CoreVideo")
    end
    
    -- 构建后安装Shader文件
    after_build(install_shaders)
target_end()

-- 文字渲染示例
target("demo_text_rendering")
    set_kind("binary")
    set_default(false)
    
    add_deps("extra2d")
    add_files("examples/text_rendering/main.cpp")
    
    -- 平台配置
    local plat = get_config("plat") or os.host()
    local backend = get_config("window_backend") or "sdl2"
    if plat == "mingw" or plat == "windows" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("opengl32", "glu32", "winmm", "imm32", "version", "setupapi")
    elseif plat == "linux" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("GL", "dl", "pthread")
    elseif plat == "macosx" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_frameworks("OpenGL", "Cocoa", "IOKit", "CoreVideo")
    end
    
    -- 构建后安装Shader文件和assets
    after_build(function (target)
        -- 安装shaders
        local plat = get_config("plat") or os.host()
        local targetdir = target:targetdir()
        local shader_src = "Extra2D/shaders"
        local shader_dest = path.join(targetdir, "shaders")
        os.rm(shader_dest)
        os.cp(shader_src, shader_dest)
        print("Shaders installed to: " .. shader_dest)
        
        -- 复制assets目录
        local assets_src = "examples/text_rendering/assets"
        local assets_dest = path.join(targetdir, "assets")
        if os.exists(assets_src) then
            os.rm(assets_dest)
            os.cp(assets_src, assets_dest)
            print("Assets installed to: " .. assets_dest)
        end
    end)
target_end()

-- Hello Module 示例 - 展示如何创建自定义模块
target("demo_hello_module")
    set_kind("binary")
    set_default(false)
    
    add_deps("extra2d")
    add_files("examples/hello_module/*.cpp")
    add_includedirs("examples/hello_module")
    
    -- 平台配置
    local plat = get_config("plat") or os.host()
    local backend = get_config("window_backend") or "sdl2"
    if plat == "mingw" or plat == "windows" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("opengl32", "glu32", "winmm", "imm32", "version", "setupapi")
    elseif plat == "linux" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("GL", "dl", "pthread")
    elseif plat == "macosx" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_frameworks("OpenGL", "Cocoa", "IOKit", "CoreVideo")
    end
    
    -- 构建后安装Shader文件
    after_build(install_shaders)
target_end()

-- 图片显示示例
target("demo_image_display")
    set_kind("binary")
    set_default(false)
    
    add_deps("extra2d")
    add_files("examples/image_display/main.cpp")
    
    -- 平台配置
    local plat = get_config("plat") or os.host()
    local backend = get_config("window_backend") or "sdl2"
    if plat == "mingw" or plat == "windows" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("opengl32", "glu32", "winmm", "imm32", "version", "setupapi")
    elseif plat == "linux" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_syslinks("GL", "dl", "pthread")
    elseif plat == "macosx" then
        add_packages("glm", "nlohmann_json")
        if backend == "glfw" then
            add_packages("glfw")
        else
            add_packages("libsdl2")
        end
        add_frameworks("OpenGL", "Cocoa", "IOKit", "CoreVideo")
    end
    
    -- 构建后安装Shader文件和assets
    after_build(function (target)
        -- 安装shaders
        local plat = get_config("plat") or os.host()
        local targetdir = target:targetdir()
        local shader_src = "Extra2D/shaders"
        local shader_dest = path.join(targetdir, "shaders")
        os.rm(shader_dest)
        os.cp(shader_src, shader_dest)
        print("Shaders installed to: " .. shader_dest)
        
        -- 复制assets目录
        local assets_src = "examples/image_display/assets"
        local assets_dest = path.join(targetdir, "assets")
        if os.exists(assets_src) then
            os.rm(assets_dest)
            os.cp(assets_src, assets_dest)
            print("Assets installed to: " .. assets_dest)
        end
    end)
target_end()
