#!/usr/bin/env python3
import dbus
import dbus.mainloop.glib
from gi.repository import AppIndicator3, Gtk, GLib


class BatteryIndicator:
    def __init__(self):
        self.app = 'battery-indicator'
        self.indicator = AppIndicator3.Indicator.new(
            self.app,
            "/home/teraflops/Icons/dest.png",
            AppIndicator3.IndicatorCategory.SYSTEM_SERVICES
        )
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

        # Cargar el menú
        self.menu = Gtk.Menu()
        self.build_menu()
        self.indicator.set_menu(self.menu)

        # Inicializar D-Bus
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        self.bus = dbus.SystemBus()

        # Conectar con la batería
        self.battery = self.bus.get_object(
            "org.freedesktop.UPower", "/org/freedesktop/UPower/devices/battery_BAT1")
        self.bus.add_signal_receiver(
            self.on_battery_properties_changed,
            dbus_interface="org.freedesktop.DBus.Properties",
            signal_name="PropertiesChanged",
            path="/org/freedesktop/UPower/devices/battery_BAT1"
        )

        # Actualizar el estado inicial
        self.update_battery_status()

    def build_menu(self):
        item_quit = Gtk.MenuItem(label="Salir")
        item_quit.connect("activate", self.quit)
        self.menu.append(item_quit)
        self.menu.show_all()

    def quit(self, _):
        Gtk.main_quit()

    def on_battery_properties_changed(self, interface, changed_properties, _):
        if "Percentage" in changed_properties or "State" in changed_properties:
            self.update_battery_status()

    def update_battery_status(self):
        battery_interface = dbus.Interface(
            self.battery, dbus_interface="org.freedesktop.DBus.Properties")
        percentage = battery_interface.Get(
            "org.freedesktop.UPower.Device", "Percentage")
        state = battery_interface.Get("org.freedesktop.UPower.Device", "State")

        # Determinar icono y tooltip basado en el porcentaje y el estado
        if state == 1:  # Cargando
            icon = "/home/teraflops/Icons/battery_icon_1.png"
            tooltip = f"Batería cargando: {percentage}%"
        elif state == 2:  # Descargando
            if percentage > 80:
                icon = "/home/teraflops/Icons/dest.png"
            elif percentage > 60:
                icon = "/home/teraflops/Icons/dest.png"
            elif percentage > 40:
                icon = "/home/teraflops/Icons/dest.png"
            elif percentage > 20:
                icon = "/home/teraflops/Icons/dest.png"
            else:
                icon = "/home/teraflops/Icons/dest.png"
            tooltip = f"Batería: {percentage}% (Descargando)"
        elif state == 4:  # Totalmente cargada
            icon = "/home/teraflops/Icons/dest.png"
            tooltip = "Batería completamente cargada"
        else:
            icon = "/home/teraflops/Icons/dest.png"
            tooltip = "Estado de batería desconocido"

        # Actualizar el icono y el tooltip
        self.indicator.set_icon_full(icon, tooltip)
        print(f"Icono actualizado a {icon} con tooltip: {tooltip}")


def main():
    BatteryIndicator()
    Gtk.main()


if __name__ == "__main__":
    main()
