from typing import Final

from gateway_transport.dbus.DbusClient import DbusClient


class SystemdManagerClient(DbusClient):
    SERVICE: Final[str] = "org.freedesktop.systemd1"
    PATH: Final[str] = "/org/freedesktop/systemd1"
    IFACE: Final[str] = "org.freedesktop.systemd1.Manager"

    def __init__(self, *, opTimeout: float = 5.0) -> None:
        super().__init__(
            service=self.SERVICE,
            path=self.PATH,
            interface=self.IFACE,
            opTimeout=opTimeout,
        )

    async def startUnit(self, unitName: str, *, mode: str = "replace") -> None:
        await self.callMethod("StartUnit", str(unitName), str(mode))

    async def stopUnit(self, unitName: str, *, mode: str = "replace") -> None:
        await self.callMethod("StopUnit", str(unitName), str(mode))

    async def restartUnit(self, unitName: str, *, mode: str = "replace") -> None:
        await self.callMethod("RestartUnit", str(unitName), str(mode))

    async def getUnitPath(self, unitName: str) -> str:
        # returns object path like /org/freedesktop/systemd1/unit/...
        return await self.callMethod("GetUnit", str(unitName))
