#!/usr/bin/env fish

# Select folders in current semester
function open_semester
	set options (ls -a $argv[1])
	set input (menu $options || exit)

	if test "$input" = "."
		alacritty msg create-window --working-directory $argv[1]
	else if test -d $argv[1]/$input
		open_semester $argv[1]/$input
	else if test (xdg-mime query filetype $argv[1]/$input) = "application/pdf" && test (menu "PDF" "Ipe") = "Ipe"
		ipe $argv[1]/$input
	else
		xdg-open $argv[1]/$input
	end
end

# Find files in home directory
function find_file
	set file (find ~/Desktop ~/Documents ~/Downloads ~/Pictures ~/Videos -printf "%p\t%f\n" | fuzzel --with-nth 2 --accept-nth 1 --dmenu --match-mode=fuzzy)
	
	if test (xdg-mime query filetype $file) = "application/pdf" && test (menu "PDF" "Ipe") = "Ipe"
		ipe $file
	else
		xdg-open $file
	end
end

# Open WueCampus Courses
function open_course
	set options "Algorithmische Graphentheorie" "Theoretische Informatik" "Energy-Aware Engineering" "Nachhaltigkeitskonzepte" "Softwareengineering"
	set input (menu $options)

	switch $input
		case $options[1]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=78644"
		case $options[2]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=79649"
		case $options[3]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=78361"
		case $options[4]
			xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=79204"
        case $options[5]
            xdg-open "https://wuecampus.uni-wuerzburg.de/moodle/course/view.php?id=79247"
	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

function set_keyboard_backlight
    brightnessctl --device=white:kbd_backlight set (menu --index "0" "1" "2" "3" "4")
end

function mirror_screen
	wl-mirror eDP-1
end

# Select sub menu
set options "󰑴 Open Semester" "󰑴 Open WueCampus Course" "󰈞 Find Files" "󰍬 Select Audio Source" "󰕾 Select Audio Sink" "󰂯 Select Bluetooth" "󰛳 Select Network" "󰔎 Select Color Scheme" "󰂚 Restore Notifications" "󰌌 Set Keyboard Backlight" "󰍺 Mirror Screen"
set input (menu $options)

switch $input
    case $options[1]
		open_semester "/home/never/Documents/InNa/26_SoSe/"

	case $options[2]
		open_course

    case $options[3]
		find_file

	case $options[4]
        wpsourcemenu.fish
		# audio_source

	case $options[5]
		wpsinkmenu.fish
        # audio_sink

	case $options[6]
        bluetoothmenu.fish
		# bluetooth_connection

	case $options[7]
        networkmenu.fish
		# network_connection

	case $options[8]
		color_scheme

    case $options[9]
        restore_notifications

    case $options[10]
        set_keyboard_backlight

    case $options[11]
        mirror_screen
end
