#!/usr/bin/env bash
# NOTE: shows battery info, requires upower
# pacman -S upower

# Colors
RESET="\033[0m"
BOLD="\033[1m"
DIM="\033[2m"
RED="\033[31m"
YELLOW="\033[33m"
GREEN="\033[32m"
CYAN="\033[36m"
BLUE="\033[34m"
WHITE="\033[37m"

# Check for upower
if ! command -v upower &>/dev/null; then
	echo -e "${RED}Error: upower is not installed.${RESET}"
	exit 1
fi

# Get all battery paths
batteries=$(upower -e | grep -i battery)

if [[ -z "$batteries" ]]; then
	echo -e "${YELLOW}No batteries detected.${RESET}"
	exit 0
fi

echo -e "${BOLD}${CYAN}⚡ Battery Report${RESET}"
echo -e "${DIM}$(date)${RESET}"
echo

battery_num=0

while IFS= read -r path; do
	[[ -z "$path" ]] && continue
	battery_num=$((battery_num + 1))

	info=$(upower -i "$path" 2>/dev/null)
	[[ -z "$info" ]] && continue

	# Parse fields
	get() { echo "$info" | grep -i "^\s*$1:" | head -1 | sed 's/.*: *//' | xargs; }

	state=$(get state)
	percentage=$(get percentage)
	capacity=$(get capacity)
	energy=$(get energy)
	energy_full=$(get energy-full)
	energy_full_design=$(get "energy-full-design")
	energy_rate=$(get energy-rate)
	voltage=$(get voltage)
	time_to=$(echo "$info" | grep -i "time to" | head -1 | sed 's/.*: *//' | xargs)
	time_label=$(echo "$info" | grep -i "time to" | head -1 | sed 's/:.*//' | xargs)
	technology=$(get technology)
	vendor=$(get vendor)
	model=$(get model)
	warning=$(get warning-level)

	# Health color
	health_pct="${capacity//%/}"
	if [[ "$health_pct" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
		health_val=$(echo "$health_pct" | cut -d. -f1)
		if ((health_val >= 90)); then
			health_color="$GREEN"
		elif ((health_val >= 75)); then
			health_color="$YELLOW"
		else health_color="$RED"; fi
	else
		health_color="$WHITE"
	fi

	# Charge color
	charge_pct="${percentage//%/}"
	if [[ "$charge_pct" =~ ^[0-9]+$ ]]; then
		if ((charge_pct >= 60)); then
			charge_color="$GREEN"
		elif ((charge_pct >= 20)); then
			charge_color="$YELLOW"
		else charge_color="$RED"; fi
	else
		charge_color="$WHITE"
	fi

	# State icon
	case "$state" in
	charging)
		state_icon="󰂄"
		state_color="$GREEN"
		;;
	discharging)
		state_icon="󰁽"
		state_color="$YELLOW"
		;;
	fully-charged)
		state_icon="󰁹"
		state_color="$CYAN"
		;;
	pending-charge)
		state_icon="󰂃"
		state_color="$BLUE"
		;;
	*)
		state_icon="󰂑"
		state_color="$WHITE"
		;;
	esac

	# Warning
	warn_str=""
	if [[ "$warning" != "none" && -n "$warning" ]]; then
		warn_str=" ${RED}${BOLD}[⚠ $warning]${RESET}"
	fi

	# Battery name from path
	batt_name=$(basename "$path")

	echo -e "${BOLD}${BLUE}Battery $battery_num${RESET} ${DIM}($batt_name)${RESET}${warn_str}"
	[[ -n "$vendor" || -n "$model" ]] &&
		echo -e "  ${DIM}${vendor} ${model}${RESET}"
	echo -e "  ${state_color}${state_icon} ${BOLD}${state}${RESET}"
	echo

	echo -e "  ${WHITE}Charge   ${RESET}${charge_color}${BOLD}${percentage}${RESET}"
	echo -e "  ${WHITE}Health   ${RESET}${health_color}${BOLD}${capacity}${RESET}"

	[[ -n "$time_to" ]] &&
		echo -e "  ${WHITE}${time_label:-Time}  ${RESET}${BOLD}${time_to}${RESET}"

	[[ -n "$energy_rate" && "$energy_rate" != "0 W" ]] &&
		echo -e "  ${WHITE}Rate     ${RESET}${BOLD}${energy_rate}${RESET}"

	echo
	echo -e "  ${DIM}Energy now  ${energy}${RESET}"
	echo -e "  ${DIM}Energy full ${energy_full}${RESET}"
	echo -e "  ${DIM}Design full ${energy_full_design}${RESET}"
	[[ -n "$voltage" ]] && echo -e "  ${DIM}Voltage     ${voltage}${RESET}"
	[[ -n "$technology" ]] && echo -e "  ${DIM}Technology  ${technology}${RESET}"

	echo
	echo -e "${DIM}────────────────────────────────${RESET}"
	echo

done <<<"$batteries"
