#!/usr/bin/env bash

dispatcher_prompt_and_run_module() {
  local module_id="$1"
  local target_user=""
  local selected_groups=""
  local ssh_port=""
  local ssh_password_auth=""
  local ssh_root_login=""
  local avahi_enable=""
  local avahi_publish_workstation=""
  local host_name=""
  local domain_name=""
  local iface=""
  local ip_mode=""
  local ip_address=""
  local ip_prefix=""
  local ip_gateway=""
  local ip_dns=""
  local selected_nfs_shares=""
  local share_name=""
  local share_path=""
  local share_user=""
  local share_read_only=""
  local share_public=""
  local archive_name=""
  local docker_data_dir=""
  local default_nfs_ids=""
  local nfs_entry=""
  local nfs_item_id=""
  local nfs_item_name=""
  local nfs_mount_path=""
  local nfs_access=""
  local nfs_enabled=""
  local nfs_item_labels=()
  local nfs_item_ids=()
  local nfs_labels_blob=""
  local default_nfs_indices=""
  local nfs_action=""
  local nfs_mount_now=""
  local nfs_server_mode=""
  local samba_mode=""

  case "$module_id" in
    system/user-sudo)
      target_user="$(prompt_read_default "Nom de l'utilisateur a configurer" "${POSTINSTALL_DEFAULT_USER:-gilles}")"
      dispatcher_run_module "$module_id" "$target_user"
      ;;
    system/user-groups)
      target_user="$(prompt_read_default "Nom de l'utilisateur a configurer" "${POSTINSTALL_DEFAULT_USER:-gilles}")"
      selected_groups="$(prompt_read_csv_default "Groupes a ajouter, separes par des virgules" "${POSTINSTALL_USER_GROUPS_DEFAULT_GROUPS:-audio,video,plugdev,dialout,netdev,lpadmin,scanner}")"
      dispatcher_run_module "$module_id" "$target_user" "$selected_groups"
      ;;
    network/ssh-server)
      ssh_port="$(prompt_read_default "Port SSH" "${POSTINSTALL_SSH_PORT:-22}")"
      ssh_password_auth="$(prompt_confirm_default "Autoriser l'authentification par mot de passe" "${POSTINSTALL_SSH_PASSWORD_AUTH:-yes}")"
      ssh_root_login="$(prompt_confirm_default "Autoriser la connexion root SSH" "${POSTINSTALL_SSH_ROOT_LOGIN:-no}")"
      dispatcher_run_module "$module_id" "$ssh_port" "$ssh_password_auth" "$ssh_root_login"
      ;;
    network/mdns-avahi)
      avahi_enable="$(prompt_confirm_default "Activer la publication mDNS de la machine" "${POSTINSTALL_MDNS_AVAHI_ENABLE:-yes}")"
      avahi_publish_workstation="$(prompt_confirm_default "Publier la machine comme poste de travail" "${POSTINSTALL_MDNS_AVAHI_PUBLISH_WORKSTATION:-yes}")"
      dispatcher_run_module "$module_id" "$avahi_enable" "$avahi_publish_workstation"
      ;;
    network/identity)
      host_name="$(prompt_read_default "Hostname" "${POSTINSTALL_NETWORK_IDENTITY_DEFAULT_HOSTNAME:-debian}")"
      domain_name="$(prompt_read_default "Domaine local" "${POSTINSTALL_NETWORK_IDENTITY_DEFAULT_DOMAIN:-local}")"
      dispatcher_run_module "$module_id" "$host_name" "$domain_name"
      ;;
    network/ip-config)
      iface="$(prompt_read_default "Interface reseau" "${POSTINSTALL_NETWORK_IP_DEFAULT_INTERFACE:-$(system_primary_interface)}")"
      ip_mode="$(prompt_read_default "Mode reseau (dhcp ou static)" "${POSTINSTALL_NETWORK_IP_DEFAULT_MODE:-dhcp}")"
      if [[ "$ip_mode" == "static" ]]; then
        ip_address="$(prompt_read_default "Adresse IP" "${POSTINSTALL_NETWORK_IP_DEFAULT_ADDRESS:-10.0.0.10}")"
        ip_prefix="$(prompt_read_default "Prefixe CIDR" "${POSTINSTALL_NETWORK_IP_DEFAULT_PREFIX:-22}")"
        ip_gateway="$(prompt_read_default "Passerelle" "${POSTINSTALL_NETWORK_IP_DEFAULT_GATEWAY:-10.0.0.1}")"
        ip_dns="$(prompt_read_default "DNS" "${POSTINSTALL_NETWORK_IP_DEFAULT_DNS:-10.0.0.1}")"
      else
        ip_address=""
        ip_prefix=""
        ip_gateway=""
        ip_dns=""
      fi
      dispatcher_run_module "$module_id" "$iface" "$ip_mode" "$ip_address" "$ip_prefix" "$ip_gateway" "$ip_dns"
      ;;
    network/nfs-client)
      # shellcheck source=/dev/null
      source "$RUNTIME_PROJECT_ROOT/modules/network/nfs-client/module.sh"
      nfs_action="$(prompt_select_from_list "Action NFS client" "activer des partages" "desactiver des partages")"
      nfs_item_labels=()
      nfs_item_ids=()
      if [[ "$nfs_action" == "desactiver des partages" ]]; then
        ui_section "Partages NFS actifs dans fstab"
        while IFS= read -r nfs_entry; do
          [[ -n "$nfs_entry" ]] || continue
          IFS='|' read -r nfs_item_id nfs_item_name nfs_mount_path nfs_remote nfs_access <<< "$nfs_entry"
          nfs_item_ids+=("$nfs_item_id")
          nfs_item_labels+=("$nfs_item_id : $nfs_item_name -> $nfs_mount_path ($nfs_access)")
        done < <(module_nfs_client_active_entries)
        if [[ "${#nfs_item_ids[@]}" -eq 0 ]]; then
          ui_info "Aucun partage NFS actif a desactiver"
          return 0
        fi
        nfs_labels_blob="$(printf '%s\n' "${nfs_item_labels[@]}")"
        selected_nfs_shares="$(prompt_select_multiple_from_list "Selectionner les partages a retirer de fstab, indexes separes par des virgules" "" "$nfs_labels_blob" "${nfs_item_ids[@]}")"
        dispatcher_run_module "$module_id" "disable" "$selected_nfs_shares"
      else
        ui_section "Partages NFS client disponibles"
        while IFS= read -r nfs_entry; do
          [[ -n "$nfs_entry" ]] || continue
          IFS='|' read -r nfs_item_id nfs_item_name _ _ _ nfs_mount_path nfs_access _ nfs_enabled <<< "$nfs_entry"
          nfs_item_ids+=("$nfs_item_id")
          nfs_item_labels+=("$nfs_item_id : $nfs_item_name -> $nfs_mount_path ($nfs_access)")
        done < <(module_nfs_client_entries)
        default_nfs_indices="$(module_nfs_client_default_indices)"
        nfs_labels_blob="$(printf '%s\n' "${nfs_item_labels[@]}")"
        selected_nfs_shares="$(prompt_select_multiple_from_list "Selectionner les partages a activer dans fstab, indexes separes par des virgules" "$default_nfs_indices" "$nfs_labels_blob" "${nfs_item_ids[@]}")"
        nfs_mount_now="$(prompt_confirm_default "Monter immediatement les partages selectionnes" "yes")"
        dispatcher_run_module "$module_id" "enable" "$selected_nfs_shares" "$nfs_mount_now"
      fi
      ;;
    network/nfs-server)
      nfs_server_mode="$(prompt_select_from_list "Mode de synchronisation NFS serveur" "add-only" "strict")"
      ui_info "Synchronisation des exports NFS depuis le fichier YAML du repo"
      dispatcher_run_module "$module_id" "$nfs_server_mode"
      ;;
    network/samba-share)
      samba_mode="$(prompt_select_from_list "Mode de synchronisation Samba" "add-only" "strict")"
      ui_info "Synchronisation des partages Samba depuis le fichier YAML du repo"
      dispatcher_run_module "$module_id" "$samba_mode"
      ;;
    hardware/detect)
      dispatcher_run_module "$module_id"
      ;;
    boot/grub-theme)
      # shellcheck source=/dev/null
      source "$RUNTIME_PROJECT_ROOT/modules/boot/grub-theme/module.sh"
      mapfile -t grub_archives < <(module_grub_theme_list_archives)
      if [[ "${#grub_archives[@]}" -eq 0 ]]; then
        ui_warn "Aucune archive de theme GRUB disponible"
        return 1
      fi
      archive_name="$(prompt_select_from_list "Selectionner une archive de theme" "${grub_archives[@]}")"
      dispatcher_run_module "$module_id" "$archive_name"
      ;;
    containers/docker-engine)
      target_user="$(prompt_read_default "Utilisateur a ajouter au groupe docker" "${POSTINSTALL_DOCKER_TARGET_USER:-gilles}")"
      docker_data_dir="$(prompt_read_default "Dossier Docker" "/home/${target_user}/docker")"
      dispatcher_run_module "$module_id" "$target_user" "$docker_data_dir"
      ;;
    *)
      dispatcher_run_module "$module_id"
      ;;
  esac
}

dispatcher_run_module() {
  local module_id="$1"
  local module_path=""
  local module_slug=""
  local install_function=""

  shift

  if ! registry_has_module "$module_id"; then
    ui_error "Module introuvable : $module_id"
    return 1
  fi

  module_path="$(registry_module_path "$module_id")"
  # shellcheck source=/dev/null
  source "$module_path"

  module_slug="${module_id##*/}"
  module_slug="${module_slug//-/_}"
  install_function="module_${module_slug}_install"

  if ! declare -F "$install_function" >/dev/null 2>&1; then
    ui_error "Fonction d'installation absente pour le module : $module_id"
    return 1
  fi

  ui_section "Execution du module $module_id"
  "$install_function" "$@"
}

dispatcher_not_implemented() {
  local feature="$1"
  ui_warn "Fonction non implementee : $feature"
}
