#!/bin/bash

# blob:summary=Switch display modes, choose the primary display, and set resolution and refresh rate
# blob:args=<status|extend|duplicate [name]|only <name>|primary <name>|resolution <name> <WxH@rate>>
# blob:group=monitor

set -e

action="${1-}"
shift || true

monitors_json=$(hyprctl monitors all -j)

monitor_names() {
  printf '%s\n' "$monitors_json" | jq -r '.[].name'
}

monitor_field() {
  printf '%s\n' "$monitors_json" | jq -r --arg name "$1" --arg field "$2" '.[] | select(.name == $name) | .[$field]'
}

is_disabled() {
  [[ $(monitor_field "$1" disabled) == "true" ]]
}

current_mode() {
  printf '%s\n' "$monitors_json" | jq -r --arg name "$1" '
    .[] | select(.name == $name)
    | if .disabled or .width == 0 then "preferred"
      else "\(.width)x\(.height)@\((.refreshRate * 100 | round) / 100)" end'
}

current_scale() {
  local scale
  scale=$(monitor_field "$1" scale)
  if [[ -z $scale || $scale == "null" || $scale == "0" ]] || is_disabled "$1"; then
    scale=1
  fi
  printf '%s\n' "$scale"
}

primary_name() {
  printf '%s\n' "$monitors_json" | jq -r '
    ([.[] | select(.disabled != true and .mirrorOf == "none" and .x == 0 and .y == 0)][0].name)
    // ([.[] | select(.disabled != true)][0].name)
    // ""'
}

is_mirroring() {
  printf '%s\n' "$monitors_json" | jq -e 'any(.[]; .disabled != true and .mirrorOf != "none")' >/dev/null
}

require_monitor() {
  if [[ -z ${1-} ]] || ! monitor_names | grep -qxF -- "$1"; then
    echo "Unknown monitor: ${1-}" >&2
    exit 1
  fi
}

apply() {
  hyprctl keyword monitor "$1" >/dev/null
}

place_from_primary() {
  local primary="$1"
  local include_disabled="$2"
  local name
  apply "$primary,$(current_mode "$primary"),0x0,$(current_scale "$primary")"
  for name in $(monitor_names); do
    if [[ $name == "$primary" ]]; then
      continue
    fi
    if [[ $include_disabled != "yes" ]] && is_disabled "$name"; then
      continue
    fi
    apply "$name,$(current_mode "$name"),auto-right,$(current_scale "$name")"
  done
}

duplicate_from() {
  local source="$1"
  local name
  apply "$source,$(current_mode "$source"),0x0,$(current_scale "$source")"
  for name in $(monitor_names); do
    if [[ $name != "$source" ]]; then
      apply "$name,preferred,auto,1,mirror,$source"
    fi
  done
}

show_only() {
  local target="$1"
  local name
  apply "$target,$(current_mode "$target"),0x0,$(current_scale "$target")"
  for name in $(monitor_names); do
    if [[ $name != "$target" ]]; then
      apply "$name,disable"
    fi
  done
}

make_primary() {
  local target="$1"
  if is_mirroring; then
    duplicate_from "$target"
  else
    place_from_primary "$target" no
  fi
  hyprctl dispatch focusmonitor "$target" >/dev/null || true
  hyprctl dispatch moveworkspacetomonitor 1 "$target" >/dev/null || true
  if command -v xrandr >/dev/null; then
    xrandr --output "$target" --primary >/dev/null 2>&1 || true
  fi
}

set_resolution() {
  local target="$1"
  local mode="$2"
  if [[ ! $mode =~ ^[0-9]+x[0-9]+@[0-9.]+$ ]]; then
    echo "Not a mode: $mode (expected WxH@rate)" >&2
    exit 1
  fi
  local keyword="$target,$mode,$(monitor_field "$target" x)x$(monitor_field "$target" y),$(current_scale "$target")"
  local mirror_of
  mirror_of=$(monitor_field "$target" mirrorOf)
  if [[ -n $mirror_of && $mirror_of != "none" ]]; then
    keyword+=",mirror,$mirror_of"
  fi
  apply "$keyword"
}

print_status() {
  printf '%s\n' "$monitors_json" | jq -c --arg primary "$(primary_name)" '[.[] | {
    name,
    description,
    enabled: (.disabled != true),
    focused: (.focused == true),
    width,
    height,
    refreshRate,
    scale,
    mirrorOf,
    primary: (.name == $primary),
    modes: (.availableModes // [])
  }]'
}

case "$action" in
status)
  print_status
  ;;
extend)
  place_from_primary "$(primary_name)" yes
  ;;
duplicate)
  source="${1:-$(primary_name)}"
  require_monitor "$source"
  duplicate_from "$source"
  ;;
only)
  require_monitor "${1-}"
  show_only "$1"
  ;;
primary)
  require_monitor "${1-}"
  make_primary "$1"
  ;;
resolution)
  require_monitor "${1-}"
  set_resolution "$1" "${2-}"
  ;;
*)
  echo "Usage: blob-display-mode <status|extend|duplicate [name]|only <name>|primary <name>|resolution <name> <WxH@rate>>" >&2
  exit 1
  ;;
esac
