// 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 (
	"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/disconnectedoperations/armdisconnectedoperations"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// ArtifactsServer is a fake server for instances of the armdisconnectedoperations.ArtifactsClient type.
type ArtifactsServer struct {
	// Get is the fake for method ArtifactsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, options *armdisconnectedoperations.ArtifactsClientGetOptions) (resp azfake.Responder[armdisconnectedoperations.ArtifactsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByParentPager is the fake for method ArtifactsClient.NewListByParentPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByParentPager func(resourceGroupName string, name string, imageName string, options *armdisconnectedoperations.ArtifactsClientListByParentOptions) (resp azfake.PagerResponder[armdisconnectedoperations.ArtifactsClientListByParentResponse])

	// ListDownloadURI is the fake for method ArtifactsClient.ListDownloadURI
	// HTTP status codes to indicate success: http.StatusOK
	ListDownloadURI func(ctx context.Context, resourceGroupName string, name string, imageName string, artifactName string, options *armdisconnectedoperations.ArtifactsClientListDownloadURIOptions) (resp azfake.Responder[armdisconnectedoperations.ArtifactsClientListDownloadURIResponse], errResp azfake.ErrorResponder)
}

// NewArtifactsServerTransport creates a new instance of ArtifactsServerTransport with the provided implementation.
// The returned ArtifactsServerTransport instance is connected to an instance of armdisconnectedoperations.ArtifactsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewArtifactsServerTransport(srv *ArtifactsServer) *ArtifactsServerTransport {
	return &ArtifactsServerTransport{
		srv:                  srv,
		newListByParentPager: newTracker[azfake.PagerResponder[armdisconnectedoperations.ArtifactsClientListByParentResponse]](),
	}
}

// ArtifactsServerTransport connects instances of armdisconnectedoperations.ArtifactsClient to instances of ArtifactsServer.
// Don't use this type directly, use NewArtifactsServerTransport instead.
type ArtifactsServerTransport struct {
	srv                  *ArtifactsServer
	newListByParentPager *tracker[azfake.PagerResponder[armdisconnectedoperations.ArtifactsClientListByParentResponse]]
}

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

func (a *ArtifactsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if artifactsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = artifactsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ArtifactsClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "ArtifactsClient.NewListByParentPager":
				res.resp, res.err = a.dispatchNewListByParentPager(req)
			case "ArtifactsClient.ListDownloadURI":
				res.resp, res.err = a.dispatchListDownloadURI(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 (a *ArtifactsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if a.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\.Edge/disconnectedOperations/(?P<name>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/images/(?P<imageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/artifacts/(?P<artifactName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 6 {
		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
	}
	nameParam, err := url.PathUnescape(matches[regex.SubexpIndex("name")])
	if err != nil {
		return nil, err
	}
	imageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("imageName")])
	if err != nil {
		return nil, err
	}
	artifactNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("artifactName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, nameParam, imageNameParam, artifactNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.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).Artifact, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (a *ArtifactsServerTransport) dispatchNewListByParentPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewListByParentPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByParentPager not implemented")}
	}
	newListByParentPager := a.newListByParentPager.get(req)
	if newListByParentPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/disconnectedOperations/(?P<name>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/images/(?P<imageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/artifacts`
		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
		}
		nameParam, err := url.PathUnescape(matches[regex.SubexpIndex("name")])
		if err != nil {
			return nil, err
		}
		imageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("imageName")])
		if err != nil {
			return nil, err
		}
		resp := a.srv.NewListByParentPager(resourceGroupNameParam, nameParam, imageNameParam, nil)
		newListByParentPager = &resp
		a.newListByParentPager.add(req, newListByParentPager)
		server.PagerResponderInjectNextLinks(newListByParentPager, req, func(page *armdisconnectedoperations.ArtifactsClientListByParentResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByParentPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newListByParentPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByParentPager) {
		a.newListByParentPager.remove(req)
	}
	return resp, nil
}

func (a *ArtifactsServerTransport) dispatchListDownloadURI(req *http.Request) (*http.Response, error) {
	if a.srv.ListDownloadURI == nil {
		return nil, &nonRetriableError{errors.New("fake for method ListDownloadURI not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/disconnectedOperations/(?P<name>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/images/(?P<imageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/artifacts/(?P<artifactName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/listDownloadUri`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 6 {
		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
	}
	nameParam, err := url.PathUnescape(matches[regex.SubexpIndex("name")])
	if err != nil {
		return nil, err
	}
	imageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("imageName")])
	if err != nil {
		return nil, err
	}
	artifactNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("artifactName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.ListDownloadURI(req.Context(), resourceGroupNameParam, nameParam, imageNameParam, artifactNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.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).ArtifactDownloadResult, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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