#!/usr/bin/env bash

# omarchy:summary=Print monitor panel state for the shell
# omarchy:group=monitor


case $("${BASH_SOURCE[0]%/*}/omarchy-compositor") in
  hyprland) ;;
  sway)
    # The same eight lines from swaymsg. sway cannot mirror, so line five stays
    # empty, and an output it has disabled reports no current mode.
    outputs_json=$(swaymsg -t get_outputs -r 2>/dev/null) || outputs_json='[]'
    focused_output=$(printf '%s\n' "$outputs_json" | jq -r '[.[] | select(.focused == true)][0].name // ""')

    { omarchy-brightness-display --monitor "$focused_output" 2>/dev/null; echo; } | head -n 1

    printf '%s\n' "$outputs_json" | jq -r '
      def internal: test("^(eDP|LVDS|DSI)-");
      ([.[] | select(.name | internal)][0].name // ""),
      ([.[] | select((.name | internal) | not)][0].name // ""),
      ([.[] | select((.name | internal) and .active == true)][0].name // ""),
      ""
    '

    printf '%s\n' "$focused_output"
    omarchy-monitor-scaling 2>/dev/null || echo

    printf '%s\n' "$outputs_json" | jq -c \
      '[.[] | {name, enabled: (.active == true), focused: (.focused == true), width: (.current_mode.width // .rect.width), height: (.current_mode.height // .rect.height)}]'
    exit 0
    ;;
  *) exit 0 ;;
esac

monitors_json=$(hyprctl monitors all -j)
focused_monitor=$(printf '%s\n' "$monitors_json" | jq -r '[.[] | select(.focused == true)][0].name // ""')

{ omarchy-brightness-display --monitor "$focused_monitor" 2>/dev/null; echo; } | head -n 1

printf '%s\n' "$monitors_json" | jq -r '
  def internal: test("^(eDP|LVDS|DSI)-");
  ([.[] | select(.name | internal)][0].name // ""),
  ([.[] | select((.name | internal) | not)][0].name // ""),
  ([.[] | select((.name | internal) and .disabled != true)][0].name // ""),
  ([.[] | select(.mirrorOf != "none") | if (.name | internal) then .mirrorOf else .name end][0] // "")
'

printf '%s\n' "$focused_monitor"
omarchy-hyprland-monitor-scaling 2>/dev/null || echo

printf '%s\n' "$monitors_json" | jq -c \
  '[.[] | {name, enabled:(.disabled != true), focused:(.focused == true), width, height}]'
