#!/bin/bash

# omarchy:summary=Mutate the user shell.json bar layout
# omarchy:args=add <plugin> [left|center|right] | drop <plugin> | position <top|bottom|left|right> | transparent <true|false> | show
# omarchy:examples=omarchy config shell bar add omarchy.tailscale | omarchy config shell bar position top | omarchy config shell bar transparent true | omarchy config shell bar show

set -euo pipefail

CONFIG_FILE="$HOME/.config/omarchy/shell.json"
OMARCHY_ROOT="${OMARCHY_PATH:-}"
DEFAULTS_FILE="$OMARCHY_ROOT/config/omarchy/shell.json"

usage() {
  echo "Usage: omarchy-config-shell-bar add <plugin> [left|center|right] | drop <plugin> | position <top|bottom|left|right> | transparent <true|false> | show" >&2
}

fail() {
  echo "omarchy-config-shell-bar: $*" >&2
  exit 1
}

source_file() {
  local source="$CONFIG_FILE"

  if [[ ! -s $source ]]; then
    [[ -n $OMARCHY_ROOT ]] || fail "OMARCHY_PATH is not set"
    source="$DEFAULTS_FILE"
  fi

  [[ -s $source ]] || fail "could not find shell config or defaults"
  printf '%s\n' "$source"
}

command="${1:-}"

case "$command" in
  show)
    if (( $# != 1 )); then
      usage
      exit 1
    fi

    jq '.bar // {}' "$(source_file)"
    ;;
  add)
    if (( $# < 2 || $# > 3 )); then
      usage
      exit 1
    fi

    plugin="${2:-}"
    section="${3:-right}"

    [[ -n $plugin ]] || fail "plugin is required"
    [[ $section =~ ^(left|center|right)$ ]] || fail "section must be left, center, or right"

    mkdir -p "$(dirname "$CONFIG_FILE")"

    source="$(source_file)"
    tmp=$(mktemp)
    trap 'rm -f "$tmp"' EXIT

    jq --arg section "$section" --arg plugin "$plugin" '
      def object_or_empty: if type == "object" then . else {} end;
      def array_or_empty: if type == "array" then . else [] end;
      def entry_id: if type == "object" then (.id // "" | tostring) else tostring end;
      def append_anchor($section):
        {
          left: "omarchy.workspaces",
          center: "omarchy.weather",
          right: "omarchy.tray"
        }[$section];
      def insert_after_anchor($entries; $entry; $anchor):
        ($entries | map(entry_id) | index($anchor)) as $index
        | if $index == null then
            $entries + [$entry]
          else
            $entries[0:$index + 1] + [$entry] + $entries[$index + 1:]
          end;

      object_or_empty
      | .version = 1
      | .bar = (.bar | object_or_empty)
      | .bar.layout = (.bar.layout | object_or_empty)
      | .bar.layout.left = (.bar.layout.left | array_or_empty | map(select(entry_id != $plugin)))
      | .bar.layout.center = (.bar.layout.center | array_or_empty | map(select(entry_id != $plugin)))
      | .bar.layout.right = (.bar.layout.right | array_or_empty | map(select(entry_id != $plugin)))
      | .plugins = (.plugins | array_or_empty)
      | .bar.layout[$section] = insert_after_anchor(.bar.layout[$section]; { id: $plugin }; append_anchor($section))
    ' "$source" >"$tmp"

    mv "$tmp" "$CONFIG_FILE"
    trap - EXIT
    omarchy-shell -q shell rescanPlugins >/dev/null 2>&1 || true
    ;;
  position)
    if (( $# != 2 )); then
      usage
      exit 1
    fi

    position="${2:-}"

    [[ $position =~ ^(top|bottom|left|right)$ ]] || fail "position must be top, bottom, left, or right"

    mkdir -p "$(dirname "$CONFIG_FILE")"

    source="$(source_file)"
    tmp=$(mktemp)
    trap 'rm -f "$tmp"' EXIT

    jq --arg position "$position" '
      def object_or_empty: if type == "object" then . else {} end;

      object_or_empty
      | .version = 1
      | .bar = (.bar | object_or_empty)
      | .bar.position = $position
    ' "$source" >"$tmp"

    mv "$tmp" "$CONFIG_FILE"
    trap - EXIT
    ;;
  transparent)
    if (( $# != 2 )); then
      usage
      exit 1
    fi

    transparent="${2:-}"

    [[ $transparent =~ ^(true|false)$ ]] || fail "transparent must be true or false"

    mkdir -p "$(dirname "$CONFIG_FILE")"

    source="$(source_file)"
    tmp=$(mktemp)
    trap 'rm -f "$tmp"' EXIT

    jq --argjson transparent "$transparent" '
      def object_or_empty: if type == "object" then . else {} end;

      object_or_empty
      | .version = 1
      | .bar = (.bar | object_or_empty)
      | .bar.transparent = $transparent
    ' "$source" >"$tmp"

    mv "$tmp" "$CONFIG_FILE"
    trap - EXIT
    ;;
  drop)
    if (( $# != 2 )); then
      usage
      exit 1
    fi

    plugin="${2:-}"

    [[ -n $plugin ]] || fail "plugin is required"

    mkdir -p "$(dirname "$CONFIG_FILE")"

    source="$(source_file)"
    tmp=$(mktemp)
    trap 'rm -f "$tmp"' EXIT

    jq --arg plugin "$plugin" '
      def object_or_empty: if type == "object" then . else {} end;
      def array_or_empty: if type == "array" then . else [] end;
      def entry_id: if type == "object" then (.id // "" | tostring) else tostring end;

      object_or_empty
      | .version = 1
      | .bar = (.bar | object_or_empty)
      | .bar.layout = (.bar.layout | object_or_empty)
      | .bar.layout.left = (.bar.layout.left | array_or_empty | map(select(entry_id != $plugin)))
      | .bar.layout.center = (.bar.layout.center | array_or_empty | map(select(entry_id != $plugin)))
      | .bar.layout.right = (.bar.layout.right | array_or_empty | map(select(entry_id != $plugin)))
      | .plugins = (.plugins | array_or_empty)
    ' "$source" >"$tmp"

    mv "$tmp" "$CONFIG_FILE"
    trap - EXIT
    omarchy-shell -q shell rescanPlugins >/dev/null 2>&1 || true
    ;;
  *)
    usage
    exit 1
    ;;
esac
