#!/usr/bin/env python3
import os
import time

from psutil import process_iter, sensors_battery
from setproctitle import setproctitle


APP_NAME = "BSC"  # Battery status checker


class config:
    low = 15
    critical = 5
    sleep = 2
    debug = False


def debug(level):
    if config.debug:
        print(level)
        print("Charging: ", sensors_battery().power_plugged)
        print("Battery: ", sensors_battery().percent)


def main():
    while True:
        battery = sensors_battery()
        if (
            not battery.power_plugged and
            config.critical < int(battery.percent) and
            int(battery.percent) <= config.low
        ):
            os.system('notify-send -u critical -r "6896" "Battery" "Low battery level"')
        elif (
            not battery.power_plugged and
            int(battery.percent) <= config.critical
        ):
            os.system('notify-send -u critical -r "6896" "Battery" "Critical battery level"')
        time.sleep(config.sleep)


for proc in process_iter(["pid", "name"]):
    if proc.name() == APP_NAME and proc.pid != os.getpid():
        print(proc)
        os.kill(proc.pid, 9)


if __name__ == "__main__":
    setproctitle(APP_NAME)
    list_of_procs = []
    main()
