// Code generated by winrt-go-gen. DO NOT EDIT.

//go:build windows

//nolint:all
package foundation

import (
	"syscall"
	"unsafe"

	"github.com/go-ole/go-ole"
)

const SignatureDeferral string = "rc(Windows.Foundation.Deferral;{d6269732-3b7f-46a7-b40b-4fdca2a2c693})"

type Deferral struct {
	ole.IUnknown
}

func (impl *Deferral) Complete() error {
	itf := impl.MustQueryInterface(ole.NewGUID(GUIDiDeferral))
	defer itf.Release()
	v := (*iDeferral)(unsafe.Pointer(itf))
	return v.Complete()
}

func (impl *Deferral) Close() error {
	itf := impl.MustQueryInterface(ole.NewGUID(GUIDIClosable))
	defer itf.Release()
	v := (*IClosable)(unsafe.Pointer(itf))
	return v.Close()
}

const GUIDiDeferral string = "d6269732-3b7f-46a7-b40b-4fdca2a2c693"
const SignatureiDeferral string = "{d6269732-3b7f-46a7-b40b-4fdca2a2c693}"

type iDeferral struct {
	ole.IInspectable
}

type iDeferralVtbl struct {
	ole.IInspectableVtbl

	Complete uintptr
}

func (v *iDeferral) VTable() *iDeferralVtbl {
	return (*iDeferralVtbl)(unsafe.Pointer(v.RawVTable))
}

func (v *iDeferral) Complete() error {
	hr, _, _ := syscall.SyscallN(
		v.VTable().Complete,
		uintptr(unsafe.Pointer(v)), // this
	)

	if hr != 0 {
		return ole.NewError(hr)
	}

	return nil
}

const GUIDiDeferralFactory string = "65a1ecc5-3fb5-4832-8ca9-f061b281d13a"
const SignatureiDeferralFactory string = "{65a1ecc5-3fb5-4832-8ca9-f061b281d13a}"

type iDeferralFactory struct {
	ole.IInspectable
}

type iDeferralFactoryVtbl struct {
	ole.IInspectableVtbl

	DeferralCreate uintptr
}

func (v *iDeferralFactory) VTable() *iDeferralFactoryVtbl {
	return (*iDeferralFactoryVtbl)(unsafe.Pointer(v.RawVTable))
}

func DeferralCreate(handler *DeferralCompletedHandler) (*Deferral, error) {
	inspectable, err := ole.RoGetActivationFactory("Windows.Foundation.Deferral", ole.NewGUID(GUIDiDeferralFactory))
	if err != nil {
		return nil, err
	}
	v := (*iDeferralFactory)(unsafe.Pointer(inspectable))

	var out *Deferral
	hr, _, _ := syscall.SyscallN(
		v.VTable().DeferralCreate,
		uintptr(unsafe.Pointer(v)),       // this
		uintptr(unsafe.Pointer(handler)), // in DeferralCompletedHandler
		uintptr(unsafe.Pointer(&out)),    // out Deferral
	)

	if hr != 0 {
		return nil, ole.NewError(hr)
	}

	return out, nil
}
