// Code generated by "./generator ./system/com.deepin.system.procs"; DO NOT EDIT.

// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package procs

import (
	"errors"
	"fmt"
	"unsafe"

	"github.com/godbus/dbus/v5"
	"github.com/linuxdeepin/go-lib/dbusutil"
	"github.com/linuxdeepin/go-lib/dbusutil/proxy"
)

type Procs interface {
	procs // interface com.deepin.system.Procs
	proxy.Object
}

type objectProcs struct {
	interfaceProcs // interface com.deepin.system.Procs
	proxy.ImplObject
}

func NewProcs(conn *dbus.Conn) Procs {
	obj := new(objectProcs)
	obj.ImplObject.Init_(conn, "com.deepin.system.Procs", "/com/deepin/system/Procs")
	return obj
}

type procs interface {
	ConnectExecProc(cb func(ExecPath string, CGroupPath string, Pid string, PPid string)) (dbusutil.SignalHandlerId, error)
	ConnectExitProc(cb func(ExecPath string, CGroupPath string, Pid string, PPid string)) (dbusutil.SignalHandlerId, error)
	Procs() PropProcsMap
}

type interfaceProcs struct{}

func (v *interfaceProcs) GetObject_() *proxy.ImplObject {
	return (*proxy.ImplObject)(unsafe.Pointer(v))
}

func (*interfaceProcs) GetInterfaceName_() string {
	return "com.deepin.system.Procs"
}

// signal ExecProc

func (v *interfaceProcs) ConnectExecProc(cb func(ExecPath string, CGroupPath string, Pid string, PPid string)) (dbusutil.SignalHandlerId, error) {
	if cb == nil {
		return 0, errors.New("nil callback")
	}
	obj := v.GetObject_()
	rule := fmt.Sprintf(
		"type='signal',interface='%s',member='%s',path='%s',sender='%s'",
		v.GetInterfaceName_(), "ExecProc", obj.Path_(), obj.ServiceName_())

	sigRule := &dbusutil.SignalRule{
		Path: obj.Path_(),
		Name: v.GetInterfaceName_() + ".ExecProc",
	}
	handlerFunc := func(sig *dbus.Signal) {
		var ExecPath string
		var CGroupPath string
		var Pid string
		var PPid string
		err := dbus.Store(sig.Body, &ExecPath, &CGroupPath, &Pid, &PPid)
		if err == nil {
			cb(ExecPath, CGroupPath, Pid, PPid)
		}
	}

	return obj.ConnectSignal_(rule, sigRule, handlerFunc)
}

// signal ExitProc

func (v *interfaceProcs) ConnectExitProc(cb func(ExecPath string, CGroupPath string, Pid string, PPid string)) (dbusutil.SignalHandlerId, error) {
	if cb == nil {
		return 0, errors.New("nil callback")
	}
	obj := v.GetObject_()
	rule := fmt.Sprintf(
		"type='signal',interface='%s',member='%s',path='%s',sender='%s'",
		v.GetInterfaceName_(), "ExitProc", obj.Path_(), obj.ServiceName_())

	sigRule := &dbusutil.SignalRule{
		Path: obj.Path_(),
		Name: v.GetInterfaceName_() + ".ExitProc",
	}
	handlerFunc := func(sig *dbus.Signal) {
		var ExecPath string
		var CGroupPath string
		var Pid string
		var PPid string
		err := dbus.Store(sig.Body, &ExecPath, &CGroupPath, &Pid, &PPid)
		if err == nil {
			cb(ExecPath, CGroupPath, Pid, PPid)
		}
	}

	return obj.ConnectSignal_(rule, sigRule, handlerFunc)
}

// property Procs a{s(ssss)}

func (v *interfaceProcs) Procs() PropProcsMap {
	return &implPropProcsMap{
		Impl: v,
		Name: "Procs",
	}
}

type PropProcsMap interface {
	Get(flags dbus.Flags) (value map[string]ProcMessage, err error)
	Set(flags dbus.Flags, value map[string]ProcMessage) error
	ConnectChanged(cb func(hasValue bool, value map[string]ProcMessage)) error
}

type implPropProcsMap struct {
	Impl proxy.Implementer
	Name string
}

func (p implPropProcsMap) Get(flags dbus.Flags) (value map[string]ProcMessage, err error) {
	err = p.Impl.GetObject_().GetProperty_(flags, p.Impl.GetInterfaceName_(),
		p.Name, &value)
	return
}

func (p implPropProcsMap) Set(flags dbus.Flags, value map[string]ProcMessage) error {
	return p.Impl.GetObject_().SetProperty_(flags, p.Impl.GetInterfaceName_(), p.Name, value)
}

func (p implPropProcsMap) ConnectChanged(cb func(hasValue bool, value map[string]ProcMessage)) error {
	if cb == nil {
		return errors.New("nil callback")
	}
	cb0 := func(hasValue bool, value interface{}) {
		if hasValue {
			var v map[string]ProcMessage
			err := dbus.Store([]interface{}{value}, &v)
			if err != nil {
				return
			}
			cb(true, v)
		} else {
			cb(false, nil)
		}
	}
	return p.Impl.GetObject_().ConnectPropertyChanged_(p.Impl.GetInterfaceName_(),
		p.Name, cb0)
}
