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

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

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

	MountPoints *dbusPropertyFilesystemMountPoints
}

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

	obj.MountPoints.Reset()
}

func (obj *Filesystem) SetLabel(label string, options map[string]dbus.Variant) (_err error) {
	_err = obj.core.Call("org.freedesktop.UDisks2.Filesystem.SetLabel", 0, label, options).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Filesystem) Mount(options map[string]dbus.Variant) (mount_path string, _err error) {
	_err = obj.core.Call("org.freedesktop.UDisks2.Filesystem.Mount", 0, options).Store(&mount_path)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Filesystem) Unmount(options map[string]dbus.Variant) (_err error) {
	_err = obj.core.Call("org.freedesktop.UDisks2.Filesystem.Unmount", 0, options).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

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

func (this *dbusPropertyFilesystemMountPoints) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Filesystem.MountPoints is not writable")
}

func (this *dbusPropertyFilesystemMountPoints) Get() [][]uint8 {
	v, _ := this.GetValue()
	return v.([][]uint8)
}
func (this *dbusPropertyFilesystemMountPoints) GetValue() (interface{} /*[][]uint8*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Filesystem", "MountPoints").Store(&r)
	if err == nil && r.Signature().String() == "aay" {
		return r.Value().([][]uint8), nil
	}
	return *new([][]uint8), err
}
func (this *dbusPropertyFilesystemMountPoints) GetType() reflect.Type {
	return reflect.TypeOf((*[][]uint8)(nil)).Elem()
}

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

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

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

	obj.MountPoints = &dbusPropertyFilesystemMountPoints{&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.UDisks2.Filesystem',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.UDisks2.Filesystem" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "MountPoints" {
						obj.MountPoints.Notify()
					}
				}
			} else if v.Name == "org.freedesktop.UDisks2.Filesystem.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 == "MountPoints" {
						obj.MountPoints.Notify()
					}
				}
			}
		}
	}()

	runtime.SetFinalizer(obj, func(_obj *Filesystem) { DestroyFilesystem(_obj) })
	return obj, nil
}
