# The screens, motion, state machine and deck live in ../../ui and are
# shared verbatim with the desktop simulator (sim/). The fonts are the
# committed lv_font_conv output in generated/fonts.
#
# CONFIGURE_DEPENDS re-runs the glob on every build rather than only at
# configure time. Without it, dropping a new file into ui/ -- router.c
# lands in Task 6, and the content step may add fonts -- does not reach an
# incremental build: the file sits in the directory and the link fails, or
# worse, nothing references it yet and it is silently absent.
#
# It has to be guarded. ESP-IDF includes every component CMakeLists.txt a
# second time in CMake *script* mode to extract REQUIRES
# (tools/cmake/scripts/component_get_requirements.cmake), and
# CONFIGURE_DEPENDS is rejected outright in script mode -- unguarded, the
# build dies at configure with "CONFIGURE_DEPENDS is invalid for script and
# find package modes". The glob result is unused on that pass, so a plain
# glob there is fine.
#
# Argument order matters: the output variable first, CONFIGURE_DEPENDS
# after. Reversed, CMake takes CONFIGURE_DEPENDS as the variable name,
# treats the real name as a glob pattern, and sets it empty -- compiling
# none of ui/ and none of the fonts.
if(CMAKE_SCRIPT_MODE_FILE)
    file(GLOB MAGIC8_UI_SOURCES   ${CMAKE_CURRENT_LIST_DIR}/../../ui/*.c)
    file(GLOB MAGIC8_FONT_SOURCES ${CMAKE_CURRENT_LIST_DIR}/generated/fonts/*.c)
else()
    file(GLOB MAGIC8_UI_SOURCES   CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/../../ui/*.c)
    file(GLOB MAGIC8_FONT_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/generated/fonts/*.c)
endif()

# generated/content_data.c is the move table + QR bit-arrays (definitions,
# compiled once; the header only declares). Produced by
# ../../tools/generate-qr.mjs from ../../content/moves.json, committed.
idf_component_register(SRCS "app_main.c" "device_seed.c" "lvgl_port.c" "input.c"
                             "imu_qmi8658.c" "power_sleep.c" "battery_monitor.c"
                             "i2c_bsp.c" "pmic_axp2101.c" "display_bsp.c" "touch_bsp.c"
                             "generated/content_data.c"
                             ${MAGIC8_UI_SOURCES} ${MAGIC8_FONT_SOURCES}
                       INCLUDE_DIRS "." "../../ui" "generated"
                       REQUIRES lvgl esp_timer esp_lcd esp_app_format)

# lv_font_conv emits `#ifdef LV_LVGL_H_INCLUDE_SIMPLE #include "lvgl.h"`;
# without this it falls back to "lvgl/lvgl.h", which does not resolve here.
target_compile_definitions(${COMPONENT_LIB} PRIVATE LV_LVGL_H_INCLUDE_SIMPLE)

# generated/ is committed, not gitignored -- deterministic output from
# content/moves.json and assets/fonts/*.ttf, both also committed. Fails
# the build (not a warning) if it's stale; see
# ../../tools/check-generated-fresh.sh for why that matters here.
add_custom_target(magic8_check_generated_fresh ALL
    COMMAND bash ${CMAKE_CURRENT_LIST_DIR}/../../tools/check-generated-fresh.sh
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../..
    COMMENT "Checking firmware/main/generated/ was produced from the current content/moves.json and assets/fonts/*.ttf"
    VERBATIM
)
add_dependencies(${COMPONENT_LIB} magic8_check_generated_fresh)

# The device LVGL config must match the simulator's outside the host-only
# allowlist; see the script for why.
add_custom_target(magic8_check_lv_conf_drift ALL
    COMMAND bash ${CMAKE_CURRENT_LIST_DIR}/../../tools/check-lv-conf-drift.sh
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../..
    COMMENT "Checking firmware/main/lv_conf.h matches sim/lv_conf.h"
    VERBATIM
)
add_dependencies(${COMPONENT_LIB} magic8_check_lv_conf_drift)
