#!/bin/bash

set -e

SCRIPT_PATH=$(realpath "$(dirname "$(follow_link "$0")")")

if [ -n "$1" ]; then
  CONFIG_PATH=$(realpath "$1")
else
  CONFIG_PATH=$(realpath "${SCRIPT_PATH}/config")
fi

menu_message="Choisir l'action à réaliser"

# Menu registry
# Format: key="Label|RelativeInputPath"
declare -A MENU_ITEMS=(
  [1]="Déploiement postes|deploy"
  [2]="Démarrage postes|start"
  [3]="Arret des postes|stop"
  [4]="Suppression postes|remove"
  [I]="Information|info"
  [Q]="Quitter|quit"
)


dispatch_selection() {
  entry="${MENU_ITEMS[$selection]}"
  if [[ -z "$entry" ]]; then
    echo "Sélection invalide !"
    return
  fi

  # echo "Entry: $entry"
  action=$(echo $entry|cut -f 2 -d '|')
  # echo "Action: $action"


  case "$action" in
    "deploy")
       echo ""
       echo ""
       echo "Deployement"
       echo "==========="
       echo -n "Nombre de machines à déployer : "
       read count
       if [[ "$count" =~ ^[0-9]+$ ]]; then
         # 2. Vérifie si le nombre est dans la plage [1, 30]
         if (( count >= 1 && count <= 30 )); then
            docker container run -it --rm -v /data/Tofu:/data stid:1.3 tofu apply -var="vm_count=$count"  -auto-approve
            break
         else
            echo "Erreur : le nombre doit être compris entre 1 et 30."
         fi
       else
         echo "Erreur : '$count' n'est pas un nombre valide."
       fi       
       ;;
    "start")
       echo "Démarrage"
       docker container run  -it --rm -v /data/Tofu:/data stid:1.3 ansible-playbook start-2.yaml
       ;;
    "stop")
       echo "Arret des VMs"
       docker container run  -it --rm -v /data/Tofu:/data stid:1.3 ansible-playbook stop.yaml
       ;;
    "remove")
       echo ""
       echo ""
       echo "Suppression"
       docker container run -it --rm -v /data/Tofu:/data stid:1.3 tofu destroy -auto-approve
       ;;
    "info")
       echo ""
       echo ""
       echo "Information"
       echo "==========="
       docker container run -it --rm -v /data/Tofu:/data stid:1.3 ansible-playbook info.yaml
       echo "Le csv est diponible ici : /data/Tofu/machines_ip.csv"
       echo "Contenu : "
       cat /data/Tofu/machines_ip.csv
       ;;
    "quit")
       exit 0
       ;;
    *)
      echo "Option invalide"
      ;;
  esac
  # IFS='|' read -r label path_or_function extra_args <<< "$entry"

  # echo "Vous avez choisi : $IFS"


  # Decide if it’s a build or a function
  #if [[ -d "$SCRIPT_PATH/builds/$path_or_function" ]]; then
  #  build_template "$label" "$path_or_function" "$extra_args"
  #else
  #  $path_or_function
  #fi
}

press_enter() {
  cd "$SCRIPT_PATH"
  echo -n "Appuyer sur <entrée> pour continuer."
  read -r
  clear
}

info() {
  echo "License: BSD-2"
  echo ""
  echo "For more information, review the project README."
  read -r
}

quit_program() {
  echo "Sortie..."
  exit 0
}

read_selection() {
  read -rp "Selection: " input
  echo "${input^^}"  # always uppercase
}

display_menu() {
  clear
  echo ""
  echo "    ____                                             "
  echo "   / __ \ _____ ____   _  __ ____ ___   ____   _  __ "
  echo '  / /_/ // ___// __ \ | |/_// __ `__ \ / __ \ | |/_/ '
  echo " / ____// /   / /_/ /_>  < / / / / / // /_/ /_>  <   "
  echo '/_/    /_/    \____//_/|_|/_/ /_/ /_/ \____//_/|_|   '
  echo ""
  echo -n "  Choisir une option"
  echo ""
  echo ""

  for key in $(printf "%s\n" "${!MENU_ITEMS[@]}" | sort -n 2>/dev/null); do
    IFS='|' read -r label _ <<< "${MENU_ITEMS[$key]}"
    printf "  %2s - %s\n" "$key" "$label"
  done
  echo ""
  selection=$(read_selection)
}

while true; do
  display_menu
  dispatch_selection
  echo ""
  read -rp "Appuyer sur <Entrée> pour continuer..."
done
