/*
Copyright (c) Edgeless Systems GmbH

SPDX-License-Identifier: AGPL-3.0-only
*/

package deploy

import (
	"context"

	"github.com/coreos/go-systemd/v22/dbus"
)

// wraps go-systemd dbus.
type dbusWrapper struct{}

func (d *dbusWrapper) NewSystemConnectionContext(ctx context.Context) (dbusConn, error) {
	conn, err := dbus.NewSystemConnectionContext(ctx)
	if err != nil {
		return nil, err
	}
	return &dbusConnWrapper{
		conn: conn,
	}, nil
}

type dbusConnWrapper struct {
	conn *dbus.Conn
}

func (c *dbusConnWrapper) StartUnitContext(ctx context.Context, name string, mode string, ch chan<- string) (int, error) {
	return c.conn.StartUnitContext(ctx, name, mode, ch)
}

func (c *dbusConnWrapper) StopUnitContext(ctx context.Context, name string, mode string, ch chan<- string) (int, error) {
	return c.conn.StopUnitContext(ctx, name, mode, ch)
}

func (c *dbusConnWrapper) RestartUnitContext(ctx context.Context, name string, mode string, ch chan<- string) (int, error) {
	return c.conn.RestartUnitContext(ctx, name, mode, ch)
}

func (c *dbusConnWrapper) ReloadContext(ctx context.Context) error {
	return c.conn.ReloadContext(ctx)
}

func (c *dbusConnWrapper) Close() {
	c.conn.Close()
}
