#!/usr/bin/env bash

TUI_FS_ACTIVE=0
TUI_FS_ROWS=0
TUI_FS_COLS=0
TUI_FS_CURRENT=0
TUI_FS_TOTAL=0
TUI_FS_LABEL=""
TUI_FS_SAVED_STTY=""

tui_fs_init() {
  TUI_FS_SAVED_STTY=$(stty -g 2>/dev/null || true)
  TUI_FS_ROWS=$(tput lines 2>/dev/null || echo 24)
  TUI_FS_COLS=$(tput cols 2>/dev/null || echo 80)

  tput smcup 2>/dev/null || true
  tput civis 2>/dev/null || true
  tput clear 2>/dev/null || true

  TUI_FS_ACTIVE=1
  _tui_fs_draw_selection_banner
}

tui_fs_exit() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return
  TUI_FS_ACTIVE=0
  tput cnorm 2>/dev/null || true
  tput rmcup 2>/dev/null || true
  [[ -n "$TUI_FS_SAVED_STTY" ]] && stty "$TUI_FS_SAVED_STTY" 2>/dev/null || true
}

_tui_fs_hline() {
  local len=$1
  local i
  for ((i=0; i<len; i++)); do printf '─'; done
}

_tui_fs_title() {
  local cols=$TUI_FS_COLS
  local c=$((cols - 4))
  ((c < 30)) && c=30
  printf '\033[1;36m╭%s╮\033[0m\n' "$(_tui_fs_hline "$c")"
  printf '\033[1;36m│\033[0m  \033[1;37m◆  Omeron\033[0m  —  \033[2mModular System Setup\033[0m  \033[1;36m│\033[0m\n'
  printf '\033[1;36m╰%s╯\033[0m\n' "$(_tui_fs_hline "$c")"
  printf '\n'
}

_tui_fs_draw_selection_banner() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return
  tput clear 2>/dev/null || true
  _tui_fs_title
}

tui_fs_show_msg() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && { printf '%s\n' "$*"; return; }
  printf '  \033[1;34m▶\033[0m %s\n' "$*"
}

tui_fs_show_ok() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && { printf '%s\n' "$*"; return; }
  printf '  \033[1;32m✓\033[0m %s\n' "$*"
}

tui_fs_start_execution() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return
  TUI_FS_CURRENT=0
  TUI_FS_TOTAL=0
  TUI_FS_LABEL=""
}

tui_fs_set_step() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return
  TUI_FS_CURRENT=$1
  TUI_FS_TOTAL=$2
  TUI_FS_LABEL="${3:-}"
  _tui_fs_draw_exec_header
}

_tui_fs_draw_exec_header() {
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return
  tput cup 0 0 2>/dev/null || true
  tput clear 2>/dev/null || true

  local cols=$TUI_FS_COLS
  local c=$((cols - 4))
  ((c < 30)) && c=30
  local hline
  hline="$(_tui_fs_hline "$c")"

  printf '\033[1;36m╭%s╮\033[0m\n' "$hline"
  local title="◆  Omeron"
  if [[ -n "$TUI_FS_LABEL" ]]; then
    title+="  —  ${TUI_FS_LABEL}"
  fi
  printf '\033[1;36m│\033[0m  \033[1;37m%-*s\033[0m  \033[1;36m│\033[0m\n' "$c" "$title"

  local progress=0
  if ((TUI_FS_TOTAL > 0)); then
    progress=$(( TUI_FS_CURRENT * 100 / TUI_FS_TOTAL ))
    ((progress > 100)) && progress=100
  fi

  local bar_width=$((c - 24))
  ((bar_width < 10)) && bar_width=10
  local filled=$(( progress * bar_width / 100 ))
  local bar=""
  local i
  for ((i=0; i<filled && i<bar_width; i++)); do bar+='▓'; done
  for ((i=filled; i<bar_width; i++)); do bar+='░'; done

  local counter="[$TUI_FS_CURRENT/$TUI_FS_TOTAL]"
  printf '\033[1;36m│\033[0m  %s  %s  \033[1;33m%3d%%\033[0m  \033[1;36m│\033[0m\n' "$counter" "$bar" "$progress"
  printf '\033[1;36m╰%s╯\033[0m\n' "$hline"
  printf '\n'
}

tui_fs_select_module() {
  local idx="$1"
  local total="$2"
  local label="$3"
  local description="${4:-$label}"
  local required="${5:-0}"

  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && {
    if ((required)); then return 0; fi
    printf '  '
    tui_confirm "$description" && return 0 || return 1
  }

  tput cup 4 0 2>/dev/null || true
  tput ed 2>/dev/null || true

  printf '  \033[1;36m┌─\033[0m  \033[1;37mModule %d/%d\033[0m\n' "$idx" "$total"
  printf '  \033[1;36m│\033[0m\n'
  printf '  \033[1;36m│\033[0m  \033[1;37m%s\033[0m\n' "$description"
  printf '  \033[1;36m│\033[0m  \033[2m%s\033[0m\n' "$label"
  printf '  \033[1;36m└─\033[0m\n'
  printf '\n'

  if ((required)); then
    printf '  \033[1;33m▶\033[0m  required module\n'
    printf '\n'
    printf '  Press any key to continue... '
    read -n 1 -s -r
    printf '\n'
    return 0
  fi

  if ((OMERON_HAS_GUM)); then
    gum confirm "Include this module?"
    return $?
  fi

  printf '  \033[1;36m?\033[0m  Include this module? \033[1m[Y/n]\033[0m '
  read -r response
  response="${response:-Y}"
  [[ "$response" =~ ^[yY](es)?$ ]]
  return $?
}

tui_fs_confirm_start() {
  local count="$1"
  [[ "$TUI_FS_ACTIVE" -eq 0 ]] && return 0

  tput cup 4 0 2>/dev/null || true
  tput ed 2>/dev/null || true

  printf '  \033[1;36m┌─\033[0m  \033[1;37mSelection Complete\033[0m\n'
  printf '  \033[1;36m│\033[0m\n'
  printf '  \033[1;36m│\033[0m  \033[1;37m%d\033[0m module(s) selected\n' "$count"
  printf '  \033[1;36m└─\033[0m\n'
  printf '\n'

  if ((OMERON_HAS_GUM)); then
    gum confirm "Start installation?"
    return $?
  fi

  printf '  \033[1;36m?\033[0m  Start installation? \033[1m[Y/n]\033[0m '
  read -r response
  response="${response:-Y}"
  [[ "$response" =~ ^[yY](es)?$ ]]
  return $?
}
