#!/bin/sh
current_mode=""
previous_mode=""
kode=$(cat /data/adb/modules/AdaptiveFastCharging/)
code=$(cat /data/adb/modules/AdaptiveFastCharging/)

# Grant execution permission to the script
chmod 666 "$0"
chmod +x "$0"

# Set permissions for the necessary files
chmod 666 /sys/class/power_supply/battery/constant_charge_current
chmod 666 /sys/class/power_supply/battery/charge_control_limit
chmod 666 /sys/class/power_supply/battery/capacity
chmod 666 /sys/class/power_supply/battery/status

# Wait until boot is completed
while [[ -z $(getprop sys.boot_completed) ]]; do 
    sleep 5
done

# Function to send notifications
send_notification() {
    local title="$1"
    local message="$2"
    su -lp 2000 -c "cmd notification post -S bigtext -t '$title' 'Goten Project' '$message'" > /dev/null 2>&1
}

# Monitor charging status
PREV_STATUS=""
CHARGER_ENABLED=1

while true; do
    CHARGER_STATUS=$(cat /sys/class/power_supply/battery/status)
    BATTERY_CAPACITY=$(cat /sys/class/power_supply/battery/capacity)

    # Read chipset and remove strange characters
    CHIPSET=$(cat /proc/device-tree/model | tr -d '\0')

    if [[ "$CHARGER_STATUS" == "Charging" && "$PREV_STATUS" != "Charging" ]]; then
        # Fast Charging only applies when plugged in for the first time
        if echo "$CHIPSET" | grep -qi "mediatek"; then
            # MediaTek Fast Charging
            echo "0" > /sys/devices/platform/charger/tran_aichg_disable_charger
            echo "9000000" > /sys/firmware/devicetree/base/lk_charger/max_charger_voltage
            echo "6500000" > /sys/firmware/devicetree/base/lk_charger/fast_charge_voltage
            echo "4500000" > /sys/firmware/devicetree/base/lk_charger/ac_charger_input_current

            echo "9000000" > /sys/devices/platform/charger/power_supply/mtk-master-charger/constant_charge_current_max
            echo "9000000" > /sys/devices/platform/charger/power_supply/mtk-slave-charger/constant_charge_current_max
            echo "9000000" > /sys/devices/platform/charger/power_supply/mtk-slv-div-chg/constant_charge_current_max
            echo "9000000" > /sys/devices/platform/charger/power_supply/mtk-mst-div-chg/constant_charge_current_max
            echo "9000000" > /sys/devices/platform/soc/11017000.i2c/i2c-5/5-0053/power_supply/charger/constant_charge_current
            echo "6500000" > /sys/devices/platform/soc/11017000.i2c/i2c-5/5-0053/power_supply/charger/constant_charge_voltage

        elif echo "$CHIPSET" | grep -qi "qcom"; then
            # Snapdragon Fast Charging
            echo "6700000" > /sys/class/power_supply/battery/constant_charge_current
            echo "1" > /sys/class/power_supply/battery/charge_control_limit
        fi

        send_notification "Fast Charger Active" "Fast charging mode enabled."

        CHARGER_ENABLED=1  # Make sure the charger can be turned off at 100%

    elif [[ "$BATTERY_CAPACITY" -ge 100 && "$CHARGER_STATUS" == "Charging" && "$CHARGER_ENABLED" -eq 1 ]]; then
        # Stop charging when the battery is full
        if echo "$CHIPSET" | grep -qi "mediatek"; then
            echo "1" > /sys/devices/platform/charger/tran_aichg_disable_charger
        elif echo "$CHIPSET" | grep -qi "qcom"; then
            echo "0" > /sys/class/power_supply/battery/charge_control_limit
            echo "0" > /sys/class/power_supply/battery/constant_charge_current
        fi
        
        send_notification "Battery Full" "Charging stopped to preserve battery health."

        # Turn off the charger so that it doesn't stay on all the time.
        CHARGER_ENABLED=0

    elif [[ "$CHARGER_STATUS" == "Not Charging" && "$PREV_STATUS" == "Charging" ]]; then
        # Charger dicabut
        send_notification "Charger Disconnected" "Charging has been stopped."
    fi

    PREV_STATUS="$CHARGER_STATUS"
    sleep 5  # Reduce delay to make scripts more responsive
done
