#!/bin/sh

notify() {
	notify-send -e -t 5000 -i "${icon_dir}${1}" -h "string:x-canonical-private-synchronous:${0##*/}" "$2" -u "${3:-normal}"
}

switch_to_ac_mode() {
	hyprctl keyword monitor 'eDP-1',preferred,auto,1.5,bitdepth,10
	hyprctl keyword decoration:blur:enabled true
	hyprctl keyword decoration:shadow:enabled true
	notify "battery-$charge_icon-charging.svg" "AC Mode ($charge%, Charging)"
}

switch_to_battery_mode() {
	hyprctl keyword monitor 'eDP-1',$battery_resolution,auto,1.5,bitdepth,10
	hyprctl keyword decoration:blur:enabled false
	hyprctl keyword decoration:shadow:enabled false
	notify "battery-$charge_icon.svg" "Battery Mode ($charge%, Discharging)"
}

check_battery_status() {
	[ "$(cat "$battery_dir/status")" = "Charging" ] && return 0

	case "$charge" in
		[2-9][0-9] | 100)
			status="normal"
			;;
		[1-2][0-9])
			status="low"
			;;
		[0-9])
			status="critical"
			;;
	esac
	
	case "$status" in
		low | critical)
			notify "battery-$charge_icon.svg" "$charge% Battery $status, Please Plug In Charger!!" "$status"
			;;
		*) ;;
	esac
}

#declaration
dir="/sys/class/power_supply/"
battery="BAT0" #change accordingly
battery_dir="${dir}${battery}/"
icon_dir="/usr/share/icons/Papirus-Dark/22x22/panel/" #change accordingly
status="normal"
charge=$(cat "$battery_dir"/capacity)
battery_resolution="2880x1800@48" # widthxheight@refresh_rate
charge_icon=$(printf "%03d" "$((charge / 10))0")

case "$1" in
	ac)
		switch_to_ac_mode
		;;

	battery)
		switch_to_battery_mode
		;;
	check)
		if [ "$(cat "$dir/ADP1/online")" -eq 0 ]; then
			switch_to_battery_mode
		fi
		;;

	status)
		check_battery_status
		;;

	*) ;;
esac
