//go:build go1.18
// +build go1.18

// 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 (
	"context"
	"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/azurestackhci/armazurestackhci/v2"
	"net/http"
	"net/url"
	"regexp"
)

// OffersServer is a fake server for instances of the armazurestackhci.OffersClient type.
type OffersServer struct {
	// Get is the fake for method OffersClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, clusterName string, publisherName string, offerName string, options *armazurestackhci.OffersClientGetOptions) (resp azfake.Responder[armazurestackhci.OffersClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByClusterPager is the fake for method OffersClient.NewListByClusterPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByClusterPager func(resourceGroupName string, clusterName string, options *armazurestackhci.OffersClientListByClusterOptions) (resp azfake.PagerResponder[armazurestackhci.OffersClientListByClusterResponse])

	// NewListByPublisherPager is the fake for method OffersClient.NewListByPublisherPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByPublisherPager func(resourceGroupName string, clusterName string, publisherName string, options *armazurestackhci.OffersClientListByPublisherOptions) (resp azfake.PagerResponder[armazurestackhci.OffersClientListByPublisherResponse])
}

// NewOffersServerTransport creates a new instance of OffersServerTransport with the provided implementation.
// The returned OffersServerTransport instance is connected to an instance of armazurestackhci.OffersClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewOffersServerTransport(srv *OffersServer) *OffersServerTransport {
	return &OffersServerTransport{
		srv:                     srv,
		newListByClusterPager:   newTracker[azfake.PagerResponder[armazurestackhci.OffersClientListByClusterResponse]](),
		newListByPublisherPager: newTracker[azfake.PagerResponder[armazurestackhci.OffersClientListByPublisherResponse]](),
	}
}

// OffersServerTransport connects instances of armazurestackhci.OffersClient to instances of OffersServer.
// Don't use this type directly, use NewOffersServerTransport instead.
type OffersServerTransport struct {
	srv                     *OffersServer
	newListByClusterPager   *tracker[azfake.PagerResponder[armazurestackhci.OffersClientListByClusterResponse]]
	newListByPublisherPager *tracker[azfake.PagerResponder[armazurestackhci.OffersClientListByPublisherResponse]]
}

// Do implements the policy.Transporter interface for OffersServerTransport.
func (o *OffersServerTransport) 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")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "OffersClient.Get":
		resp, err = o.dispatchGet(req)
	case "OffersClient.NewListByClusterPager":
		resp, err = o.dispatchNewListByClusterPager(req)
	case "OffersClient.NewListByPublisherPager":
		resp, err = o.dispatchNewListByPublisherPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (o *OffersServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if o.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/clusters/(?P<clusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/publishers/(?P<publisherName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/offers/(?P<offerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 5 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	qp := req.URL.Query()
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	clusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterName")])
	if err != nil {
		return nil, err
	}
	publisherNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("publisherName")])
	if err != nil {
		return nil, err
	}
	offerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("offerName")])
	if err != nil {
		return nil, err
	}
	expandUnescaped, err := url.QueryUnescape(qp.Get("$expand"))
	if err != nil {
		return nil, err
	}
	expandParam := getOptional(expandUnescaped)
	var options *armazurestackhci.OffersClientGetOptions
	if expandParam != nil {
		options = &armazurestackhci.OffersClientGetOptions{
			Expand: expandParam,
		}
	}
	respr, errRespr := o.srv.Get(req.Context(), resourceGroupNameParam, clusterNameParam, publisherNameParam, offerNameParam, options)
	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).Offer, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (o *OffersServerTransport) dispatchNewListByClusterPager(req *http.Request) (*http.Response, error) {
	if o.srv.NewListByClusterPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByClusterPager not implemented")}
	}
	newListByClusterPager := o.newListByClusterPager.get(req)
	if newListByClusterPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/clusters/(?P<clusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/offers`
		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)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		clusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterName")])
		if err != nil {
			return nil, err
		}
		expandUnescaped, err := url.QueryUnescape(qp.Get("$expand"))
		if err != nil {
			return nil, err
		}
		expandParam := getOptional(expandUnescaped)
		var options *armazurestackhci.OffersClientListByClusterOptions
		if expandParam != nil {
			options = &armazurestackhci.OffersClientListByClusterOptions{
				Expand: expandParam,
			}
		}
		resp := o.srv.NewListByClusterPager(resourceGroupNameParam, clusterNameParam, options)
		newListByClusterPager = &resp
		o.newListByClusterPager.add(req, newListByClusterPager)
		server.PagerResponderInjectNextLinks(newListByClusterPager, req, func(page *armazurestackhci.OffersClientListByClusterResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByClusterPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		o.newListByClusterPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByClusterPager) {
		o.newListByClusterPager.remove(req)
	}
	return resp, nil
}

func (o *OffersServerTransport) dispatchNewListByPublisherPager(req *http.Request) (*http.Response, error) {
	if o.srv.NewListByPublisherPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByPublisherPager not implemented")}
	}
	newListByPublisherPager := o.newListByPublisherPager.get(req)
	if newListByPublisherPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/clusters/(?P<clusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/publishers/(?P<publisherName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/offers`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 4 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		clusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterName")])
		if err != nil {
			return nil, err
		}
		publisherNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("publisherName")])
		if err != nil {
			return nil, err
		}
		expandUnescaped, err := url.QueryUnescape(qp.Get("$expand"))
		if err != nil {
			return nil, err
		}
		expandParam := getOptional(expandUnescaped)
		var options *armazurestackhci.OffersClientListByPublisherOptions
		if expandParam != nil {
			options = &armazurestackhci.OffersClientListByPublisherOptions{
				Expand: expandParam,
			}
		}
		resp := o.srv.NewListByPublisherPager(resourceGroupNameParam, clusterNameParam, publisherNameParam, options)
		newListByPublisherPager = &resp
		o.newListByPublisherPager.add(req, newListByPublisherPager)
		server.PagerResponderInjectNextLinks(newListByPublisherPager, req, func(page *armazurestackhci.OffersClientListByPublisherResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByPublisherPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		o.newListByPublisherPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByPublisherPager) {
		o.newListByPublisherPager.remove(req)
	}
	return resp, nil
}
