#!/bin/sh

wlr-randr --output DP-1 --mode 3840x2160 --scale 2
wlr-randr --output HDMI-A-1 --mode 3840x2160 --scale 2 --left-of DP-1

argenctl config set label-mode modifier

# Terminal
argenctl binding set normal Super Return sh "kitty --single-instance --instance-group default"

# Reload config
argenctl binding set normal Super+Shift R exec "$0"

argenctl binding set normal Super P exec fuzzel
argenctl binding set normal Super+Shift L exec waylock
argenctl binding set normal Super+Shift T sh "lupan-set-theme toggle"

# Window focus and swap
argenctl binding set normal Super J window focus next
argenctl binding set normal Super K window focus prev
argenctl binding set normal Super+Shift J window swap next
argenctl binding set normal Super+Shift K window swap prev

argenctl binding set normal Super F window float
argenctl binding set normal Super+Shift F window fullscreen

argenctl binding set normal Super+Shift C window close
argenctl binding set normal Super+Shift X window detach

# Output focus and send
argenctl binding set normal Super Period output focus next
argenctl binding set normal Super Comma output focus prev
argenctl binding set normal Super+Shift Period window send next
argenctl binding set normal Super+Shift Comma window send prev

# Layout
argenctl binding set normal Super H layout set primary-ratio -0.05
argenctl binding set normal Super L layout set primary-ratio +0.05
argenctl binding set normal Super+Control K layout set secondary-ratio -0.05
argenctl binding set normal Super+Control J layout set secondary-ratio +0.05

# Pointer
argenctl pointer-binding set normal Super BTN_LEFT move-window
argenctl pointer-binding set normal Super BTN_RIGHT resize-window

# Context
argenctl binding set normal Super+Shift+Control X context close
argenctl binding set normal Super Tab context switch --last

# Switch contexts interactively
argenctl binding set normal Super Space sh 'argenctl context switch "$(argenctl context list | fuzzel -d)"'

# Create new context interactively
argenctl binding set normal Super+Shift Space sh '
    set -eu
    prompt="$(fuzzel -dl0 -p"new > ")"
    argenctl context new "$prompt"
'

# Rename current context interactively
argenctl binding set normal Super+Control Space sh '
    set -eu
    prompt="$(fuzzel -dl0 -p"rename > ")"
    argenctl context rename "$prompt"
'

# Assign currently focused window to be toggled with Super U
argenctl binding set normal Super+Shift U sh '
    set -eu
    window_id=$(argenctl window list --json | jq -r ".[] | select(.focused) | .id")
    argenctl binding set normal Super U window toggle $window_id
'

# Attach window interactively
argenctl binding set normal Super+Shift D sh '
    set -eu
    id=$(argenctl window list | fuzzel -d --with-nth 2 --accept-nth 1)
    argenctl window attach "$id"
'

# Adopt all orphans
argenctl binding set normal Super+Control D sh '
    argenctl window list --json | jq -r ".[] | select(.contexts | length == 0) | .id" | while read -r id; do
        argenctl window attach "$id"
    done
'

# Quit after confirmed with 'yes'
argenctl binding set normal Super+Shift Q sh '
    set -eu
    confirm="$(fuzzel -dl0 -p"quit? > ")"
    if [ "$confirm" = yes ]; then
        argenctl exit
    fi
'

# Audio and brightness controls
VOL_DEC='wpctl set-volume @DEFAULT_SINK@ --limit 1.5 5%-'
VOL_INC='wpctl set-mute @DEFAULT_SINK@ 0 && wpctl set-volume @DEFAULT_SINK@ --limit 1.5 5%+'
VOL_TOGGLE='wpctl set-mute @DEFAULT_SINK@ toggle'
for mode in normal locked; do
    argenctl binding set $mode Super Bracketleft sh "$VOL_DEC"
    argenctl binding set $mode Super Bracketright sh "$VOL_INC"
    argenctl binding set $mode Super M sh "$VOL_TOGGLE"
    argenctl binding set $mode None XF86AudioLowerVolume sh "$VOL_DEC"
    argenctl binding set $mode None XF86AudioRaiseVolume sh "$VOL_INC"
    argenctl binding set $mode None XF86AudioMute sh "$VOL_TOGGLE"
    argenctl binding set $mode None XF86MonBrightnessDown sh "brightnessctl s 5%-"
    argenctl binding set $mode None XF86MonBrightnessUp sh "brightnessctl s +5%"
    argenctl binding set $mode Super+Shift Bracketleft sh "brightnessctl s 5%-"
    argenctl binding set $mode Super+Shift Bracketright sh "brightnessctl s +5%"
done

# Passthrough mode
argenctl mode declare passthrough
argenctl binding set normal Super Grave mode enter passthrough
argenctl binding set passthrough Super Grave mode enter normal

# Select/swap window by index
for num in 1 2 3 4 5 6 7 8 9; do
    idx=$((num - 1))
    argenctl binding set normal Super $num sh "
        argenctl window focus \$(argenctl context list --json | jq -r '.[] | select(.current) | .windows | .[$idx]')
    "
    argenctl binding set normal Super+Shift $num sh "
        context_id=\$(argenctl context list --json | jq -r '.[] | select(.current) | .id')
        length=\$(argenctl context list --json | jq -r '.[] | select(.current) | .windows | length')
        focused_window=\$(argenctl context list --json | jq -r '.[] | select(.current) | .focused_window')
        dest_window=\$(argenctl context list --json | jq -r '.[] | select(.current) | .windows | .[$idx]')
        focused_output_id=\$(argenctl window list --json | jq -r \".[] | select(.id == \\\"\$focused_window\\\") | .contexts[] | select(.id = \$context_id) | .output_id\")
        dest_output_id=\$(argenctl window list --json | jq -r \".[] | select(.id == \\\"\$dest_window\\\") | .contexts[] | select(.id = \$context_id) | .output_id\")
        if [ -n \"\$focused_output_id\" -a \"\$focused_output_id\" = \"\$dest_output_id\" ]; then
            for i in \$(seq \$length); do
                if [ \"\$focused_window\" = \"\$dest_window\" ]; then
                    break
                fi
                argenctl window swap next
                dest_window=\$(argenctl context list --json | jq -r '.[] | select(.current) | .windows | .[$idx]')
            done
        fi
    "
done

# Rules
argenctl rule add float --app-id "my-app"
