#!/bin/bash

# omarchy:summary=Cycle the current active workspace through the dwindle, scrolling, and floating layouts
# omarchy:args=[back]

DIRECTION=${1:-next}
if [[ $DIRECTION != "next" && $DIRECTION != "back" ]]; then
  echo "Usage: omarchy-hyprland-workspace-layout-toggle [back]" >&2
  exit 1
fi

ACTIVE_WORKSPACE=$(hyprctl activeworkspace -j | jq -r '.id')
# Named workspaces report a negative id, and Hyprland reads a leading "-" as a
# selector relative to the current workspace: targeting one lands on workspace 1
# and reconfigures that instead.
[[ $ACTIVE_WORKSPACE =~ ^[0-9]+$ ]] || exit 1
MODES_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/workspace-layouts"
MODE_FILE="$MODES_DIR/$ACTIVE_WORKSPACE.lua"

CURRENT_MODE=$(omarchy-hyprland-workspace-layout-current)

if [[ $DIRECTION == "back" ]]; then
  case "$CURRENT_MODE" in
    dwindle) NEW_MODE=floating ;;
    floating) NEW_MODE=scrolling ;;
    *) NEW_MODE=dwindle ;;
  esac
else
  case "$CURRENT_MODE" in
    dwindle) NEW_MODE=scrolling ;;
    scrolling) NEW_MODE=floating ;;
    *) NEW_MODE=dwindle ;;
  esac
fi

APPLY="o.workspace_mode({ workspace = \"$ACTIVE_WORKSPACE\", mode = \"$NEW_MODE\" })"

# Saving a mode that did not apply would leave the file naming one layout while
# the screen shows another, and restore it at the next login.
APPLIED=0
if hyprctl eval "$APPLY" >/dev/null 2>&1; then
  APPLIED=1
fi

if (( ! APPLIED )); then
  omarchy-notification-send -g 󱂬 "Workspace layout needs a Hyprland reload"
elif [[ $NEW_MODE == "$(omarchy-hyprland-workspace-layout-default)" ]] && rm -f "$MODE_FILE"; then
  # Cycled onto the default, a workspace goes back to following it, so a later
  # change of the default reaches it too.
  omarchy-notification-send -g 󱂬 "Workspace layout set to $NEW_MODE"
elif mkdir -p "$MODES_DIR" && printf '%s\n' "$APPLY" >"$MODE_FILE"; then
  omarchy-notification-send -g 󱂬 "Workspace layout set to $NEW_MODE"
else
  # The mode is on screen but nothing will bring it back, and the next press
  # reads the mode from this file, so say so rather than counting it as set.
  omarchy-notification-send -g 󱂬 "Workspace layout set to $NEW_MODE but could not be saved"
fi

# The bar shows the focused workspace's layout.
if (( APPLIED )); then
  omarchy-shell -q omarchy.workspaces refresh
fi

# Titlebars and shadows are set up while the config loads, and only
# when something floats, so the first floating workspace needs a reload to get
# them. Once the titlebar plugin is in, later ones have them already.
if (( APPLIED )) && [[ $NEW_MODE == "floating" ]] && ! hyprctl plugin list -j 2>/dev/null | jq -e 'any(.[]?; .name == "hyprbars")' >/dev/null 2>&1; then
  hyprctl reload >/dev/null
fi
