# TODO: Check for env or command line "ZMK_CONFIG" setting.
#  * That directory should load
#    * defconfigs,
#    * .conf file,
#    * single overlay,
#    * or per board/shield.

list(APPEND KEYMAP_DIRS ${BOARD_DIR})
get_filename_component(BOARD_DIR_NAME ${BOARD_DIR} NAME)
# Give a shield like `kyria_rev2_left` we want to use `kyria_rev2` and `kyria` as candidate names for
# overlay/conf/keymap files.
if(DEFINED SHIELD)
    list(APPEND KEYMAP_DIRS ${SHIELD_DIRS})
    foreach(s ${SHIELD_AS_LIST})
        if (DEFINED SHIELD_DIR_${s})
            get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
            list(APPEND shield_candidate_names ${shield_dir_name})
        endif()
        string(REPLACE "_" ";" S_PIECES ${s})
        list(LENGTH S_PIECES S_PIECES_LEN)
        while(NOT S_PIECES STREQUAL "")
            list(POP_BACK S_PIECES)
            list(JOIN S_PIECES "_" s_substr)
            if ("${s_substr}" STREQUAL "" OR "${s_substr}" STREQUAL "${shield_dir_name}")
                break()
            endif()
            list(APPEND shield_candidate_names ${s_substr})
        endwhile()
    endforeach()
endif()

if (ZMK_CONFIG)
    if (EXISTS ${ZMK_CONFIG})
        message(STATUS "ZMK Config directory: ${ZMK_CONFIG}")
        list(PREPEND KEYMAP_DIRS "${ZMK_CONFIG}")

        if (DEFINED SHIELD)
            foreach (s ${shield_candidate_names} ${SHIELD_AS_LIST})
                if (DEFINED ${SHIELD_DIR_${s}})
                    get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
                endif()
                list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}_${BOARD}.overlay")
                list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}.overlay")
                if (NOT "${shield_dir_name}" STREQUAL "${s}")
                    list(APPEND config_candidates "${ZMK_CONFIG}/${shield_dir_name}_${BOARD}.conf")
                    list(APPEND config_candidates "${ZMK_CONFIG}/${shield_dir_name}.conf")
                endif()
                list(APPEND config_candidates "${ZMK_CONFIG}/${s}_${BOARD}.conf")
                list(APPEND config_candidates "${ZMK_CONFIG}/${s}.conf")
            endforeach()
        endif()

        # TODO: Board revisions?
        list(APPEND overlay_candidates "${ZMK_CONFIG}/${BOARD_DIR_NAME}.overlay")
        list(APPEND overlay_candidates "${ZMK_CONFIG}/${BOARD}.overlay")
        list(APPEND overlay_candidates "${ZMK_CONFIG}/default.overlay")
        list(APPEND config_candidates "${ZMK_CONFIG}/${BOARD_DIR_NAME}.conf")
        list(APPEND config_candidates "${ZMK_CONFIG}/${BOARD}.conf")
        list(APPEND config_candidates "${ZMK_CONFIG}/default.conf")

        foreach(overlay ${overlay_candidates})
            if (EXISTS "${overlay}")
                message(STATUS "ZMK Config devicetree overlay: ${overlay}")
                list(APPEND shield_dts_files "${overlay}")
                break()
            endif()
        endforeach()

        foreach(conf ${config_candidates})
            if (EXISTS "${conf}")
                message(STATUS "ZMK Config Kconfig: ${conf}")
                list(APPEND shield_conf_files "${conf}")
            endif()
        endforeach()
    else()
        message(WARNING "Unable to locate ZMK config at: ${ZMK_CONFIG}")
    endif()
endif()


if(NOT KEYMAP_FILE)
    message("${NORMALIZED_BOARD_TARGET} for ${NORMALIZED_BOARD_QUALIFIERS} for ${BOARD} with version ${BOARD_REVISION}")
    foreach(keymap_dir ${KEYMAP_DIRS})
        foreach(keymap_prefix ${shield_candidate_names} ${SHIELD_AS_LIST} ${SHIELD_DIR} "${BOARD}_${BOARD_REVISION_STRING}" ${BOARD} ${BOARD_DIR_NAME})
            if (EXISTS ${keymap_dir}/${keymap_prefix}.keymap)
                set(KEYMAP_FILE "${keymap_dir}/${keymap_prefix}.keymap" CACHE STRING "Selected keymap file")
                message(STATUS "Using keymap file: ${KEYMAP_FILE}")
                set(EXTRA_DTC_OVERLAY_FILE ${KEYMAP_FILE})
                break()
            endif()
        endforeach()
    endforeach()
else()
    message(STATUS "Using keymap file: ${KEYMAP_FILE}")
    set(EXTRA_DTC_OVERLAY_FILE ${KEYMAP_FILE})
endif()

if (NOT KEYMAP_FILE)
    message(WARNING "Failed to locate keymap file!")
endif()
