#!/usr/bin/env bash


DIVIDER="---------"
RESCAN_BUTTON="Rescan"
BACK="Back"


# Load NetworkManager
network_status=$(ps aux | grep NetworkManager | grep root)

# If the service is not running
if [ "$network_status" = "" ]; then
	rofi -dmenu -password -p "Root Password: " | sudo -S NetworkManager
fi

# Starts a scan of available broadcasting SSIDs
notify-send "Getting list of available Wi-Fi networks..."

# Get active connection
active_network=$(nmcli device status | awk '{print $4}' | sed "/--/d" | sed "/(externally/d" | sed "/CONNECTION/d")

if [ "$active_network" = "" ]; then
	active_network="Not Found"
fi

wifi_list=$(nmcli --fields "SECURITY,SSID" device wifi list | sed 1d | sed 's/  */ /g' | sed -E "s/WPA*.?\S/ /g" | sed "s/^--/ /g" | sed "s/  //g" | sed "/--/d")

# Gives a list of known connections so we can parse it later
connected=$(nmcli -fields WIFI g)
if [[ "$connected" =~ "enabled" ]]; then
	toggle="Disable Wi-Fi"
elif [[ "$connected" =~ "disabled" ]]; then
	toggle="Enable Wi-Fi"
fi

choose_options="$wifi_list\n$DIVIDER\n$toggle\n$RESCAN_BUTTON"

# If wifi is disabled, then the options change
if [ "$wifi_list" = "" ]; then
	choose_options="$toggle"
fi

chosen_network=$(echo -e "$choose_options" | uniq -u | rofi -dmenu -i -selected-row 0 -p "$active_network" )
chosen_id=$(echo "${chosen_network:3}" | xargs)

# Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
if [ "$chosen_network" = "" ]; then
	exit
elif [ "$chosen_network" = "Enable Wi-Fi" ]; then
	nmcli radio wifi on
elif [ "$chosen_network" = "Disable Wi-Fi" ]; then
	nmcli radio wifi off
elif [ "$chosen_network" = "Rescan" ]; then
	notify-send "Device rescan started..."
	nmcli radio wifi off && nmcli radio wifi on && sleep 3 && sh "$HOME/bin/wifimenu"
else
	# Message to show when connection is activated successfully
	success_message="You are now connected to the Wi-Fi network \"$chosen_id\"."
	# Get known connections
	saved_connections=$(nmcli -g NAME connection)
	if [[ $(echo "$saved_connections" | grep -w "$chosen_id") = "$chosen_id" ]]; then
		# Display information about the currently selected device
		modified_id=$(echo "$chosen_id" | tr -d '[:space:]' | awk '{print tolower($0)}')
		connection_info=$(nmcli --fields "SSID,BSSID,MODE,FREQ,RATE,SECURITY" device wifi list | sed "s/$chosen_id/$modified_id/" | awk -v chosen_id="$modified_id" '$1 == chosen_id {print "BS:", $2, "\nMODE:", $3, "\nFREQ:", $4, $5, "\nRATE:", $6, $7, "\nSECURITY:", $8}')

		# Processing logic in connection information
		wifi_details_options="$connection_info\n$DIVIDER\nChange password\nForget access point\nShare connection\n$BACK"
		chosen="$(echo -e "$wifi_details_options" | uniq -u | rofi -dmenu -i -selected-row 0 -p "$chosen_id")"
		echo $chosen

		case "$chosen" in
	        "" | "$DIVIDER")
	            echo "No option chosen."
	            ;;
			"Change password")
				new_password=$(rofi -dmenu -password -p "Password: ")
				nmcli con modify "$chosen_id" wifi-sec.psk "$new_password"
				notify-send "The password for point '$chosen_id' has been changed."
				;;
			"Forget access point")
				nmcli connection delete $chosen_id
				notify-send "The '$chosen_id' access point was forgotten."
				;;
			"Back")
				sh "$HOME/bin/wifimenu"
				;;
			"Share connection")
				alacritty -e sh -c 'nmcli dev wifi show-password; read -n 1 -s -r -p "Press any key to exit"'
				;;
	        *)
	        echo $chosen | xclip -selection clipboard
	        ;;
	    esac

		nmcli connection up id "$chosen_id" | grep "successfully" && notify-send "Connection Established" "$success_message"
		
	else
		if [[ "$chosen_network" =~ "" ]]; then
			wifi_password=$(rofi -dmenu -password -p "Password: " )
		fi
		nmcli device wifi connect "$chosen_id" password "$wifi_password" | grep "successfully" && notify-send "Connection Established" "$success_message"
	fi
fi
