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

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

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

	HasAccelerometer         *dbusPropertySensorProxyHasAccelerometer
	AccelerometerOrientation *dbusPropertySensorProxyAccelerometerOrientation
	HasAmbientLight          *dbusPropertySensorProxyHasAmbientLight
	LightLevelUnit           *dbusPropertySensorProxyLightLevelUnit
	LightLevel               *dbusPropertySensorProxyLightLevel
}

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

	obj.HasAccelerometer.Reset()
	obj.AccelerometerOrientation.Reset()
	obj.HasAmbientLight.Reset()
	obj.LightLevelUnit.Reset()
	obj.LightLevel.Reset()
}

func (obj *SensorProxy) ClaimAccelerometer() (_err error) {
	_err = obj.core.Call("net.hadess.SensorProxy.ClaimAccelerometer", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *SensorProxy) ReleaseAccelerometer() (_err error) {
	_err = obj.core.Call("net.hadess.SensorProxy.ReleaseAccelerometer", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *SensorProxy) ClaimLight() (_err error) {
	_err = obj.core.Call("net.hadess.SensorProxy.ClaimLight", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *SensorProxy) ReleaseLight() (_err error) {
	_err = obj.core.Call("net.hadess.SensorProxy.ReleaseLight", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

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

func (this *dbusPropertySensorProxyHasAccelerometer) SetValue(notwritable interface{}) {
	fmt.Println("net.hadess.SensorProxy.HasAccelerometer is not writable")
}

func (this *dbusPropertySensorProxyHasAccelerometer) Get() bool {
	v, _ := this.GetValue()
	return v.(bool)
}
func (this *dbusPropertySensorProxyHasAccelerometer) GetValue() (interface{} /*bool*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "net.hadess.SensorProxy", "HasAccelerometer").Store(&r)
	if err == nil && r.Signature().String() == "b" {
		return r.Value().(bool), nil
	}
	return *new(bool), err
}
func (this *dbusPropertySensorProxyHasAccelerometer) GetType() reflect.Type {
	return reflect.TypeOf((*bool)(nil)).Elem()
}

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

func (this *dbusPropertySensorProxyAccelerometerOrientation) SetValue(notwritable interface{}) {
	fmt.Println("net.hadess.SensorProxy.AccelerometerOrientation is not writable")
}

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

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

func (this *dbusPropertySensorProxyHasAmbientLight) SetValue(notwritable interface{}) {
	fmt.Println("net.hadess.SensorProxy.HasAmbientLight is not writable")
}

func (this *dbusPropertySensorProxyHasAmbientLight) Get() bool {
	v, _ := this.GetValue()
	return v.(bool)
}
func (this *dbusPropertySensorProxyHasAmbientLight) GetValue() (interface{} /*bool*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "net.hadess.SensorProxy", "HasAmbientLight").Store(&r)
	if err == nil && r.Signature().String() == "b" {
		return r.Value().(bool), nil
	}
	return *new(bool), err
}
func (this *dbusPropertySensorProxyHasAmbientLight) GetType() reflect.Type {
	return reflect.TypeOf((*bool)(nil)).Elem()
}

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

func (this *dbusPropertySensorProxyLightLevelUnit) SetValue(notwritable interface{}) {
	fmt.Println("net.hadess.SensorProxy.LightLevelUnit is not writable")
}

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

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

func (this *dbusPropertySensorProxyLightLevel) SetValue(notwritable interface{}) {
	fmt.Println("net.hadess.SensorProxy.LightLevel is not writable")
}

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

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

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

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

	obj.HasAccelerometer = &dbusPropertySensorProxyHasAccelerometer{&property.BaseObserver{}, core}
	obj.AccelerometerOrientation = &dbusPropertySensorProxyAccelerometerOrientation{&property.BaseObserver{}, core}
	obj.HasAmbientLight = &dbusPropertySensorProxyHasAmbientLight{&property.BaseObserver{}, core}
	obj.LightLevelUnit = &dbusPropertySensorProxyLightLevelUnit{&property.BaseObserver{}, core}
	obj.LightLevel = &dbusPropertySensorProxyLightLevel{&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='net.hadess.SensorProxy',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) == "net.hadess.SensorProxy" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "HasAccelerometer" {
						obj.HasAccelerometer.Notify()

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

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

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

					} else if key == "LightLevel" {
						obj.LightLevel.Notify()
					}
				}
			} else if v.Name == "net.hadess.SensorProxy.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 == "HasAccelerometer" {
						obj.HasAccelerometer.Notify()

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

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

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

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

	runtime.SetFinalizer(obj, func(_obj *SensorProxy) { DestroySensorProxy(_obj) })
	return obj, nil
}
