//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 (
	"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/desktopvirtualization/armdesktopvirtualization/v2"
	"net/http"
	"net/url"
	"regexp"
)

// AppAttachPackageInfoServer is a fake server for instances of the armdesktopvirtualization.AppAttachPackageInfoClient type.
type AppAttachPackageInfoServer struct {
	// NewImportPager is the fake for method AppAttachPackageInfoClient.NewImportPager
	// HTTP status codes to indicate success: http.StatusOK
	NewImportPager func(resourceGroupName string, hostPoolName string, importPackageInfoRequest armdesktopvirtualization.ImportPackageInfoRequest, options *armdesktopvirtualization.AppAttachPackageInfoClientImportOptions) (resp azfake.PagerResponder[armdesktopvirtualization.AppAttachPackageInfoClientImportResponse])
}

// NewAppAttachPackageInfoServerTransport creates a new instance of AppAttachPackageInfoServerTransport with the provided implementation.
// The returned AppAttachPackageInfoServerTransport instance is connected to an instance of armdesktopvirtualization.AppAttachPackageInfoClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAppAttachPackageInfoServerTransport(srv *AppAttachPackageInfoServer) *AppAttachPackageInfoServerTransport {
	return &AppAttachPackageInfoServerTransport{
		srv:            srv,
		newImportPager: newTracker[azfake.PagerResponder[armdesktopvirtualization.AppAttachPackageInfoClientImportResponse]](),
	}
}

// AppAttachPackageInfoServerTransport connects instances of armdesktopvirtualization.AppAttachPackageInfoClient to instances of AppAttachPackageInfoServer.
// Don't use this type directly, use NewAppAttachPackageInfoServerTransport instead.
type AppAttachPackageInfoServerTransport struct {
	srv            *AppAttachPackageInfoServer
	newImportPager *tracker[azfake.PagerResponder[armdesktopvirtualization.AppAttachPackageInfoClientImportResponse]]
}

// Do implements the policy.Transporter interface for AppAttachPackageInfoServerTransport.
func (a *AppAttachPackageInfoServerTransport) 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 "AppAttachPackageInfoClient.NewImportPager":
		resp, err = a.dispatchNewImportPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (a *AppAttachPackageInfoServerTransport) dispatchNewImportPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewImportPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewImportPager not implemented")}
	}
	newImportPager := a.newImportPager.get(req)
	if newImportPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DesktopVirtualization/hostPools/(?P<hostPoolName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/importAppAttachPackageInfo`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armdesktopvirtualization.ImportPackageInfoRequest](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		hostPoolNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("hostPoolName")])
		if err != nil {
			return nil, err
		}
		resp := a.srv.NewImportPager(resourceGroupNameParam, hostPoolNameParam, body, nil)
		newImportPager = &resp
		a.newImportPager.add(req, newImportPager)
		server.PagerResponderInjectNextLinks(newImportPager, req, func(page *armdesktopvirtualization.AppAttachPackageInfoClientImportResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newImportPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newImportPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newImportPager) {
		a.newImportPager.remove(req)
	}
	return resp, nil
}
