// Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.9, generator: @autorest/go@4.0.0-preview.74)
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// Code generated by @autorest/go. DO NOT EDIT.

package fake

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"regexp"

	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/ARO-HCP/test/sdk/v20240610preview/resourcemanager/redhatopenshifthcp/armredhatopenshifthcp"
)

// HcpOpenShiftVersionsServer is a fake server for instances of the armredhatopenshifthcp.HcpOpenShiftVersionsClient type.
type HcpOpenShiftVersionsServer struct {
	// Get is the fake for method HcpOpenShiftVersionsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, location string, hcpOpenShiftVersionName string, options *armredhatopenshifthcp.HcpOpenShiftVersionsClientGetOptions) (resp azfake.Responder[armredhatopenshifthcp.HcpOpenShiftVersionsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method HcpOpenShiftVersionsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(location string, options *armredhatopenshifthcp.HcpOpenShiftVersionsClientListOptions) (resp azfake.PagerResponder[armredhatopenshifthcp.HcpOpenShiftVersionsClientListResponse])
}

// NewHcpOpenShiftVersionsServerTransport creates a new instance of HcpOpenShiftVersionsServerTransport with the provided implementation.
// The returned HcpOpenShiftVersionsServerTransport instance is connected to an instance of armredhatopenshifthcp.HcpOpenShiftVersionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewHcpOpenShiftVersionsServerTransport(srv *HcpOpenShiftVersionsServer) *HcpOpenShiftVersionsServerTransport {
	return &HcpOpenShiftVersionsServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armredhatopenshifthcp.HcpOpenShiftVersionsClientListResponse]](),
	}
}

// HcpOpenShiftVersionsServerTransport connects instances of armredhatopenshifthcp.HcpOpenShiftVersionsClient to instances of HcpOpenShiftVersionsServer.
// Don't use this type directly, use NewHcpOpenShiftVersionsServerTransport instead.
type HcpOpenShiftVersionsServerTransport struct {
	srv          *HcpOpenShiftVersionsServer
	newListPager *tracker[azfake.PagerResponder[armredhatopenshifthcp.HcpOpenShiftVersionsClientListResponse]]
}

// Do implements the policy.Transporter interface for HcpOpenShiftVersionsServerTransport.
func (h *HcpOpenShiftVersionsServerTransport) 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 h.dispatchToMethodFake(req, method)
}

func (h *HcpOpenShiftVersionsServerTransport) 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 hcpOpenShiftVersionsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = hcpOpenShiftVersionsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "HcpOpenShiftVersionsClient.Get":
				res.resp, res.err = h.dispatchGet(req)
			case "HcpOpenShiftVersionsClient.NewListPager":
				res.resp, res.err = h.dispatchNewListPager(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 (h *HcpOpenShiftVersionsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if h.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RedHatOpenShift/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/hcpOpenShiftVersions/(?P<hcpOpenShiftVersionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
	if err != nil {
		return nil, err
	}
	hcpOpenShiftVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("hcpOpenShiftVersionName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := h.srv.Get(req.Context(), locationParam, hcpOpenShiftVersionNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).HcpOpenShiftVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (h *HcpOpenShiftVersionsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if h.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := h.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RedHatOpenShift/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/hcpOpenShiftVersions`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 3 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
		if err != nil {
			return nil, err
		}
		resp := h.srv.NewListPager(locationParam, nil)
		newListPager = &resp
		h.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armredhatopenshifthcp.HcpOpenShiftVersionsClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		h.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		h.newListPager.remove(req)
	}
	return resp, nil
}

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