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

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

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

	Features                        *dbusPropertyModemOmaFeatures
	PendingNetworkInitiatedSessions *dbusPropertyModemOmaPendingNetworkInitiatedSessions
	SessionType                     *dbusPropertyModemOmaSessionType
	SessionState                    *dbusPropertyModemOmaSessionState
}

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

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.ModemManager1.Modem.Oma',sender='" + obj.DestName + "',member='SessionStateChanged'")

	obj.Features.Reset()
	obj.PendingNetworkInitiatedSessions.Reset()
	obj.SessionType.Reset()
	obj.SessionState.Reset()
}

func (obj *ModemOma) Setup(features uint32) (_err error) {
	_err = obj.core.Call("org.freedesktop.ModemManager1.Modem.Oma.Setup", 0, features).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *ModemOma) StartClientInitiatedSession(session_type uint32) (_err error) {
	_err = obj.core.Call("org.freedesktop.ModemManager1.Modem.Oma.StartClientInitiatedSession", 0, session_type).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *ModemOma) AcceptNetworkInitiatedSession(session_id uint32, accept bool) (_err error) {
	_err = obj.core.Call("org.freedesktop.ModemManager1.Modem.Oma.AcceptNetworkInitiatedSession", 0, session_id, accept).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *ModemOma) CancelSession() (_err error) {
	_err = obj.core.Call("org.freedesktop.ModemManager1.Modem.Oma.CancelSession", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *ModemOma) ConnectSessionStateChanged(callback func(old_session_state int32, new_session_state int32, session_state_failed_reason uint32)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.ModemManager1.Modem.Oma.SessionStateChanged" || 3 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*int32)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[1]) != reflect.TypeOf((*int32)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[2]) != reflect.TypeOf((*uint32)(nil)).Elem() {
				continue
			}

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

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

func (this *dbusPropertyModemOmaFeatures) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.ModemManager1.Modem.Oma.Features is not writable")
}

func (this *dbusPropertyModemOmaFeatures) Get() uint32 {
	v, _ := this.GetValue()
	return v.(uint32)
}
func (this *dbusPropertyModemOmaFeatures) GetValue() (interface{} /*uint32*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.ModemManager1.Modem.Oma", "Features").Store(&r)
	if err == nil && r.Signature().String() == "u" {
		return r.Value().(uint32), nil
	}
	return *new(uint32), err
}
func (this *dbusPropertyModemOmaFeatures) GetType() reflect.Type {
	return reflect.TypeOf((*uint32)(nil)).Elem()
}

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

func (this *dbusPropertyModemOmaPendingNetworkInitiatedSessions) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.ModemManager1.Modem.Oma.PendingNetworkInitiatedSessions is not writable")
}

func (this *dbusPropertyModemOmaPendingNetworkInitiatedSessions) Get() [][]interface{} {
	v, _ := this.GetValue()
	return v.([][]interface{})
}
func (this *dbusPropertyModemOmaPendingNetworkInitiatedSessions) GetValue() (interface{} /*[][]interface {}*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.ModemManager1.Modem.Oma", "PendingNetworkInitiatedSessions").Store(&r)
	if err == nil && r.Signature().String() == "a(uu)" {
		return r.Value().([][]interface{}), nil
	}
	return *new([][]interface{}), err
}
func (this *dbusPropertyModemOmaPendingNetworkInitiatedSessions) GetType() reflect.Type {
	return reflect.TypeOf((*[][]interface{})(nil)).Elem()
}

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

func (this *dbusPropertyModemOmaSessionType) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.ModemManager1.Modem.Oma.SessionType is not writable")
}

func (this *dbusPropertyModemOmaSessionType) Get() uint32 {
	v, _ := this.GetValue()
	return v.(uint32)
}
func (this *dbusPropertyModemOmaSessionType) GetValue() (interface{} /*uint32*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.ModemManager1.Modem.Oma", "SessionType").Store(&r)
	if err == nil && r.Signature().String() == "u" {
		return r.Value().(uint32), nil
	}
	return *new(uint32), err
}
func (this *dbusPropertyModemOmaSessionType) GetType() reflect.Type {
	return reflect.TypeOf((*uint32)(nil)).Elem()
}

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

func (this *dbusPropertyModemOmaSessionState) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.ModemManager1.Modem.Oma.SessionState is not writable")
}

func (this *dbusPropertyModemOmaSessionState) Get() int32 {
	v, _ := this.GetValue()
	return v.(int32)
}
func (this *dbusPropertyModemOmaSessionState) GetValue() (interface{} /*int32*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.ModemManager1.Modem.Oma", "SessionState").Store(&r)
	if err == nil && r.Signature().String() == "i" {
		return r.Value().(int32), nil
	}
	return *new(int32), err
}
func (this *dbusPropertyModemOmaSessionState) GetType() reflect.Type {
	return reflect.TypeOf((*int32)(nil)).Elem()
}

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

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

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

	obj.Features = &dbusPropertyModemOmaFeatures{&property.BaseObserver{}, core}
	obj.PendingNetworkInitiatedSessions = &dbusPropertyModemOmaPendingNetworkInitiatedSessions{&property.BaseObserver{}, core}
	obj.SessionType = &dbusPropertyModemOmaSessionType{&property.BaseObserver{}, core}
	obj.SessionState = &dbusPropertyModemOmaSessionState{&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='org.freedesktop.ModemManager1.Modem.Oma',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) == "org.freedesktop.ModemManager1.Modem.Oma" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "Features" {
						obj.Features.Notify()

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

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

					} else if key == "SessionState" {
						obj.SessionState.Notify()
					}
				}
			} else if v.Name == "org.freedesktop.ModemManager1.Modem.Oma.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 == "Features" {
						obj.Features.Notify()

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

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

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

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.ModemManager1.Modem.Oma',sender='" + obj.DestName + "',member='SessionStateChanged'")

	runtime.SetFinalizer(obj, func(_obj *ModemOma) { DestroyModemOma(_obj) })
	return obj, nil
}
