// Code generated by sdkgen. DO NOT EDIT.

// nolint
package clouddesktop

import (
	"context"

	"google.golang.org/grpc"

	clouddesktop "github.com/yandex-cloud/go-genproto/yandex/cloud/clouddesktop/v1"
	"github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
)

//revive:disable

// DesktopServiceClient is a clouddesktop.DesktopServiceClient with
// lazy GRPC connection initialization.
type DesktopServiceClient struct {
	getConn func(ctx context.Context) (*grpc.ClientConn, error)
}

// Create implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Create(ctx context.Context, in *clouddesktop.CreateDesktopRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Create(ctx, in, opts...)
}

// Delete implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Delete(ctx context.Context, in *clouddesktop.DeleteDesktopRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Delete(ctx, in, opts...)
}

// Get implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Get(ctx context.Context, in *clouddesktop.GetDesktopRequest, opts ...grpc.CallOption) (*clouddesktop.Desktop, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Get(ctx, in, opts...)
}

// GetRdpFile implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) GetRdpFile(ctx context.Context, in *clouddesktop.GetRdpFileRequest, opts ...grpc.CallOption) (*clouddesktop.RdpFileResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).GetRdpFile(ctx, in, opts...)
}

// List implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) List(ctx context.Context, in *clouddesktop.ListDesktopsRequest, opts ...grpc.CallOption) (*clouddesktop.ListDesktopsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).List(ctx, in, opts...)
}

type DesktopIterator struct {
	ctx  context.Context
	opts []grpc.CallOption

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *DesktopServiceClient
	request *clouddesktop.ListDesktopsRequest

	items []*clouddesktop.Desktop
}

func (c *DesktopServiceClient) DesktopIterator(ctx context.Context, req *clouddesktop.ListDesktopsRequest, opts ...grpc.CallOption) *DesktopIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &DesktopIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *DesktopIterator) Next() bool {
	if it.err != nil {
		return false
	}
	if len(it.items) > 1 {
		it.items[0] = nil
		it.items = it.items[1:]
		return true
	}
	it.items = nil // consume last item, if any

	if it.started && it.request.PageToken == "" {
		return false
	}
	it.started = true

	if it.requestedSize == 0 || it.requestedSize > it.pageSize {
		it.request.PageSize = it.pageSize
	} else {
		it.request.PageSize = it.requestedSize
	}

	response, err := it.client.List(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

	it.items = response.Desktops
	it.request.PageToken = response.NextPageToken
	return len(it.items) > 0
}

func (it *DesktopIterator) Take(size int64) ([]*clouddesktop.Desktop, error) {
	if it.err != nil {
		return nil, it.err
	}

	if size == 0 {
		size = 1 << 32 // something insanely large
	}
	it.requestedSize = size
	defer func() {
		// reset iterator for future calls.
		it.requestedSize = 0
	}()

	var result []*clouddesktop.Desktop

	for it.requestedSize > 0 && it.Next() {
		it.requestedSize--
		result = append(result, it.Value())
	}

	if it.err != nil {
		return nil, it.err
	}

	return result, nil
}

func (it *DesktopIterator) TakeAll() ([]*clouddesktop.Desktop, error) {
	return it.Take(0)
}

func (it *DesktopIterator) Value() *clouddesktop.Desktop {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

func (it *DesktopIterator) Error() error {
	return it.err
}

// ListOperations implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) ListOperations(ctx context.Context, in *clouddesktop.ListDesktopOperationsRequest, opts ...grpc.CallOption) (*clouddesktop.ListDesktopOperationsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).ListOperations(ctx, in, opts...)
}

type DesktopOperationsIterator struct {
	ctx  context.Context
	opts []grpc.CallOption

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *DesktopServiceClient
	request *clouddesktop.ListDesktopOperationsRequest

	items []*operation.Operation
}

func (c *DesktopServiceClient) DesktopOperationsIterator(ctx context.Context, req *clouddesktop.ListDesktopOperationsRequest, opts ...grpc.CallOption) *DesktopOperationsIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &DesktopOperationsIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *DesktopOperationsIterator) Next() bool {
	if it.err != nil {
		return false
	}
	if len(it.items) > 1 {
		it.items[0] = nil
		it.items = it.items[1:]
		return true
	}
	it.items = nil // consume last item, if any

	if it.started && it.request.PageToken == "" {
		return false
	}
	it.started = true

	if it.requestedSize == 0 || it.requestedSize > it.pageSize {
		it.request.PageSize = it.pageSize
	} else {
		it.request.PageSize = it.requestedSize
	}

	response, err := it.client.ListOperations(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

	it.items = response.Operations
	it.request.PageToken = response.NextPageToken
	return len(it.items) > 0
}

func (it *DesktopOperationsIterator) Take(size int64) ([]*operation.Operation, error) {
	if it.err != nil {
		return nil, it.err
	}

	if size == 0 {
		size = 1 << 32 // something insanely large
	}
	it.requestedSize = size
	defer func() {
		// reset iterator for future calls.
		it.requestedSize = 0
	}()

	var result []*operation.Operation

	for it.requestedSize > 0 && it.Next() {
		it.requestedSize--
		result = append(result, it.Value())
	}

	if it.err != nil {
		return nil, it.err
	}

	return result, nil
}

func (it *DesktopOperationsIterator) TakeAll() ([]*operation.Operation, error) {
	return it.Take(0)
}

func (it *DesktopOperationsIterator) Value() *operation.Operation {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

func (it *DesktopOperationsIterator) Error() error {
	return it.err
}

// ResetPassword implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) ResetPassword(ctx context.Context, in *clouddesktop.ResetPasswordRequest, opts ...grpc.CallOption) (*clouddesktop.ResetPasswordResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).ResetPassword(ctx, in, opts...)
}

// Restart implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Restart(ctx context.Context, in *clouddesktop.RestartRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Restart(ctx, in, opts...)
}

// Start implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Start(ctx context.Context, in *clouddesktop.StartRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Start(ctx, in, opts...)
}

// Stop implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Stop(ctx context.Context, in *clouddesktop.StopRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Stop(ctx, in, opts...)
}

// Update implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) Update(ctx context.Context, in *clouddesktop.UpdateRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).Update(ctx, in, opts...)
}

// UpdateProperties implements clouddesktop.DesktopServiceClient
func (c *DesktopServiceClient) UpdateProperties(ctx context.Context, in *clouddesktop.UpdatePropertiesRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return clouddesktop.NewDesktopServiceClient(conn).UpdateProperties(ctx, in, opts...)
}
