#!/bin/bash

# pactl list sinks | grep -e "Name" -e "Description"
# pactl set-default-sink
# HiFi__Headphones__sink -> laptop
# bluez_sink -> bluetooth
# USB_Audio_Device -> usb headset
# usb-Synaptics -> usb-c headset
# <HDMI1/HDMI2/HDMI3>__sink -> Screen
getFormalNameFromSink() {
    case "$1" in
        *HiFi__Headphones__sink*)
            echo "Laptop speakers"
            ;;
        *bluez*)
            echo "Bluetooth audio"
            ;;
        *usb-Synaptics*)
            echo "USB-C audio"
            ;;
        *USB*)
            echo "USB audio"
            ;;
        *HDMI* | *DP* | *DisplayPort*)
            echo "Screen audio"
            ;;
        *pci*)
            echo "Core audio"
            ;;
        *Logitech*)
            echo "Logitech headset"
            ;;
        *)
            echo $1
            ;;
    esac
}

declare -A sink_map
declare menu_options

readarray -t sink_list < <(pactl list sinks | grep -e "Name" | awk -F: '{print $2}')
sink_default=$(pactl get-default-sink)

for sink in "${sink_list[@]}"; do
    formal_name=$(getFormalNameFromSink $sink)
	if [ "$sink" = "$sink_default" ]; then
		formal_name="󰅂 $formal_name"
	fi

    sink_map["$formal_name"]="$sink"
    if [[ ${#menu_options} -ne 0 ]]; then
        menu_options="$menu_options|"
    fi
    menu_options="$menu_options$formal_name"
done

chosen_option="$(rofi -no-config -no-lazy-grab -sep "|" -dmenu -i -p '󰓃 Audio outputs' -lines ${#sink_list[@]} -no-fixed-num-lines -theme ~/.config/rofi/theme.rasi <<< "$menu_options")"

[[ "$chosen_option" == "" ]] && exit 0

chosen_sink="${sink_map["$chosen_option"]}"

pactl set-default-sink $chosen_sink

notify-send -t 3500 "Default Audio output" "$chosen_option"
