#!/bin/bash
# Mengambil persentase baterai menggunakan termux-api
BAT_INFO=$(termux-battery-status)
PERCENT=$(echo $BAT_INFO | jq -r '.percentage')
STATUS=$(echo $BAT_INFO | jq -r '.status')
GET_ICON=$(xfconf-query -c xsettings -p /Net/IconThemeName)

if [ ! -f ~/.config/xfce4/battery.cfg ]; then
    echo -e "ICON_PATH=\nSHOW_PERCENTAGE=no\nSHOW_CHECKBOX=yes" > ~/.config/xfce4/battery.cfg
fi

source ~/.config/xfce4/battery.cfg

if [ "$ICON_PATH" = "" ];then
    ICON_PATH="/usr/share/icons/$GET_ICON/panel"
fi

# Logika pemilihan ikon Flat-Remix berdasarkan persentase
if [ "$PERCENT" -le 10 ]; then
    ICON="battery-010"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-010-charging"
    fi
elif [ "$PERCENT" -le 20 ]; then
    ICON="battery-020"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-020-charging"
    fi
elif [ "$PERCENT" -le 30 ]; then
    ICON="battery-030"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-030-charging"
    fi
elif [ "$PERCENT" -le 40 ]; then
    ICON="battery-040"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-040-charging"
    fi
elif [ "$PERCENT" -le 50 ]; then
    ICON="battery-050"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-050-charging"
    fi
elif [ "$PERCENT" -le 60 ]; then
    ICON="battery-060"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-060-charging"
    fi
elif [ "$PERCENT" -le 70 ]; then
    ICON="battery-070"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-070-charging"
    fi
elif [ "$PERCENT" -le 80 ]; then
    ICON="battery-080"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-080-charging"
    fi
elif [ "$PERCENT" -le 90 ]; then
    ICON="battery-090"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-090-charging"
    fi
else
    ICON="battery-100"
    if [ "$STATUS" = "CHARGING" ]; then
        ICON="battery-100-charging"
    fi
fi

# Format output untuk Genmon (bisa menggunakan tag XML untuk ikon/warna)
echo "<img>$ICON_PATH/$ICON.svg</img>" # Opsional: Tambahkan ikon
if [ "$SHOW_PERCENTAGE" = "yes" ]; then
    echo "<txt>$PERCENT%</txt>"
fi
if [ "$SHOW_CHECKBOX" = "yes" ]; then
    echo "<tool>Battery Level: $PERCENT%
Battery Status: $STATUS</tool>"
fi
