// Code generated by smithy-go-codegen DO NOT EDIT.

package acm

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/acm/types"
	"github.com/aws/smithy-go/middleware"
	"github.com/aws/smithy-go/ptr"
)

// Retrieves a list of ACME accounts registered with the specified ACME endpoint.
// ACME accounts are created when clients use external account binding credentials
// to register.
func (c *Client) ListAcmeAccounts(ctx context.Context, params *ListAcmeAccountsInput, optFns ...func(*Options)) (*ListAcmeAccountsOutput, error) {
	if params == nil {
		params = &ListAcmeAccountsInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "ListAcmeAccounts", params, optFns, c.addOperationListAcmeAccountsMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*ListAcmeAccountsOutput)
	out.ResultMetadata = metadata
	return out, nil
}

type ListAcmeAccountsInput struct {

	// The Amazon Resource Name (ARN) of the ACME endpoint.
	//
	// This member is required.
	AcmeEndpointArn *string

	// The maximum number of results to return.
	MaxResults *int32

	// A token for pagination.
	NextToken *string

	noSmithyDocumentSerde
}

func (in *ListAcmeAccountsInput) bindEndpointParams(p *EndpointParameters) {

	p.ServiceType = ptr.String("ACM-ACME")
}

type ListAcmeAccountsOutput struct {

	// The list of ACME accounts.
	AcmeAccounts []types.AcmeAccountSummary

	// A token for pagination.
	NextToken *string

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (c *Client) addOperationListAcmeAccountsMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsAwsjson11_serializeOpListAcmeAccounts{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpListAcmeAccounts{}, middleware.After)
	if err != nil {
		return err
	}

	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack, options); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addOpListAcmeAccountsValidationMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

// ListAcmeAccountsPaginatorOptions is the paginator options for ListAcmeAccounts
type ListAcmeAccountsPaginatorOptions struct {
	// The maximum number of results to return.
	Limit int32

	// Set to true if pagination should stop if the service returns a pagination token
	// that matches the most recent token provided to the service.
	StopOnDuplicateToken bool
}

// ListAcmeAccountsPaginator is a paginator for ListAcmeAccounts
type ListAcmeAccountsPaginator struct {
	options   ListAcmeAccountsPaginatorOptions
	client    ListAcmeAccountsAPIClient
	params    *ListAcmeAccountsInput
	nextToken *string
	firstPage bool
}

// NewListAcmeAccountsPaginator returns a new ListAcmeAccountsPaginator
func NewListAcmeAccountsPaginator(client ListAcmeAccountsAPIClient, params *ListAcmeAccountsInput, optFns ...func(*ListAcmeAccountsPaginatorOptions)) *ListAcmeAccountsPaginator {
	if params == nil {
		params = &ListAcmeAccountsInput{}
	}

	options := ListAcmeAccountsPaginatorOptions{}
	if params.MaxResults != nil {
		options.Limit = *params.MaxResults
	}

	for _, fn := range optFns {
		fn(&options)
	}

	return &ListAcmeAccountsPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.NextToken,
	}
}

// HasMorePages returns a boolean indicating whether more pages are available
func (p *ListAcmeAccountsPaginator) HasMorePages() bool {
	return p.firstPage || (p.nextToken != nil && len(*p.nextToken) != 0)
}

// NextPage retrieves the next ListAcmeAccounts page.
func (p *ListAcmeAccountsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListAcmeAccountsOutput, error) {
	if !p.HasMorePages() {
		return nil, fmt.Errorf("no more pages available")
	}

	params := *p.params
	params.NextToken = p.nextToken

	var limit *int32
	if p.options.Limit > 0 {
		limit = &p.options.Limit
	}
	params.MaxResults = limit

	optFns = append([]func(*Options){
		addIsPaginatorUserAgent,
	}, optFns...)
	result, err := p.client.ListAcmeAccounts(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.nextToken = result.NextToken

	if p.options.StopOnDuplicateToken &&
		prevToken != nil &&
		p.nextToken != nil &&
		*prevToken == *p.nextToken {
		p.nextToken = nil
	}

	return result, nil
}

// ListAcmeAccountsAPIClient is a client that implements the ListAcmeAccounts
// operation.
type ListAcmeAccountsAPIClient interface {
	ListAcmeAccounts(context.Context, *ListAcmeAccountsInput, ...func(*Options)) (*ListAcmeAccountsOutput, error)
}

var _ ListAcmeAccountsAPIClient = (*Client)(nil)
