#!/usr/bin/env fish

function open_semester
	set recent "/home/never/Documents/InNa/25_WiSe/"
	set options (ls $recent)
	set input (menu $options)

	alacritty --working-directory $recent/$input
end

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 --working-directory (xdg-user-dir)/Documents/
		case $options[3]
			alacritty --working-directory (xdg-user-dir)/Music/
		case $options[4]
			alacritty --working-directory (xdg-user-dir)/Pictures/
		case $options[5]
			alacritty --working-directory (xdg-user-dir)/Videos/
		case $options[6]
			alacritty --working-directory (xdg-user-dir)/Downloads/
    end
end

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

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

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

function network_connection
	set options (nmcli --terse --fields name connection show)
	set input (menu $options)

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

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) + 1)
	
	notify-send "Connect to bluetooth device $names[$input]" (bluetoothctl connect $ids[$input] | string collect)
end

function color_scheme
	set options "Light" "Dark"
	set input (menu $options)

	switch $input
		case $options[1]
			dconf write /org/gnome/desktop/interface/color-scheme \'prefer-light\'
		case $options[2]
			dconf write /org/gnome/desktop/interface/color-scheme \'prefer-dark\'
	end
end

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

switch $input
    case $options[1]
		open_location

	case $options[2]
		open_course

	case $options[3]
		audio_source

	case $options[4]
		audio_sink
	
	case $options[5]
		bluetooth_connection

	case $options[6]
		network_connection
	
	case $options[7]
		color_scheme
end
