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

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

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

	Items    *dbusPropertyCollectionItems
	Label    *dbusPropertyCollectionLabel
	Locked   *dbusPropertyCollectionLocked
	Created  *dbusPropertyCollectionCreated
	Modified *dbusPropertyCollectionModified
}

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

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemCreated'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemDeleted'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemChanged'")

	obj.Items.Reset()
	obj.Label.Reset()
	obj.Locked.Reset()
	obj.Created.Reset()
	obj.Modified.Reset()
}

func (obj *Collection) Delete() (prompt dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Collection.Delete", 0).Store(&prompt)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Collection) SearchItems(attributes map[string]string) (results []dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&results)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Collection) CreateItem(properties map[string]dbus.Variant, secret interface{}, replace bool) (item dbus.ObjectPath, prompt dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&item, &prompt)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Collection) ConnectItemCreated(callback func(item dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Collection.ItemCreated" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*dbus.ObjectPath)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(dbus.ObjectPath))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func (obj *Collection) ConnectItemDeleted(callback func(item dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Collection.ItemDeleted" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*dbus.ObjectPath)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(dbus.ObjectPath))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func (obj *Collection) ConnectItemChanged(callback func(item dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Collection.ItemChanged" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*dbus.ObjectPath)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(dbus.ObjectPath))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

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

func (this *dbusPropertyCollectionItems) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.Secret.Collection.Items is not writable")
}

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

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

func (this *dbusPropertyCollectionLabel) SetValue(v interface{} /*string*/) {
	if reflect.TypeOf(v) == reflect.TypeOf((*string)(nil)).Elem() {
		this.core.Call("org.freedesktop.DBus.Properties.Set", 0, "org.freedesktop.Secret.Collection", "Label", dbus.MakeVariant(v))
	} else {
		fmt.Println("The property Label of org.freedesktop.Secret.Collection is an string but Set with an ", reflect.TypeOf(v))
	}
}
func (this *dbusPropertyCollectionLabel) Set(v string) {
	this.SetValue(v)
}

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

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

func (this *dbusPropertyCollectionLocked) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.Secret.Collection.Locked is not writable")
}

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

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

func (this *dbusPropertyCollectionCreated) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.Secret.Collection.Created is not writable")
}

func (this *dbusPropertyCollectionCreated) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyCollectionCreated) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.Secret.Collection", "Created").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyCollectionCreated) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

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

func (this *dbusPropertyCollectionModified) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.Secret.Collection.Modified is not writable")
}

func (this *dbusPropertyCollectionModified) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyCollectionModified) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.Secret.Collection", "Modified").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyCollectionModified) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

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

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

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

	obj.Items = &dbusPropertyCollectionItems{&property.BaseObserver{}, core}
	obj.Label = &dbusPropertyCollectionLabel{&property.BaseObserver{}, core}
	obj.Locked = &dbusPropertyCollectionLocked{&property.BaseObserver{}, core}
	obj.Created = &dbusPropertyCollectionCreated{&property.BaseObserver{}, core}
	obj.Modified = &dbusPropertyCollectionModified{&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.Secret.Collection',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.Secret.Collection" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "Items" {
						obj.Items.Notify()

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

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

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

					} else if key == "Modified" {
						obj.Modified.Notify()
					}
				}
			} else if v.Name == "org.freedesktop.Secret.Collection.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 == "Items" {
						obj.Items.Notify()

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

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

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

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

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemCreated'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemDeleted'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.Secret.Collection',sender='" + obj.DestName + "',member='ItemChanged'")

	runtime.SetFinalizer(obj, func(_obj *Collection) { DestroyCollection(_obj) })
	return obj, nil
}
