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

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

	Locked     *dbusPropertyItemLocked
	Attributes *dbusPropertyItemAttributes
	Label      *dbusPropertyItemLabel
	Created    *dbusPropertyItemCreated
	Modified   *dbusPropertyItemModified
}

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

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

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

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

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

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

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

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

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

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

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

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

func (this *dbusPropertyItemLabel) 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.Item", "Label", dbus.MakeVariant(v))
	} else {
		fmt.Println("The property Label of org.freedesktop.Secret.Item is an string but Set with an ", reflect.TypeOf(v))
	}
}
func (this *dbusPropertyItemLabel) Set(v string) {
	this.SetValue(v)
}

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

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

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

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

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

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

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

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

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

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

	obj.Locked = &dbusPropertyItemLocked{&property.BaseObserver{}, core}
	obj.Attributes = &dbusPropertyItemAttributes{&property.BaseObserver{}, core}
	obj.Label = &dbusPropertyItemLabel{&property.BaseObserver{}, core}
	obj.Created = &dbusPropertyItemCreated{&property.BaseObserver{}, core}
	obj.Modified = &dbusPropertyItemModified{&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.Item',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.Item" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "Locked" {
						obj.Locked.Notify()

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

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

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

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

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

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

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

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

	runtime.SetFinalizer(obj, func(_obj *Item) { DestroyItem(_obj) })
	return obj, nil
}
