#!/usr/bin/env bash


restart_dwm() {
    cur=$(xprop -root _NET_CURRENT_DESKTOP | awk '{print log($3)/log(2)+1}')
    [[ $(pidof -s dwm) ]] && kill -HUP $(pidof -s dwm) && xdotool key Super_L
    [[ $(pidof -s pdwm) ]] && kill -HUP $(pidof -s pdwm) && xdotool key Super_L
    (sleep 0.1 && xdotool key "Super_L+$cur")
}

[[ $1 = "restart" ]] && restart_dwm && exit

[[ -f "$HOME/.config/phyos/phyos.conf" ]] && source "$HOME/.config/phyos/phyos.conf"

dir="$pOS_CONFIG_DIR/rofi/powermenu"
theme="default"

[[ ! -z $POWERMENU_TYPE ]] && \
[[ $POWERMENU_TYPE != "Default" ]] && \
[[ ! -z $POWERMENU_STYLE ]] && \
dir="$dir/$POWERMENU_TYPE" && \
theme="$POWERMENU_STYLE"
lastlogin="`last $USER | head -n1 | tr -s ' ' | cut -d' ' -f5,6,7`"

uptime=$(uptime -p | sed -e 's/up //g')
host=$(hostnamectl --static)

case "${POWERMENU_TYPE: -1}" in
    1) rofi_p="Uptime: $uptime"
    rofi_m="Uptime: $uptime" ;;
    2) rofi_p="Goodbye ${USER}"
    rofi_m="Uptime: $uptime" ;;
    3) rofi_p="$USER@$host"
    rofi_m="Last Login: $lastlogin |    Uptime: $uptime" ;;
    4) rofi_p="$USER@$host"
    rofi_m="  Uptime: $uptime" ;;
esac

pOS-powermenutitle &

rofi_command="rofi -theme $dir/$theme"
# Options
shutdown=""
reboot="ﰇ"
lock=""
logout="﫼"
renew_dwm=""
yes=''
no=''

case "$(readlink -f /sbin/init)" in
	*systemd*) ctl='systemctl' ;;
	*) ctl='loginctl' ;;
esac

rofi_cmd() {
	rofi -dmenu \
		-p "$rofi_p" \
		-mesg "$rofi_m" \
		-select $shutdown \
		-theme ${dir}/${theme}.rasi
}

confirm_cmd() {
    th="${dir}/confirm.rasi"
    if [[ -f $th ]]; then
        rofi -dmenu -p 'Confirmation' -mesg 'Are you Sure?' -select $yes -theme $th
    else
        [[ $theme = "default" ]] && th="${dir}/type-4/style-1.rasi" || th="${dir}/${theme}.rasi"
        rofi -theme-str 'window {location: center; anchor: center; fullscreen: false; width: 350px;}' \
            -theme-str 'mainbox {orientation: vertical; children: [ "message", "listview" ];}' \
            -theme-str 'listview {columns: 2; lines: 1;}' \
            -theme-str 'element-text {horizontal-align: 0.5;}' \
            -theme-str 'textbox {horizontal-align: 0.5;}' \
            -dmenu \
            -p 'Confirmation' \
            -mesg 'Are you Sure?' \
            -select $yes \
            -theme $th
    fi
}

# Ask for confirmation
confirm_exit() {
    [[ $POWERMENU_CONFIRM != "true" ]] && return

    sel=$(echo -e "$yes\n$no" | confirm_cmd)
    [[ -z $sel ]] || [[ $sel == "$no" ]] && exit
}

# Variable passed to rofi
options="$logout\n$lock\n$shutdown\n$reboot\n$renew_dwm"

if [[ $theme = "default" ]]; then
    chosen="$(echo -e "$options" | $rofi_command -p "Uptime: $uptime" -dmenu -selected-row 2)"
else
    chosen="$(echo -e "$options" | rofi_cmd)"
fi
xset r rate 250 40
killall -9 pOS-powermenutitle && dunstctl close-all
case $chosen in
    $logout)
        [[ $(pidof -s dwm) ]] && kill -TERM $(pidof -s dwm)
        [[ $(pidof -s pdwm) ]] && kill -TERM $(pidof -s pdwm) ;;
    $lock)
        if pacman -Qq "lightdm" > /dev/null ; then
            dm-tool lock &
        else
            betterlockscreen -l &
        fi ;;
    $shutdown)
        confirm_exit
		$ctl poweroff -i ;;
    $reboot)
        confirm_exit
		$ctl reboot -i ;;
	$renew_dwm)
        restart_dwm ;;
esac
