// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package fake

import (
	"errors"
	"fmt"
	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v3"
	"net/http"
	"net/url"
	"regexp"
)

// GatewaySKUsServer is a fake server for instances of the armapimanagement.GatewaySKUsClient type.
type GatewaySKUsServer struct {
	// NewListAvailableSKUsPager is the fake for method GatewaySKUsClient.NewListAvailableSKUsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListAvailableSKUsPager func(resourceGroupName string, gatewayName string, options *armapimanagement.GatewaySKUsClientListAvailableSKUsOptions) (resp azfake.PagerResponder[armapimanagement.GatewaySKUsClientListAvailableSKUsResponse])
}

// NewGatewaySKUsServerTransport creates a new instance of GatewaySKUsServerTransport with the provided implementation.
// The returned GatewaySKUsServerTransport instance is connected to an instance of armapimanagement.GatewaySKUsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewGatewaySKUsServerTransport(srv *GatewaySKUsServer) *GatewaySKUsServerTransport {
	return &GatewaySKUsServerTransport{
		srv:                       srv,
		newListAvailableSKUsPager: newTracker[azfake.PagerResponder[armapimanagement.GatewaySKUsClientListAvailableSKUsResponse]](),
	}
}

// GatewaySKUsServerTransport connects instances of armapimanagement.GatewaySKUsClient to instances of GatewaySKUsServer.
// Don't use this type directly, use NewGatewaySKUsServerTransport instead.
type GatewaySKUsServerTransport struct {
	srv                       *GatewaySKUsServer
	newListAvailableSKUsPager *tracker[azfake.PagerResponder[armapimanagement.GatewaySKUsClientListAvailableSKUsResponse]]
}

// Do implements the policy.Transporter interface for GatewaySKUsServerTransport.
func (g *GatewaySKUsServerTransport) Do(req *http.Request) (*http.Response, error) {
	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
	method, ok := rawMethod.(string)
	if !ok {
		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
	}

	return g.dispatchToMethodFake(req, method)
}

func (g *GatewaySKUsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if gatewaySkUsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = gatewaySkUsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "GatewaySKUsClient.NewListAvailableSKUsPager":
				res.resp, res.err = g.dispatchNewListAvailableSKUsPager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (g *GatewaySKUsServerTransport) dispatchNewListAvailableSKUsPager(req *http.Request) (*http.Response, error) {
	if g.srv.NewListAvailableSKUsPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListAvailableSKUsPager not implemented")}
	}
	newListAvailableSKUsPager := g.newListAvailableSKUsPager.get(req)
	if newListAvailableSKUsPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ApiManagement/gateways/(?P<gatewayName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/skus`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 3 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		gatewayNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("gatewayName")])
		if err != nil {
			return nil, err
		}
		resp := g.srv.NewListAvailableSKUsPager(resourceGroupNameParam, gatewayNameParam, nil)
		newListAvailableSKUsPager = &resp
		g.newListAvailableSKUsPager.add(req, newListAvailableSKUsPager)
		server.PagerResponderInjectNextLinks(newListAvailableSKUsPager, req, func(page *armapimanagement.GatewaySKUsClientListAvailableSKUsResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListAvailableSKUsPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		g.newListAvailableSKUsPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListAvailableSKUsPager) {
		g.newListAvailableSKUsPager.remove(req)
	}
	return resp, nil
}

// set this to conditionally intercept incoming requests to GatewaySKUsServerTransport
var gatewaySkUsServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
