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

package devopsagent

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/devopsagent/types"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

// Lists goals in the specified agent space with optional filtering
func (c *Client) ListGoals(ctx context.Context, params *ListGoalsInput, optFns ...func(*Options)) (*ListGoalsOutput, error) {
	if params == nil {
		params = &ListGoalsInput{}
	}

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

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

// Request structure for listing goals
type ListGoalsInput struct {

	// The unique identifier for the agent space
	//
	// This member is required.
	AgentSpaceId *string

	// Filter goals by goal type
	GoalType types.GoalType

	// Maximum number of goals to return
	Limit *int32

	// Pagination token for the next set of results
	NextToken *string

	// Filter goals by goal status
	Status types.GoalStatus

	noSmithyDocumentSerde
}

// Response structure containing the list of goals
type ListGoalsOutput struct {

	// List of goals matching the criteria
	//
	// This member is required.
	Goals []types.Goal

	// Pagination token for the next set of results
	NextToken *string

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

	noSmithyDocumentSerde
}

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

	if err = addlegacyEndpointContextSetter(stack, options); 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); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addEndpointPrefix_opListGoalsMiddleware(stack); err != nil {
		return err
	}
	if err = addOpListGoalsValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "ListGoals"), middleware.Before); 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
}

// ListGoalsPaginatorOptions is the paginator options for ListGoals
type ListGoalsPaginatorOptions struct {
	// Maximum number of goals 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
}

// ListGoalsPaginator is a paginator for ListGoals
type ListGoalsPaginator struct {
	options   ListGoalsPaginatorOptions
	client    ListGoalsAPIClient
	params    *ListGoalsInput
	nextToken *string
	firstPage bool
}

// NewListGoalsPaginator returns a new ListGoalsPaginator
func NewListGoalsPaginator(client ListGoalsAPIClient, params *ListGoalsInput, optFns ...func(*ListGoalsPaginatorOptions)) *ListGoalsPaginator {
	if params == nil {
		params = &ListGoalsInput{}
	}

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

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

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

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

// NextPage retrieves the next ListGoals page.
func (p *ListGoalsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListGoalsOutput, 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.Limit = limit

	optFns = append([]func(*Options){
		addIsPaginatorUserAgent,
	}, optFns...)
	result, err := p.client.ListGoals(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
}

type endpointPrefix_opListGoalsMiddleware struct {
}

func (*endpointPrefix_opListGoalsMiddleware) ID() string {
	return "EndpointHostPrefix"
}

func (m *endpointPrefix_opListGoalsMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (
	out middleware.FinalizeOutput, metadata middleware.Metadata, err error,
) {
	if smithyhttp.GetHostnameImmutable(ctx) || smithyhttp.IsEndpointHostPrefixDisabled(ctx) {
		return next.HandleFinalize(ctx, in)
	}

	req, ok := in.Request.(*smithyhttp.Request)
	if !ok {
		return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
	}

	req.URL.Host = "dp." + req.URL.Host

	return next.HandleFinalize(ctx, in)
}
func addEndpointPrefix_opListGoalsMiddleware(stack *middleware.Stack) error {
	return stack.Finalize.Insert(&endpointPrefix_opListGoalsMiddleware{}, "ResolveEndpointV2", middleware.After)
}

// ListGoalsAPIClient is a client that implements the ListGoals operation.
type ListGoalsAPIClient interface {
	ListGoals(context.Context, *ListGoalsInput, ...func(*Options)) (*ListGoalsOutput, error)
}

var _ ListGoalsAPIClient = (*Client)(nil)
