#!/bin/bash

# omarchy:summary=Set the active bar option, position, and transparency
# omarchy:group=bar
# omarchy:args=use <id> | reset | defaults | position <top|bottom|left|right> | transparent <true|false|toggle>
# omarchy:examples=omarchy bar use local.neon-bar | omarchy bar reset | omarchy bar defaults | omarchy bar position top | omarchy bar transparent true

set -euo pipefail

source omarchy-shell-config

usage() {
  cat <<USAGE
Usage: omarchy bar <command> [args...]

  use <id>                             Use a bar option as the active bar
  reset                                Return to the built-in Omarchy bar
  defaults                             Restore the default bar and service widgets
  position <top|bottom|left|right>     Bar position
  transparent <true|false|toggle>      Bar transparency

Bar widgets are added, moved, removed, and configured with 'omarchy bar plugin'.

Examples:
  omarchy bar use local.neon-bar
  omarchy bar reset
  omarchy bar defaults
  omarchy bar position top
  omarchy bar transparent true
USAGE
}

# ------------------------------------------------------------------ validation

bar_option_exists() {
  omarchy-plugin-catalog | jq -e --arg id "$1" '
    any(.[]; (.kinds | index("bar")) and .barPath != null and .id == $id)
  ' >/dev/null
}

# -------------------------------------------------------------------- commands

cmd_use() {
  local plugin="${1:-}"
  [[ -n $plugin ]] || fail "bar option id is required"
  (( $# == 1 )) || fail "use takes a single bar option id"
  if [[ $plugin == "default" || $plugin == "built-in" ]]; then
    plugin="omarchy.bar"
  fi
  bar_option_exists "$plugin" || fail "$plugin is not a known bar option; run 'omarchy plugin list'"

  if [[ $plugin == "omarchy.bar" ]]; then
    commit "$NORMALIZE | del(.bar.id)"
  else
    commit "$NORMALIZE | .bar.id = \$plugin" --arg plugin "$plugin"
  fi
  echo "Using $plugin as the active bar"
}

cmd_defaults() {
  (( $# == 0 )) || fail "defaults does not take arguments"
  # Restore the whole default bar, then re-add widgets for services the user
  # actually has installed (they aren't in the shipped default layout).
  commit "$NORMALIZE | .bar = \$defaults[0].bar" --slurpfile defaults "$DEFAULTS_FILE"
  local service
  for service in dropbox tailscale; do
    if "omarchy-installed-service-$service"; then
      omarchy-bar-plugin add "omarchy.$service"
    fi
  done
  echo "Restored the default Omarchy bar"
}

cmd_position() {
  local position="${1:-}"
  [[ -n $position ]] || fail "position is required"
  (( $# == 1 )) || fail "position takes a single value"
  [[ $position =~ ^(top|bottom|left|right)$ ]] || fail "position must be top, bottom, left, or right"
  commit "$NORMALIZE | .bar.position = \$position" --arg position "$position"
  echo "Bar position set to $position"
}

cmd_transparent() {
  local transparent="${1:-}"
  [[ -n $transparent ]] || fail "transparent is required"
  (( $# == 1 )) || fail "transparent takes a single value"
  [[ $transparent =~ ^(true|false|toggle)$ ]] || fail "transparent must be true, false, or toggle"
  if [[ $transparent == "toggle" ]]; then
    commit "$NORMALIZE | .bar.transparent = (.bar.transparent != true)"
    echo "Bar transparency toggled"
  else
    commit "$NORMALIZE | .bar.transparent = \$transparent" --argjson transparent "$transparent"
    echo "Bar transparency set to $transparent"
  fi
}

# --------------------------------------------------------------------- dispatch

command="${1:-}"
(( $# > 0 )) && shift || true

case "$command" in
  use)
    cmd_use "$@"
    ;;
  reset)
    (( $# == 0 )) || fail "reset does not take arguments"
    cmd_use omarchy.bar
    ;;
  defaults)
    cmd_defaults "$@"
    ;;
  position)
    cmd_position "$@"
    ;;
  transparent)
    cmd_transparent "$@"
    ;;
  plugin)
    exec omarchy-bar-plugin "$@"
    ;;
  add | move | remove | rm | drop | set | replace)
    fail "bar widgets are managed with: omarchy bar plugin $command ..."
    ;;
  -h | --help | help | "")
    usage
    ;;
  *)
    fail "unknown command: $command"
    ;;
esac
