set(platform_srcs
        "src/runtime/platform_core_runtime.c"
        "src/runtime/platform_storage.c"
        "src/runtime/platform_time.c"
        "src/runtime/platform_https_client.c"
        "src/runtime/platform_ota.c"
        "src/runtime/platform_lan_http_server.c"
        "src/runtime/platform_ble_gatt_server.c"
        "src/platform_console_driver.c"
        "src/runtime/platform_behavior_runtime.c"
        "src/runtime/platform_multimodal_runtime.c"
        "src/runtime/platform_lcd_runtime.c"
        "src/platform_stepper_driver.c"
        "src/platform_digital_input_driver.c"
        "src/platform_io_effects.c"
        "src/lcd_direct.c"
        "src/lcd_direct_assets.c"
        "third_party/lodepng/lodepng.cpp"
        "third_party/lodepng/lodepng_esp_alloc.cpp"
        "third_party/lodepng/lodepng_esp_bridge.cpp"
        "src/multimodal/audio_pipeline.c"
        "src/multimodal/transport_ws.c"
        "src/wifi_portal_bridge.cpp"
        "src/platform_behavior_stubs.c"
)

set(platform_priv_requires
        78__esp-wifi-connect
        bt
        cxx
        driver
        esp_audio_codec
        esp_codec_dev
        esp_driver_gpio
        esp_driver_i2c
        esp_driver_i2s
        esp_driver_rmt
        esp_driver_spi
        esp_event
        esp_app_format
        app_update
        esp_http_client
        esp_http_server
        esp_lcd
        esp_netif
        esp_psram
        heap
        esp_system
        esp_timer
        esp_wifi
        esp_websocket_client
        json
        littlefs
        log
        espressif__mdns
        mbedtls
        nvs_flash
)

if(CONFIG_TQ_NETWORK_DIAGNOSTICS_ENABLE)
    list(APPEND platform_priv_requires lwip)
endif()

idf_component_register(
    SRCS
        ${platform_srcs}
    INCLUDE_DIRS
        "include"
    PRIV_INCLUDE_DIRS
        "internal"
        "third_party/lodepng"
    PRIV_REQUIRES
        ${platform_priv_requires}
)

set_source_files_properties(
    "third_party/lodepng/lodepng.cpp"
    PROPERTIES
        COMPILE_DEFINITIONS "LODEPNG_NO_COMPILE_ENCODER;LODEPNG_NO_COMPILE_DISK;LODEPNG_NO_COMPILE_CPP;LODEPNG_NO_COMPILE_ALLOCATORS"
)

set(platform_audio_prompt_src_dir "${CMAKE_CURRENT_SOURCE_DIR}/assets/audio_prompts")
set(platform_lcd_asset_src_dir "${CMAKE_CURRENT_SOURCE_DIR}/assets/lcd")
set(platform_asset_image_dir "${CMAKE_CURRENT_BINARY_DIR}/asset_image")
set(platform_asset_stage_stamp "${CMAKE_CURRENT_BINARY_DIR}/asset_image.stamp")
file(GLOB_RECURSE platform_audio_prompt_files CONFIGURE_DEPENDS
    "${platform_audio_prompt_src_dir}/*.opus"
)
set(platform_lcd_png_assets
    buyunqian1
    jichubiaoqing0
    jichubiaoqing1
    jichubiaoqing2
    listening0
    listening1
    listening2
    muyu0
    muyu1
    muyu2
    chouqian_qianzhi0
    chouqian_qianzhi1
    chouqian_qianzhi2
    chouqian_buyun0
    chouqian_buyun1
    chouqian_buyun2
    chouqian_heping0
    chouqian_heping1
    chouqian_heping2
    chouqian_shangshang0
    chouqian_shangshang1
    chouqian_shangshang2
    chouqian_xiaoji0
    chouqian_xiaoji1
    chouqian_xiaoji2
    chouqian_zhongshang0
    chouqian_zhongshang1
    chouqian_zhongshang2
    qianzhiwuxing0
    shui0
    shui1
    mu0
    mu1
    huo0
    huo1
    tu0
    tu1
    jin0
    jin1
    spin0
    south0
    southwest0
    west0
    northwest0
    north0
    northeast0
    east0
    southeast0
    icon_shui0
    icon_mu0
    icon_huo0
    icon_tu0
    icon_jin0
    qrcode
)
set(platform_lcd_png_asset_files)
foreach(lcd_png_asset IN LISTS platform_lcd_png_assets)
    list(APPEND platform_lcd_png_asset_files
        "${platform_lcd_asset_src_dir}/${lcd_png_asset}.png"
    )
endforeach()

set(platform_asset_stage_commands)
foreach(audio_file IN LISTS platform_audio_prompt_files)
    file(RELATIVE_PATH audio_rel_path "${platform_audio_prompt_src_dir}" "${audio_file}")
    list(APPEND platform_asset_stage_commands
        COMMAND ${CMAKE_COMMAND} -E make_directory "${platform_asset_image_dir}/audio_prompts"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${audio_file}"
            "${platform_asset_image_dir}/audio_prompts/${audio_rel_path}"
    )
endforeach()
foreach(lcd_png_asset IN LISTS platform_lcd_png_assets)
    list(APPEND platform_asset_stage_commands
        COMMAND ${CMAKE_COMMAND} -E make_directory "${platform_asset_image_dir}/lcd"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${platform_lcd_asset_src_dir}/${lcd_png_asset}.png"
            "${platform_asset_image_dir}/lcd/${lcd_png_asset}.png"
    )
endforeach()

add_custom_command(
    OUTPUT "${platform_asset_stage_stamp}"
    COMMAND ${CMAKE_COMMAND} -E rm -rf "${platform_asset_image_dir}"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${platform_asset_image_dir}"
    ${platform_asset_stage_commands}
    COMMAND ${CMAKE_COMMAND} -E touch "${platform_asset_stage_stamp}"
    DEPENDS
        ${platform_audio_prompt_files}
        ${platform_lcd_png_asset_files}
)

littlefs_create_partition_image(
    gifpack
    "${platform_asset_image_dir}"
    FLASH_IN_PROJECT
    DEPENDS "${platform_asset_stage_stamp}"
)
