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

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

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

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

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceRemoved'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceAdded'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceSwitched'")

}

func (obj *Wm) PerformAction(type_ int32) (_err error) {
	_err = obj.core.Call("com.deepin.wm.PerformAction", 0, type_).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) ToggleDebug() (_err error) {
	_err = obj.core.Call("com.deepin.wm.ToggleDebug", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) CancelPreviewWindow() (_err error) {
	_err = obj.core.Call("com.deepin.wm.CancelPreviewWindow", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) PreviewWindow(xid uint32) (_err error) {
	_err = obj.core.Call("com.deepin.wm.PreviewWindow", 0, xid).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) PresentWindows(xids []uint32) (_err error) {
	_err = obj.core.Call("com.deepin.wm.PresentWindows", 0, xids).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) RequestHideWindows() (_err error) {
	_err = obj.core.Call("com.deepin.wm.RequestHideWindows", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) CancelHideWindows() (_err error) {
	_err = obj.core.Call("com.deepin.wm.CancelHideWindows", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) ChangeCurrentWorkspaceBackground(uri string) (_err error) {
	_err = obj.core.Call("com.deepin.wm.ChangeCurrentWorkspaceBackground", 0, uri).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) SetTransientBackground(uri string) (_err error) {
	_err = obj.core.Call("com.deepin.wm.SetTransientBackground", 0, uri).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) GetCurrentWorkspaceBackground() (result string, _err error) {
	_err = obj.core.Call("com.deepin.wm.GetCurrentWorkspaceBackground", 0).Store(&result)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) EnableZoneDetected(val bool) (_err error) {
	_err = obj.core.Call("com.deepin.wm.EnableZoneDetected", 0, val).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) SwitchApplication(backward bool) (_err error) {
	_err = obj.core.Call("com.deepin.wm.SwitchApplication", 0, backward).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) SwitchToWorkspace(backward bool) (_err error) {
	_err = obj.core.Call("com.deepin.wm.SwitchToWorkspace", 0, backward).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) TileActiveWindow(side uint32) (_err error) {
	_err = obj.core.Call("com.deepin.wm.TileActiveWindow", 0, side).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) BeginToMoveActiveWindow() (_err error) {
	_err = obj.core.Call("com.deepin.wm.BeginToMoveActiveWindow", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Wm) ConnectWorkspaceRemoved(callback func(index int32)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "com.deepin.wm.WorkspaceRemoved" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*int32)(nil)).Elem() {
				continue
			}

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

func (obj *Wm) ConnectWorkspaceAdded(callback func(index int32)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "com.deepin.wm.WorkspaceAdded" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*int32)(nil)).Elem() {
				continue
			}

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

func (obj *Wm) ConnectWorkspaceSwitched(callback func(from int32, to int32)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "com.deepin.wm.WorkspaceSwitched" || 2 != 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
			}

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

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

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

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

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceRemoved'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceAdded'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='com.deepin.wm',sender='" + obj.DestName + "',member='WorkspaceSwitched'")

	runtime.SetFinalizer(obj, func(_obj *Wm) { DestroyWm(_obj) })
	return obj, nil
}
