// 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) Go Code Generator. DO NOT EDIT.

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/containerservice/armcontainerservice/v9"
	"net/http"
	"net/url"
	"regexp"
	"slices"
	"strconv"
)

// VMSKUsServer is a fake server for instances of the armcontainerservice.VMSKUsClient type.
type VMSKUsServer struct {
	// NewListPager is the fake for method VMSKUsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(location string, options *armcontainerservice.VMSKUsClientListOptions) (resp azfake.PagerResponder[armcontainerservice.VMSKUsClientListResponse])
}

// NewVMSKUsServerTransport creates a new instance of VMSKUsServerTransport with the provided implementation.
// The returned VMSKUsServerTransport instance is connected to an instance of armcontainerservice.VMSKUsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewVMSKUsServerTransport(srv *VMSKUsServer) *VMSKUsServerTransport {
	return &VMSKUsServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armcontainerservice.VMSKUsClientListResponse]](),
	}
}

// VMSKUsServerTransport connects instances of armcontainerservice.VMSKUsClient to instances of VMSKUsServer.
// Don't use this type directly, use NewVMSKUsServerTransport instead.
type VMSKUsServerTransport struct {
	srv          *VMSKUsServer
	newListPager *tracker[azfake.PagerResponder[armcontainerservice.VMSKUsClientListResponse]]
}

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

func (v *VMSKUsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if vmskUsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = vmskUsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "VMSKUsClient.NewListPager":
				res.resp, res.err = v.dispatchNewListPager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

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

func (v *VMSKUsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if v.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := v.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ContainerService/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/vmSkus`
		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)
		}
		qp := req.URL.Query()
		locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
		if err != nil {
			return nil, err
		}
		includeExtendedLocationsParam, err := parseOptional(qp.Get("includeExtendedLocations"), strconv.ParseBool)
		if err != nil {
			return nil, err
		}
		var options *armcontainerservice.VMSKUsClientListOptions
		if includeExtendedLocationsParam != nil {
			options = &armcontainerservice.VMSKUsClientListOptions{
				IncludeExtendedLocations: includeExtendedLocationsParam,
			}
		}
		resp := v.srv.NewListPager(locationParam, options)
		newListPager = &resp
		v.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armcontainerservice.VMSKUsClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		v.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		v.newListPager.remove(req)
	}
	return resp, nil
}

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