#!/usr/bin/env fish

# Select folders in current semester
function open_semester
	set recent "/home/never/Documents/InNa/25_WiSe/"
	set options (ls $recent)
	set input (menu $options)

	alacritty msg create-window --working-directory $recent/$input
end

# Open user directories
function open_location
    set options "25_WiSe" "󰈙 Documents" "󰝚 Music" "󰋩 Pictures" "󰕧 Videos" "󰇚 Downloads"
    set input (menu $options)

	switch $input
		case $options[1]
			open_semester
		case $options[2]
			alacritty msg create-window --working-directory (xdg-user-dir)/Documents/
		case $options[3]
			alacritty msg create-window --working-directory (xdg-user-dir)/Music/
		case $options[4]
			alacritty msg create-window  --working-directory (xdg-user-dir)/Pictures/
		case $options[5]
			alacritty msg create-window --working-directory (xdg-user-dir)/Videos/
		case $options[6]
			alacritty msg create-window --working-directory (xdg-user-dir)/Downloads/
    end
end

# Open WueCampus Courses
function open_course
	set options "Algorithmen und Datenstrukturen" "Modellbildung und Simulation" "Amateurfunkkurs" "Datenbanken"
	set input (menu $options)

	switch $input
		case $options[1]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=75979"
		case $options[2]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=76309"
		case $options[3]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=76586"
		case $options[4]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=76199"
	end
end

# Select the default audio source
function audio_source
	set dump (string trim -c '"' (pw-dump | jq '.[] | select(.info.props."media.class" == "Audio/Source")' | jq '.id, .info.props."node.description"'))

	set ids
	set names

	for i in (seq (count $dump))
		if test (math $i % 2) -eq 0
			set -a names $dump[$i] 
		else
			set -a ids $dump[$i]
		end
	end

	set input (math (menu --index $names) + 1)

	wpctl set-default $ids[$input]
end

# Select the default audio sink
function audio_sink
	set dump (string trim -c '"' (pw-dump | jq '.[] | select(.info.props."media.class" == "Audio/Sink")' | jq '.id, .info.props."node.description"'))
	
	set ids
	set names

	for i in (seq (count $dump))
		if test (math $i % 2) -eq 0
			set -a names $dump[$i] 
		else
			set -a ids $dump[$i]
		end
	end

	set input (math (menu --index $names) + 1)

	wpctl set-default $ids[$input]
end

# Select the network connection profile
function network_connection
	set options (nmcli --terse --fields name connection show)
	set input (menu $options || exit)

	notify-send "Connect to network $input" (nmcli connection up $input)
end

# Select a paired bluetooth connection
function bluetooth_connection
	set dump (bluetoothctl devices | string replace "Device " "" | string split -m 1 " ")
	
	set ids
	set names

	for i in (seq (count $dump))
		if test (math $i % 2) -eq 0
			set -a names $dump[$i] 
		else
			set -a ids $dump[$i]
		end
	end

	set input (math (menu --index $names || exit) + 1)
	
	notify-send "Connect to bluetooth device $names[$input]" (bluetoothctl connect $ids[$input] | string collect)
end

# Select the system color scheme preference
function color_scheme
	set options "Light" "Dark"
	set input (menu $options)

	switch $input
		case $options[1]
            niri msg action do-screen-transition
			dconf write /org/gnome/desktop/interface/color-scheme \'prefer-light\'
            command alacritty msg config -w -1 (cat /home/never/.config/alacritty/gruvbox-light.toml)
		case $options[2]
            niri msg action do-screen-transition
			dconf write /org/gnome/desktop/interface/color-scheme \'prefer-dark\'
            command alacritty msg config -w -1 (cat /home/never/.config/alacritty/gruvbox-dark.toml)
	end
end

function restore_notifications
    for i in (seq (count (string match -r 'Notification [0-9]' (makoctl history))))
        makoctl restore
    end
end


# Select sub menu
set options "󱂵 Open Location" "󰑴 Open WueCampus Course" "󰍬 Select Audio Source" "󰕾 Select Audio Sink" "󰂯 Select Bluetooth" "󰛳 Select Network" "󰔎 Color Scheme" "󰂚 Restore Notifications"
set input (menu $options)

switch $input
    case $options[1]
		open_location

	case $options[2]
		open_course

	case $options[3]
        wpsourcemenu.fish
		# audio_source

	case $options[4]
		wpsinkmenu.fish
        # audio_sink
	
	case $options[5]
        bluetoothmenu.fish
		# bluetooth_connection

	case $options[6]
        networkmenu.fish
		# network_connection
	
	case $options[7]
		color_scheme
    
    case $options[8]
        restore_notifications
end
