#!/usr/bin/env bash
# ui/activity.module.sh — rendering for wgctl activity

# ui::activity::peer_row <name_pad> <rx_pad> <tx_pad> <drops> <drop_word> <w_drops>
function ui::activity::peer_row() {
  local name_pad="${1:-}" rx_pad="${2:-}" tx_pad="${3:-}" \
        drops="${4:-0}" drop_word="${5:-drops}" w_drops="${6:-1}"

  printf "  \033[1m%s\033[0m  \033[2m↓\033[0m%s  \033[2m↑\033[0m%s  %${w_drops}s %s\n" \
    "$name_pad" "$rx_pad" "$tx_pad" "$drops" "$drop_word"
}

# ui::activity::service_row <dest_display> <drop_count> <drop_word> <drops_col> <w_drops>
function ui::activity::service_row() {
  local dest_display="${1:-}" drop_count="${2:-0}" drop_word="${3:-drops}" \
        drops_col="${4:-30}" w_count="${5:-1}"

  local arrow_prefix="    → "
  local prefix_bytes=${#arrow_prefix}
  local prefix_len=$(( prefix_bytes + ${#dest_display} ))
  local pad_n=$(( drops_col - prefix_len ))
  [[ $pad_n -lt 1 ]] && pad_n=1

  printf "    \033[0;31m→ %s%*s  %${w_count}s %s\033[0m\n" \
    "$dest_display" "$pad_n" "" "$drop_count" "$drop_word"
}

# Table versions (kept for future display config)
function ui::activity::header_table() {
  printf "\n  %-24s %-14s %-14s %s\n" "PEER" "↓ RX" "↑ TX" "DROPS"
  printf "  %s\n" "$(printf '─%.0s' {1..65})"
}

function ui::activity::peer_row_table() {
  local name="${1:-}" rx_fmt="${2:-}" tx_fmt="${3:-}" \
        drops="${4:-0}" drop_word="${5:-drops}"
  printf "  %-24s %-14s %-14s %s %s\n" \
    "$name" "↓$rx_fmt" "↑$tx_fmt" "$drops" "$drop_word"
}

function ui::activity::service_row_table() {
  local dest_display="${1:-}" drop_count="${2:-0}" drop_word="${3:-drops}"
  printf "    → %-30s %s %s\n" "$dest_display" "$drop_count" "$drop_word"
}

function ui::activity::accept_row() {
  local name_pad="${1:-}" bytes_in="${2:-}" bytes_out="${3:-}" \
        conns="${4:-0}" w_count="${5:-4}"
 
  local conn_word="conns"
  [[ "$conns" -eq 1 ]] && conn_word="conn"
 
  local spaces
  spaces=$(printf '%*s' "${#name_pad}" '')
 
  printf "  \033[0;32m%s  ↓%-10s  ↑%-10s  %${w_count}s %s\033[0m\n" \
    "$spaces" "$bytes_in" "$bytes_out" "$conns" "$conn_word"
}
 
 
function ui::activity::accept_dest_row() {
  local dest="${1:-}" bytes_orig="${2:-0}" bytes_reply="${3:-0}" \
        count="${4:-0}" drops_col="${5:-40}" w_count="${6:-4}"

  local conn_word="conns"
  [[ "$count" -eq 1 ]] && conn_word="conn"

  local arrow_prefix="    → "
  local prefix_bytes=${#arrow_prefix}
  local prefix_len=$(( prefix_bytes + ${#dest} ))
  local pad_n=$(( drops_col - prefix_len ))
  [[ $pad_n -lt 1 ]] && pad_n=1

  # Only show bytes if non-zero
  local bytes_display=""
  if [[ "$bytes_orig" -gt 0 || "$bytes_reply" -gt 0 ]]; then
    local bytes_display="  "
    [[ "$bytes_orig"  -gt 0 ]] && bytes_display+="↓$(fmt::bytes "$bytes_orig") "
    [[ "$bytes_reply" -gt 0 ]] && bytes_display+="↑$(fmt::bytes "$bytes_reply")"
    bytes_display="${bytes_display% }"  # trim trailing space
  fi

  printf "    \033[0;32m→\033[0m \033[0;32m%b" "$dest"
  printf "%*s  %${w_count}s %-5s%s\033[0m\n" "$pad_n" "" "$count" "$conn_word" "$bytes_display"
  # printf "    \033[0;32m→\033[0m \033[0;32m%s%*s  %${w_count}s %-5s%s\033[0m\n" \
  #   "$dest" "$pad_n" "" "$count" "$conn_word" "$bytes_display"
}