// 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/workloadorchestration/armworkloadorchestration"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// SolutionTemplateVersionsServer is a fake server for instances of the armworkloadorchestration.SolutionTemplateVersionsClient type.
type SolutionTemplateVersionsServer struct {
	// BeginBulkDeploySolution is the fake for method SolutionTemplateVersionsClient.BeginBulkDeploySolution
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginBulkDeploySolution func(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body armworkloadorchestration.BulkDeploySolutionParameter, options *armworkloadorchestration.SolutionTemplateVersionsClientBeginBulkDeploySolutionOptions) (resp azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkDeploySolutionResponse], errResp azfake.ErrorResponder)

	// BeginBulkPublishSolution is the fake for method SolutionTemplateVersionsClient.BeginBulkPublishSolution
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginBulkPublishSolution func(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, body armworkloadorchestration.BulkPublishSolutionParameter, options *armworkloadorchestration.SolutionTemplateVersionsClientBeginBulkPublishSolutionOptions) (resp azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkPublishSolutionResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method SolutionTemplateVersionsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, solutionTemplateName string, solutionTemplateVersionName string, options *armworkloadorchestration.SolutionTemplateVersionsClientGetOptions) (resp azfake.Responder[armworkloadorchestration.SolutionTemplateVersionsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListBySolutionTemplatePager is the fake for method SolutionTemplateVersionsClient.NewListBySolutionTemplatePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBySolutionTemplatePager func(resourceGroupName string, solutionTemplateName string, options *armworkloadorchestration.SolutionTemplateVersionsClientListBySolutionTemplateOptions) (resp azfake.PagerResponder[armworkloadorchestration.SolutionTemplateVersionsClientListBySolutionTemplateResponse])
}

// NewSolutionTemplateVersionsServerTransport creates a new instance of SolutionTemplateVersionsServerTransport with the provided implementation.
// The returned SolutionTemplateVersionsServerTransport instance is connected to an instance of armworkloadorchestration.SolutionTemplateVersionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewSolutionTemplateVersionsServerTransport(srv *SolutionTemplateVersionsServer) *SolutionTemplateVersionsServerTransport {
	return &SolutionTemplateVersionsServerTransport{
		srv:                            srv,
		beginBulkDeploySolution:        newTracker[azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkDeploySolutionResponse]](),
		beginBulkPublishSolution:       newTracker[azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkPublishSolutionResponse]](),
		newListBySolutionTemplatePager: newTracker[azfake.PagerResponder[armworkloadorchestration.SolutionTemplateVersionsClientListBySolutionTemplateResponse]](),
	}
}

// SolutionTemplateVersionsServerTransport connects instances of armworkloadorchestration.SolutionTemplateVersionsClient to instances of SolutionTemplateVersionsServer.
// Don't use this type directly, use NewSolutionTemplateVersionsServerTransport instead.
type SolutionTemplateVersionsServerTransport struct {
	srv                            *SolutionTemplateVersionsServer
	beginBulkDeploySolution        *tracker[azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkDeploySolutionResponse]]
	beginBulkPublishSolution       *tracker[azfake.PollerResponder[armworkloadorchestration.SolutionTemplateVersionsClientBulkPublishSolutionResponse]]
	newListBySolutionTemplatePager *tracker[azfake.PagerResponder[armworkloadorchestration.SolutionTemplateVersionsClientListBySolutionTemplateResponse]]
}

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

func (s *SolutionTemplateVersionsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if solutionTemplateVersionsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = solutionTemplateVersionsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "SolutionTemplateVersionsClient.BeginBulkDeploySolution":
				res.resp, res.err = s.dispatchBeginBulkDeploySolution(req)
			case "SolutionTemplateVersionsClient.BeginBulkPublishSolution":
				res.resp, res.err = s.dispatchBeginBulkPublishSolution(req)
			case "SolutionTemplateVersionsClient.Get":
				res.resp, res.err = s.dispatchGet(req)
			case "SolutionTemplateVersionsClient.NewListBySolutionTemplatePager":
				res.resp, res.err = s.dispatchNewListBySolutionTemplatePager(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 (s *SolutionTemplateVersionsServerTransport) dispatchBeginBulkDeploySolution(req *http.Request) (*http.Response, error) {
	if s.srv.BeginBulkDeploySolution == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginBulkDeploySolution not implemented")}
	}
	beginBulkDeploySolution := s.beginBulkDeploySolution.get(req)
	if beginBulkDeploySolution == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/solutionTemplates/(?P<solutionTemplateName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<solutionTemplateVersionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/bulkDeploySolution`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.BulkDeploySolutionParameter](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		solutionTemplateNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateName")])
		if err != nil {
			return nil, err
		}
		solutionTemplateVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateVersionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginBulkDeploySolution(req.Context(), resourceGroupNameParam, solutionTemplateNameParam, solutionTemplateVersionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginBulkDeploySolution = &respr
		s.beginBulkDeploySolution.add(req, beginBulkDeploySolution)
	}

	resp, err := server.PollerResponderNext(beginBulkDeploySolution, req)
	if err != nil {
		return nil, err
	}

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		s.beginBulkDeploySolution.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginBulkDeploySolution) {
		s.beginBulkDeploySolution.remove(req)
	}

	return resp, nil
}

func (s *SolutionTemplateVersionsServerTransport) dispatchBeginBulkPublishSolution(req *http.Request) (*http.Response, error) {
	if s.srv.BeginBulkPublishSolution == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginBulkPublishSolution not implemented")}
	}
	beginBulkPublishSolution := s.beginBulkPublishSolution.get(req)
	if beginBulkPublishSolution == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/solutionTemplates/(?P<solutionTemplateName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<solutionTemplateVersionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/bulkPublishSolution`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.BulkPublishSolutionParameter](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		solutionTemplateNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateName")])
		if err != nil {
			return nil, err
		}
		solutionTemplateVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateVersionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginBulkPublishSolution(req.Context(), resourceGroupNameParam, solutionTemplateNameParam, solutionTemplateVersionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginBulkPublishSolution = &respr
		s.beginBulkPublishSolution.add(req, beginBulkPublishSolution)
	}

	resp, err := server.PollerResponderNext(beginBulkPublishSolution, req)
	if err != nil {
		return nil, err
	}

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		s.beginBulkPublishSolution.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginBulkPublishSolution) {
		s.beginBulkPublishSolution.remove(req)
	}

	return resp, nil
}

func (s *SolutionTemplateVersionsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if s.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/solutionTemplates/(?P<solutionTemplateName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<solutionTemplateVersionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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
	}
	solutionTemplateNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateName")])
	if err != nil {
		return nil, err
	}
	solutionTemplateVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateVersionName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.Get(req.Context(), resourceGroupNameParam, solutionTemplateNameParam, solutionTemplateVersionNameParam, 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).SolutionTemplateVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (s *SolutionTemplateVersionsServerTransport) dispatchNewListBySolutionTemplatePager(req *http.Request) (*http.Response, error) {
	if s.srv.NewListBySolutionTemplatePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBySolutionTemplatePager not implemented")}
	}
	newListBySolutionTemplatePager := s.newListBySolutionTemplatePager.get(req)
	if newListBySolutionTemplatePager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/solutionTemplates/(?P<solutionTemplateName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 4 {
			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
		}
		solutionTemplateNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("solutionTemplateName")])
		if err != nil {
			return nil, err
		}
		resp := s.srv.NewListBySolutionTemplatePager(resourceGroupNameParam, solutionTemplateNameParam, nil)
		newListBySolutionTemplatePager = &resp
		s.newListBySolutionTemplatePager.add(req, newListBySolutionTemplatePager)
		server.PagerResponderInjectNextLinks(newListBySolutionTemplatePager, req, func(page *armworkloadorchestration.SolutionTemplateVersionsClientListBySolutionTemplateResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListBySolutionTemplatePager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		s.newListBySolutionTemplatePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBySolutionTemplatePager) {
		s.newListBySolutionTemplatePager.remove(req)
	}
	return resp, nil
}

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