// Code generated by sdkgen. DO NOT EDIT.

// nolint
package baremetal

import (
	"context"

	"google.golang.org/grpc"

	baremetal "github.com/yandex-cloud/go-genproto/yandex/cloud/baremetal/v1alpha"
	"github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
)

//revive:disable

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

// BatchCreate implements baremetal.ServerServiceClient
func (c *ServerServiceClient) BatchCreate(ctx context.Context, in *baremetal.BatchCreateServersRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).BatchCreate(ctx, in, opts...)
}

// Create implements baremetal.ServerServiceClient
func (c *ServerServiceClient) Create(ctx context.Context, in *baremetal.CreateServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).Create(ctx, in, opts...)
}

// Get implements baremetal.ServerServiceClient
func (c *ServerServiceClient) Get(ctx context.Context, in *baremetal.GetServerRequest, opts ...grpc.CallOption) (*baremetal.Server, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).Get(ctx, in, opts...)
}

// List implements baremetal.ServerServiceClient
func (c *ServerServiceClient) List(ctx context.Context, in *baremetal.ListServerRequest, opts ...grpc.CallOption) (*baremetal.ListServerResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).List(ctx, in, opts...)
}

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

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *ServerServiceClient
	request *baremetal.ListServerRequest

	items []*baremetal.Server
}

func (c *ServerServiceClient) ServerIterator(ctx context.Context, req *baremetal.ListServerRequest, opts ...grpc.CallOption) *ServerIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &ServerIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *ServerIterator) 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.Servers
	it.request.PageToken = response.NextPageToken
	return len(it.items) > 0
}

func (it *ServerIterator) Take(size int64) ([]*baremetal.Server, 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 []*baremetal.Server

	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 *ServerIterator) TakeAll() ([]*baremetal.Server, error) {
	return it.Take(0)
}

func (it *ServerIterator) Value() *baremetal.Server {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

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

// ListOperations implements baremetal.ServerServiceClient
func (c *ServerServiceClient) ListOperations(ctx context.Context, in *baremetal.ListServerOperationsRequest, opts ...grpc.CallOption) (*baremetal.ListServerOperationsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).ListOperations(ctx, in, opts...)
}

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

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *ServerServiceClient
	request *baremetal.ListServerOperationsRequest

	items []*operation.Operation
}

func (c *ServerServiceClient) ServerOperationsIterator(ctx context.Context, req *baremetal.ListServerOperationsRequest, opts ...grpc.CallOption) *ServerOperationsIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &ServerOperationsIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *ServerOperationsIterator) 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 *ServerOperationsIterator) 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 *ServerOperationsIterator) TakeAll() ([]*operation.Operation, error) {
	return it.Take(0)
}

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

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

// PowerOff implements baremetal.ServerServiceClient
func (c *ServerServiceClient) PowerOff(ctx context.Context, in *baremetal.PowerOffServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).PowerOff(ctx, in, opts...)
}

// PowerOn implements baremetal.ServerServiceClient
func (c *ServerServiceClient) PowerOn(ctx context.Context, in *baremetal.PowerOnServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).PowerOn(ctx, in, opts...)
}

// Reboot implements baremetal.ServerServiceClient
func (c *ServerServiceClient) Reboot(ctx context.Context, in *baremetal.RebootServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).Reboot(ctx, in, opts...)
}

// Reinstall implements baremetal.ServerServiceClient
func (c *ServerServiceClient) Reinstall(ctx context.Context, in *baremetal.ReinstallServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).Reinstall(ctx, in, opts...)
}

// StartProlongation implements baremetal.ServerServiceClient
func (c *ServerServiceClient) StartProlongation(ctx context.Context, in *baremetal.StartProlongationRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).StartProlongation(ctx, in, opts...)
}

// StopProlongation implements baremetal.ServerServiceClient
func (c *ServerServiceClient) StopProlongation(ctx context.Context, in *baremetal.StopProlongationRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).StopProlongation(ctx, in, opts...)
}

// Update implements baremetal.ServerServiceClient
func (c *ServerServiceClient) Update(ctx context.Context, in *baremetal.UpdateServerRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return baremetal.NewServerServiceClient(conn).Update(ctx, in, opts...)
}
