/*This file is automatically generated by pkg.deepin.io/dbus-generator. Don't edit it*/
package upower

import "pkg.deepin.io/lib/dbus"
import "pkg.deepin.io/lib/dbus/property"
import "reflect"
import "sync"
import "runtime"
import "fmt"
import "errors"

/*prevent compile error*/
var _ = fmt.Println
var _ = runtime.SetFinalizer
var _ = sync.NewCond
var _ = reflect.TypeOf
var _ = property.BaseObserver{}

type QoS struct {
	Path     dbus.ObjectPath
	DestName string
	core     *dbus.Object

	signals       map[<-chan *dbus.Signal]struct{}
	signalsLocker sync.Mutex
}

func (obj *QoS) _createSignalChan() <-chan *dbus.Signal {
	obj.signalsLocker.Lock()
	ch := getBus().Signal()
	obj.signals[ch] = struct{}{}
	obj.signalsLocker.Unlock()
	return ch
}
func (obj *QoS) _deleteSignalChan(ch <-chan *dbus.Signal) {
	obj.signalsLocker.Lock()
	delete(obj.signals, ch)
	getBus().DetachSignal(ch)
	obj.signalsLocker.Unlock()
}
func DestroyQoS(obj *QoS) {
	obj.signalsLocker.Lock()
	defer obj.signalsLocker.Unlock()
	if obj.signals == nil {
		return
	}
	for ch, _ := range obj.signals {
		getBus().DetachSignal(ch)
	}
	obj.signals = nil

	runtime.SetFinalizer(obj, nil)

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus.Properties',sender='" + obj.DestName + "',member='PropertiesChanged'")
	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.UPower.QoS',sender='" + obj.DestName + "',member='PropertiesChanged'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.UPower.QoS',sender='" + obj.DestName + "',member='RequestsChanged'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.UPower.QoS',sender='" + obj.DestName + "',member='LatencyChanged'")

}

func (obj *QoS) GetLatencyRequests() (requests [][]interface{}, _err error) {
	_err = obj.core.Call("org.freedesktop.UPower.QoS.GetLatencyRequests", 0).Store(&requests)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *QoS) GetLatency(type_ string) (value int32, _err error) {
	_err = obj.core.Call("org.freedesktop.UPower.QoS.GetLatency", 0, type_).Store(&value)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *QoS) CancelRequest(cookie uint32) (_err error) {
	_err = obj.core.Call("org.freedesktop.UPower.QoS.CancelRequest", 0, cookie).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *QoS) RequestLatency(type_ string, value int32, persistent bool) (cookie uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.UPower.QoS.RequestLatency", 0, type_, value, persistent).Store(&cookie)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *QoS) SetMinimumLatency(type_ string, value int32) (_err error) {
	_err = obj.core.Call("org.freedesktop.UPower.QoS.SetMinimumLatency", 0, type_, value).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *QoS) ConnectRequestsChanged(callback func()) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.UPower.QoS.RequestsChanged" || 0 != len(v.Body) {
				continue
			}

			callback()
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func (obj *QoS) ConnectLatencyChanged(callback func(arg0 string, arg1 int32)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.UPower.QoS.LatencyChanged" || 2 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[1]) != reflect.TypeOf((*int32)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(string), v.Body[1].(int32))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func NewQoS(destName string, path dbus.ObjectPath) (*QoS, error) {
	if !path.IsValid() {
		return nil, errors.New("The path of '" + string(path) + "' is invalid.")
	}

	core := getBus().Object(destName, path)

	obj := &QoS{Path: path, DestName: destName, core: core, signals: make(map[<-chan *dbus.Signal]struct{})}

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.UPower.QoS',sender='" + obj.DestName + "',member='RequestsChanged'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.UPower.QoS',sender='" + obj.DestName + "',member='LatencyChanged'")

	runtime.SetFinalizer(obj, func(_obj *QoS) { DestroyQoS(_obj) })
	return obj, nil
}
