#!/usr/bin/env bash

menu_modules_by_prefix() {
  local prefix="$1"
  local title="$2"
  local matching_modules=()
  local module_id=""
  local selection=""
  local index=1

  while IFS= read -r module_id; do
    [[ "$module_id" == "$prefix"/* ]] || continue
    matching_modules+=("$module_id")
  done < <(registry_list)

  if [[ "${#matching_modules[@]}" -eq 0 ]]; then
    ui_warn "Aucun module disponible pour $title"
    ui_pause
    return 0
  fi

  ui_section "$title"
  for module_id in "${matching_modules[@]}"; do
    printf ' %d. %s\n' "$index" "$module_id"
    index=$((index + 1))
  done
  printf ' 0. Retour\n'

  selection="$(prompt_select_number "Selectionner un module" 0 "${#matching_modules[@]}")"
  if [[ "$selection" == "0" ]]; then
    return 0
  fi

  dispatcher_prompt_and_run_module "${matching_modules[$((selection - 1))]}"
  ui_pause
}

menu_category_selection() {
  local categories=("network" "containers" "boot" "hardware")
  local selection=""

  ui_section "Installation par categorie"
  printf ' 1. network\n'
  printf ' 2. containers\n'
  printf ' 3. boot\n'
  printf ' 4. hardware\n'
  printf ' 0. Retour\n'

  selection="$(prompt_select_number "Selectionner une categorie" 0 4)"
  case "$selection" in
    0) return 0 ;;
    1) menu_modules_by_prefix "network" "Configuration reseau" ;;
    2) menu_modules_by_prefix "containers" "Conteneurs" ;;
    3) menu_modules_by_prefix "boot" "Configuration du boot" ;;
    4) menu_modules_by_prefix "hardware" "Materiel" ;;
  esac
}

menu_system_configuration() {
  menu_modules_by_prefix "system" "Configuration systeme"
}

menu_tests() {
  local selection=""
  local modules=()
  local module_id=""
  local index=1

  while true; do
    ui_section "Tests"
    printf ' 1. Smoke test\n'
    printf ' 2. Tous les modules\n'
    printf ' 3. Un module\n'
    printf ' 0. Retour\n'

    selection="$(prompt_select_number "Selectionner un test" 0 3)"

    case "$selection" in
      0)
        return 0
        ;;
      1)
        test_runner_smoke
        ui_pause
        ;;
      2)
        test_runner_all_modules
        ui_pause
        ;;
      3)
        modules=()
        index=1
        while IFS= read -r module_id; do
          [[ -n "$module_id" ]] || continue
          modules+=("$module_id")
        done < <(registry_list)

        if [[ "${#modules[@]}" -eq 0 ]]; then
          ui_warn "Aucun module disponible"
          ui_pause
          continue
        fi

        ui_section "Selection du module a tester"
        for module_id in "${modules[@]}"; do
          printf ' %d. %s\n' "$index" "$module_id"
          index=$((index + 1))
        done
        printf ' 0. Retour\n'

        selection="$(prompt_select_number "Selectionner un module" 0 "${#modules[@]}")"
        if [[ "$selection" != "0" ]]; then
          test_runner_module "${modules[$((selection - 1))]}"
          ui_pause
        fi
        ;;
    esac
  done
}

menu_main() {
  local selection=""

  while true; do
    ui_menu \
      "Menu principal" \
      "1. Installation par categorie" \
      "2. Installation par profil" \
      "3. Installation par materiel" \
      "4. Configuration systeme" \
      "5. Tests" \
      "0. Quitter"

    ui_info "Modules detectes : $(registry_summary)"
    ui_info "Log : $RUNTIME_LOG_FILE"

    selection="$(prompt_select_number "Choisir une action" 0 5)"

    case "$selection" in
      0)
        ui_info "Sortie du programme"
        return 0
        ;;
      1)
        menu_category_selection
        ;;
      3)
        menu_modules_by_prefix "hardware" "Materiel"
        ;;
      4)
        menu_system_configuration
        ;;
      5)
        menu_tests
        ;;
      *)
        dispatcher_not_implemented "menu $selection"
        ui_pause
        ;;
    esac
  done
}
