// 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/iotfirmwaredefense/armiotfirmwaredefense/v2"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// PasswordHashesServer is a fake server for instances of the armiotfirmwaredefense.PasswordHashesClient type.
type PasswordHashesServer struct {
	// NewListByFirmwarePager is the fake for method PasswordHashesClient.NewListByFirmwarePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByFirmwarePager func(resourceGroupName string, workspaceName string, firmwareID string, options *armiotfirmwaredefense.PasswordHashesClientListByFirmwareOptions) (resp azfake.PagerResponder[armiotfirmwaredefense.PasswordHashesClientListByFirmwareResponse])
}

// NewPasswordHashesServerTransport creates a new instance of PasswordHashesServerTransport with the provided implementation.
// The returned PasswordHashesServerTransport instance is connected to an instance of armiotfirmwaredefense.PasswordHashesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewPasswordHashesServerTransport(srv *PasswordHashesServer) *PasswordHashesServerTransport {
	return &PasswordHashesServerTransport{
		srv:                    srv,
		newListByFirmwarePager: newTracker[azfake.PagerResponder[armiotfirmwaredefense.PasswordHashesClientListByFirmwareResponse]](),
	}
}

// PasswordHashesServerTransport connects instances of armiotfirmwaredefense.PasswordHashesClient to instances of PasswordHashesServer.
// Don't use this type directly, use NewPasswordHashesServerTransport instead.
type PasswordHashesServerTransport struct {
	srv                    *PasswordHashesServer
	newListByFirmwarePager *tracker[azfake.PagerResponder[armiotfirmwaredefense.PasswordHashesClientListByFirmwareResponse]]
}

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

func (p *PasswordHashesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if passwordHashesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = passwordHashesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "PasswordHashesClient.NewListByFirmwarePager":
				res.resp, res.err = p.dispatchNewListByFirmwarePager(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 (p *PasswordHashesServerTransport) dispatchNewListByFirmwarePager(req *http.Request) (*http.Response, error) {
	if p.srv.NewListByFirmwarePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByFirmwarePager not implemented")}
	}
	newListByFirmwarePager := p.newListByFirmwarePager.get(req)
	if newListByFirmwarePager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.IoTFirmwareDefense/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/firmwares/(?P<firmwareId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/passwordHashes`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 5 {
			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
		}
		workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
		if err != nil {
			return nil, err
		}
		firmwareIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("firmwareId")])
		if err != nil {
			return nil, err
		}
		resp := p.srv.NewListByFirmwarePager(resourceGroupNameParam, workspaceNameParam, firmwareIDParam, nil)
		newListByFirmwarePager = &resp
		p.newListByFirmwarePager.add(req, newListByFirmwarePager)
		server.PagerResponderInjectNextLinks(newListByFirmwarePager, req, func(page *armiotfirmwaredefense.PasswordHashesClientListByFirmwareResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByFirmwarePager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		p.newListByFirmwarePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByFirmwarePager) {
		p.newListByFirmwarePager.remove(req)
	}
	return resp, nil
}

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