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

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 Audio struct {
	Path     dbus.ObjectPath
	DestName string
	core     *dbus.Object

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

	SinkInputs    *dbusPropertyAudioSinkInputs
	Cards         *dbusPropertyAudioCards
	DefaultSink   *dbusPropertyAudioDefaultSink
	DefaultSource *dbusPropertyAudioDefaultSource
	MaxUIVolume   *dbusPropertyAudioMaxUIVolume
}

func (obj *Audio) _createSignalChan() <-chan *dbus.Signal {
	obj.signalsLocker.Lock()
	ch := getBus().Signal()
	obj.signals[ch] = struct{}{}
	obj.signalsLocker.Unlock()
	return ch
}
func (obj *Audio) _deleteSignalChan(ch <-chan *dbus.Signal) {
	obj.signalsLocker.Lock()
	delete(obj.signals, ch)
	getBus().DetachSignal(ch)
	obj.signalsLocker.Unlock()
}
func DestroyAudio(obj *Audio) {
	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='com.deepin.daemon.Audio',sender='" + obj.DestName + "',member='PropertiesChanged'")

	obj.SinkInputs.Reset()
	obj.Cards.Reset()
	obj.DefaultSink.Reset()
	obj.DefaultSource.Reset()
	obj.MaxUIVolume.Reset()
}

func (obj *Audio) Reset() (_err error) {
	_err = obj.core.Call("com.deepin.daemon.Audio.Reset", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Audio) SetDefaultSink(arg0 string) (_err error) {
	_err = obj.core.Call("com.deepin.daemon.Audio.SetDefaultSink", 0, arg0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Audio) SetDefaultSource(arg0 string) (_err error) {
	_err = obj.core.Call("com.deepin.daemon.Audio.SetDefaultSource", 0, arg0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Audio) SetPort(arg0 uint32, arg1 string, arg2 int32) (_err error) {
	_err = obj.core.Call("com.deepin.daemon.Audio.SetPort", 0, arg0, arg1, arg2).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

type dbusPropertyAudioSinkInputs struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyAudioSinkInputs) SetValue(notwritable interface{}) {
	fmt.Println("com.deepin.daemon.Audio.SinkInputs is not writable")
}

func (this *dbusPropertyAudioSinkInputs) Get() []dbus.ObjectPath {
	v, _ := this.GetValue()
	return v.([]dbus.ObjectPath)
}
func (this *dbusPropertyAudioSinkInputs) GetValue() (interface{} /*[]dbus.ObjectPath*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "com.deepin.daemon.Audio", "SinkInputs").Store(&r)
	if err == nil && r.Signature().String() == "ao" {
		return r.Value().([]dbus.ObjectPath), nil
	}
	return *new([]dbus.ObjectPath), err
}
func (this *dbusPropertyAudioSinkInputs) GetType() reflect.Type {
	return reflect.TypeOf((*[]dbus.ObjectPath)(nil)).Elem()
}

type dbusPropertyAudioCards struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyAudioCards) SetValue(notwritable interface{}) {
	fmt.Println("com.deepin.daemon.Audio.Cards is not writable")
}

func (this *dbusPropertyAudioCards) Get() string {
	v, _ := this.GetValue()
	return v.(string)
}
func (this *dbusPropertyAudioCards) GetValue() (interface{} /*string*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "com.deepin.daemon.Audio", "Cards").Store(&r)
	if err == nil && r.Signature().String() == "s" {
		return r.Value().(string), nil
	}
	return *new(string), err
}
func (this *dbusPropertyAudioCards) GetType() reflect.Type {
	return reflect.TypeOf((*string)(nil)).Elem()
}

type dbusPropertyAudioDefaultSink struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyAudioDefaultSink) SetValue(notwritable interface{}) {
	fmt.Println("com.deepin.daemon.Audio.DefaultSink is not writable")
}

func (this *dbusPropertyAudioDefaultSink) Get() dbus.ObjectPath {
	v, _ := this.GetValue()
	return v.(dbus.ObjectPath)
}
func (this *dbusPropertyAudioDefaultSink) GetValue() (interface{} /*dbus.ObjectPath*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "com.deepin.daemon.Audio", "DefaultSink").Store(&r)
	if err == nil && r.Signature().String() == "o" {
		return r.Value().(dbus.ObjectPath), nil
	}
	return *new(dbus.ObjectPath), err
}
func (this *dbusPropertyAudioDefaultSink) GetType() reflect.Type {
	return reflect.TypeOf((*dbus.ObjectPath)(nil)).Elem()
}

type dbusPropertyAudioDefaultSource struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyAudioDefaultSource) SetValue(notwritable interface{}) {
	fmt.Println("com.deepin.daemon.Audio.DefaultSource is not writable")
}

func (this *dbusPropertyAudioDefaultSource) Get() dbus.ObjectPath {
	v, _ := this.GetValue()
	return v.(dbus.ObjectPath)
}
func (this *dbusPropertyAudioDefaultSource) GetValue() (interface{} /*dbus.ObjectPath*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "com.deepin.daemon.Audio", "DefaultSource").Store(&r)
	if err == nil && r.Signature().String() == "o" {
		return r.Value().(dbus.ObjectPath), nil
	}
	return *new(dbus.ObjectPath), err
}
func (this *dbusPropertyAudioDefaultSource) GetType() reflect.Type {
	return reflect.TypeOf((*dbus.ObjectPath)(nil)).Elem()
}

type dbusPropertyAudioMaxUIVolume struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyAudioMaxUIVolume) SetValue(notwritable interface{}) {
	fmt.Println("com.deepin.daemon.Audio.MaxUIVolume is not writable")
}

func (this *dbusPropertyAudioMaxUIVolume) Get() float64 {
	v, _ := this.GetValue()
	return v.(float64)
}
func (this *dbusPropertyAudioMaxUIVolume) GetValue() (interface{} /*float64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "com.deepin.daemon.Audio", "MaxUIVolume").Store(&r)
	if err == nil && r.Signature().String() == "d" {
		return r.Value().(float64), nil
	}
	return *new(float64), err
}
func (this *dbusPropertyAudioMaxUIVolume) GetType() reflect.Type {
	return reflect.TypeOf((*float64)(nil)).Elem()
}

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

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

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

	obj.SinkInputs = &dbusPropertyAudioSinkInputs{&property.BaseObserver{}, core}
	obj.Cards = &dbusPropertyAudioCards{&property.BaseObserver{}, core}
	obj.DefaultSink = &dbusPropertyAudioDefaultSink{&property.BaseObserver{}, core}
	obj.DefaultSource = &dbusPropertyAudioDefaultSource{&property.BaseObserver{}, core}
	obj.MaxUIVolume = &dbusPropertyAudioMaxUIVolume{&property.BaseObserver{}, core}

	dbusAddMatch("type='signal',path='" + string(path) + "',interface='org.freedesktop.DBus.Properties',sender='" + destName + "',member='PropertiesChanged'")
	dbusAddMatch("type='signal',path='" + string(path) + "',interface='com.deepin.daemon.Audio',sender='" + destName + "',member='PropertiesChanged'")
	sigChan := obj._createSignalChan()
	go func() {
		typeString := reflect.TypeOf("")
		typeKeyValues := reflect.TypeOf(map[string]dbus.Variant{})
		typeArrayValues := reflect.TypeOf([]string{})
		for v := range sigChan {
			if v.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" &&
				len(v.Body) == 3 &&
				reflect.TypeOf(v.Body[0]) == typeString &&
				reflect.TypeOf(v.Body[1]) == typeKeyValues &&
				reflect.TypeOf(v.Body[2]) == typeArrayValues &&
				v.Body[0].(string) == "com.deepin.daemon.Audio" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "SinkInputs" {
						obj.SinkInputs.Notify()

					} else if key == "Cards" {
						obj.Cards.Notify()

					} else if key == "DefaultSink" {
						obj.DefaultSink.Notify()

					} else if key == "DefaultSource" {
						obj.DefaultSource.Notify()

					} else if key == "MaxUIVolume" {
						obj.MaxUIVolume.Notify()
					}
				}
			} else if v.Name == "com.deepin.daemon.Audio.PropertiesChanged" && len(v.Body) == 1 && reflect.TypeOf(v.Body[0]) == typeKeyValues {
				for key, _ := range v.Body[0].(map[string]dbus.Variant) {
					if false {
					} else if key == "SinkInputs" {
						obj.SinkInputs.Notify()

					} else if key == "Cards" {
						obj.Cards.Notify()

					} else if key == "DefaultSink" {
						obj.DefaultSink.Notify()

					} else if key == "DefaultSource" {
						obj.DefaultSource.Notify()

					} else if key == "MaxUIVolume" {
						obj.MaxUIVolume.Notify()
					}
				}
			}
		}
	}()

	runtime.SetFinalizer(obj, func(_obj *Audio) { DestroyAudio(_obj) })
	return obj, nil
}
