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

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

	Collections *dbusPropertyServiceCollections
}

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

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

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

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

	obj.Collections.Reset()
}

func (obj *Service) OpenSession(algorithm string, input dbus.Variant) (output dbus.Variant, result dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.OpenSession", 0, algorithm, input).Store(&output, &result)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) CreateCollection(properties map[string]dbus.Variant, alias string) (collection dbus.ObjectPath, prompt dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.CreateCollection", 0, properties, alias).Store(&collection, &prompt)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

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

func (obj *Service) Unlock(objects []dbus.ObjectPath) (unlocked []dbus.ObjectPath, prompt dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.Unlock", 0, objects).Store(&unlocked, &prompt)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) Lock(objects []dbus.ObjectPath) (locked []dbus.ObjectPath, Prompt dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.Lock", 0, objects).Store(&locked, &Prompt)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) LockService() (_err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.LockService", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

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

func (obj *Service) GetSecrets(items []dbus.ObjectPath, session dbus.ObjectPath) (secrets map[dbus.ObjectPath][]interface{}, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.GetSecrets", 0, items, session).Store(&secrets)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) ReadAlias(name string) (collection dbus.ObjectPath, _err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.ReadAlias", 0, name).Store(&collection)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) SetAlias(name string, collection dbus.ObjectPath) (_err error) {
	_err = obj.core.Call("org.freedesktop.Secret.Service.SetAlias", 0, name, collection).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Service) ConnectCollectionCreated(callback func(collection dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Service.CollectionCreated" || 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 *Service) ConnectCollectionDeleted(callback func(collection dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Service.CollectionDeleted" || 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 *Service) ConnectCollectionChanged(callback func(collection dbus.ObjectPath)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.Secret.Service.CollectionChanged" || 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 dbusPropertyServiceCollections struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyServiceCollections) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.Secret.Service.Collections is not writable")
}

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

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

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

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

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

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

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

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

	runtime.SetFinalizer(obj, func(_obj *Service) { DestroyService(_obj) })
	return obj, nil
}
