#!/bin/zsh
# tmux plugin: ailog powerline status bar
# requires: aifont, ailog, jq

INTERVAL=60
SEP=$'\ue0b0'
RSEP=$'\ue0b2'

get_icon() {
  local handle=$(ailog pds s 2>/dev/null | jq -r '.handle // empty' 2>/dev/null)
  case "$handle" in
    ai.*) echo $'\ue001' ;;
    syui.*) echo $'\ue002' ;;
    *)    echo "❯" ;;
  esac
}

get_cfg() {
  case "$(uname)" in
    Darwin) echo "$HOME/Library/Application Support/ai.syui.log/config.json" ;;
    *)      echo "${XDG_CONFIG_HOME:-$HOME/.config}/ai.syui.log/config.json" ;;
  esac
}

has_aifont() {
  [ -f "$HOME/Library/Fonts/aifont.ttf" ] || [ -f "$HOME/.local/share/fonts/aifont.ttf" ]
}

can_run() {
  local cfg=$(get_cfg)
  command -v ailog &>/dev/null && command -v jq &>/dev/null && [ -f "$cfg" ] && has_aifont
}

refresh_token() {
  local stamp="/tmp/.ailog_refresh"
  local now=$(date +%s)
  local last=0
  [ -f "$stamp" ] && last=$(cat "$stamp")
  if [ $((now - last)) -gt 1800 ]; then
    ailog pds r 2>/dev/null
    echo "$now" > "$stamp"
  fi
}

get_handle() {
  ailog pds s 2>/dev/null | jq -r '.handle // empty' 2>/dev/null
}

get_notify_count() {
  ailog notify count 2>/dev/null | jq -r '.count // 0' 2>/dev/null
}

get_latest_post() {
  local data=$(ailog f tl -l 1 2>/dev/null | tr -d '\t' | sed 's/\\n/ /g')
  local handle=$(echo "$data" | jq -r '.feed[0].post.author.handle // empty' 2>/dev/null | cut -d. -f1)
  local full=$(echo "$data" | jq -r '.feed[0].post.record.text // empty' 2>/dev/null | tr '\n' ' ')
  local text="${full:0:20}"
  [[ ${#full} -gt 20 ]] && text+="..."
  [ -n "$text" ] && echo "@${handle} ${text}"
}

status_left() {
  can_run || return
  refresh_token
  local handle=$(get_handle)
  local count=$(get_notify_count)
  local post=$(get_latest_post)

  local icon=$(get_icon)
  local out=""
  out+="#[fg=yellow,bg=black] ${icon} "
  out+="#[fg=black,bg=colour234]${SEP}"
  out+="#[fg=yellow,bg=colour234] @${handle} "
  if [ -n "$count" ] && [ "$count" != "0" ] && [ "$count" != "null" ]; then
    out+="#[fg=colour234,bg=black]${SEP}"
    out+="#[fg=yellow,bg=black] ${count} "
  else
    out+="#[fg=colour234,bg=black]${SEP}"
  fi
  if [ -n "$post" ]; then
    out+="#[fg=white,bg=black] ${post} "
  fi
  out+="#[fg=black,bg=default]${SEP}#[default] "
  echo -n "$out"
}

get_pds() {
  ailog pds s 2>/dev/null | jq -r '.pds // empty' 2>/dev/null | sed 's|https://||'
}

get_pds_icon() {
  local pds="$1"
  local ai_icon=$(printf '\ue001')
  local bsky_icon=$(printf '\ue003')
  case "$pds" in
    *syu.is*) echo "#[fg=yellow]${ai_icon} #[fg=cyan]${pds}" ;;
    *bsky*) echo "#[fg=blue]${bsky_icon} #[fg=cyan]${pds}" ;;
    *) echo "#[fg=cyan]${pds}" ;;
  esac
}

status_right() {
  local pds=$(get_pds)
  local out=""
  if [ -n "$pds" ]; then
    local pds_display=$(get_pds_icon "$pds")
    out+="#[fg=colour236]${RSEP}#[bg=colour236] ${pds_display} "
  fi
  out+="#[fg=colour234]${RSEP}#[fg=white,bg=colour234] #S "
  echo -n "$out"
}

case "$1" in
  left)   status_left ;;
  right)  status_right ;;
  notify) get_notify_count ;;
  handle) get_handle ;;
  latest) get_latest_post ;;
  *)
    if can_run; then
      tmux set-option -g status-style "bg=default,fg=white"
      tmux set-window-option -g window-status-format ""
      tmux set-window-option -g window-status-current-format ""
      tmux set-option -g status-left-length 100
      tmux set-option -g status-right-length 50
      tmux set-option -g status-left "#(~/.tmux/plugin/ailog.zsh left)"
      tmux set-option -g status-right "#(~/.tmux/plugin/ailog.zsh right)#[fg=black,bg=colour234]${RSEP}#[fg=white,bg=black] %Y-%m-%dT%H:%M "
      tmux set-option -g status-interval $INTERVAL
    fi
    ;;
esac
