// 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/azurestackhci/armazurestackhcivm"
	"net/http"
	"net/url"
	"regexp"
)

// MarketplaceGalleryImagesServer is a fake server for instances of the armazurestackhcivm.MarketplaceGalleryImagesClient type.
type MarketplaceGalleryImagesServer struct {
	// BeginCreateOrUpdate is the fake for method MarketplaceGalleryImagesClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, marketplaceGalleryImageName string, resource armazurestackhcivm.MarketplaceGalleryImage, options *armazurestackhcivm.MarketplaceGalleryImagesClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method MarketplaceGalleryImagesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, marketplaceGalleryImageName string, options *armazurestackhcivm.MarketplaceGalleryImagesClientBeginDeleteOptions) (resp azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method MarketplaceGalleryImagesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, marketplaceGalleryImageName string, options *armazurestackhcivm.MarketplaceGalleryImagesClientGetOptions) (resp azfake.Responder[armazurestackhcivm.MarketplaceGalleryImagesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListAllPager is the fake for method MarketplaceGalleryImagesClient.NewListAllPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListAllPager func(options *armazurestackhcivm.MarketplaceGalleryImagesClientListAllOptions) (resp azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListAllResponse])

	// NewListByResourceGroupPager is the fake for method MarketplaceGalleryImagesClient.NewListByResourceGroupPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByResourceGroupPager func(resourceGroupName string, options *armazurestackhcivm.MarketplaceGalleryImagesClientListByResourceGroupOptions) (resp azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListByResourceGroupResponse])

	// BeginUpdate is the fake for method MarketplaceGalleryImagesClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, marketplaceGalleryImageName string, properties armazurestackhcivm.MarketplaceGalleryImageTagsUpdate, options *armazurestackhcivm.MarketplaceGalleryImagesClientBeginUpdateOptions) (resp azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewMarketplaceGalleryImagesServerTransport creates a new instance of MarketplaceGalleryImagesServerTransport with the provided implementation.
// The returned MarketplaceGalleryImagesServerTransport instance is connected to an instance of armazurestackhcivm.MarketplaceGalleryImagesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewMarketplaceGalleryImagesServerTransport(srv *MarketplaceGalleryImagesServer) *MarketplaceGalleryImagesServerTransport {
	return &MarketplaceGalleryImagesServerTransport{
		srv:                         srv,
		beginCreateOrUpdate:         newTracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientCreateOrUpdateResponse]](),
		beginDelete:                 newTracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientDeleteResponse]](),
		newListAllPager:             newTracker[azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListAllResponse]](),
		newListByResourceGroupPager: newTracker[azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListByResourceGroupResponse]](),
		beginUpdate:                 newTracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientUpdateResponse]](),
	}
}

// MarketplaceGalleryImagesServerTransport connects instances of armazurestackhcivm.MarketplaceGalleryImagesClient to instances of MarketplaceGalleryImagesServer.
// Don't use this type directly, use NewMarketplaceGalleryImagesServerTransport instead.
type MarketplaceGalleryImagesServerTransport struct {
	srv                         *MarketplaceGalleryImagesServer
	beginCreateOrUpdate         *tracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientCreateOrUpdateResponse]]
	beginDelete                 *tracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientDeleteResponse]]
	newListAllPager             *tracker[azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListAllResponse]]
	newListByResourceGroupPager *tracker[azfake.PagerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientListByResourceGroupResponse]]
	beginUpdate                 *tracker[azfake.PollerResponder[armazurestackhcivm.MarketplaceGalleryImagesClientUpdateResponse]]
}

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

func (m *MarketplaceGalleryImagesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if marketplaceGalleryImagesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = marketplaceGalleryImagesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "MarketplaceGalleryImagesClient.BeginCreateOrUpdate":
				res.resp, res.err = m.dispatchBeginCreateOrUpdate(req)
			case "MarketplaceGalleryImagesClient.BeginDelete":
				res.resp, res.err = m.dispatchBeginDelete(req)
			case "MarketplaceGalleryImagesClient.Get":
				res.resp, res.err = m.dispatchGet(req)
			case "MarketplaceGalleryImagesClient.NewListAllPager":
				res.resp, res.err = m.dispatchNewListAllPager(req)
			case "MarketplaceGalleryImagesClient.NewListByResourceGroupPager":
				res.resp, res.err = m.dispatchNewListByResourceGroupPager(req)
			case "MarketplaceGalleryImagesClient.BeginUpdate":
				res.resp, res.err = m.dispatchBeginUpdate(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

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

func (m *MarketplaceGalleryImagesServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if m.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := m.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/marketplaceGalleryImages/(?P<marketplaceGalleryImageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armazurestackhcivm.MarketplaceGalleryImage](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		marketplaceGalleryImageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("marketplaceGalleryImageName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := m.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, marketplaceGalleryImageNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		m.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
	}

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

	if !contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
		m.beginCreateOrUpdate.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginCreateOrUpdate) {
		m.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

func (m *MarketplaceGalleryImagesServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if m.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := m.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/marketplaceGalleryImages/(?P<marketplaceGalleryImageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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
		}
		marketplaceGalleryImageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("marketplaceGalleryImageName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := m.srv.BeginDelete(req.Context(), resourceGroupNameParam, marketplaceGalleryImageNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		m.beginDelete.add(req, beginDelete)
	}

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

	if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		m.beginDelete.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(beginDelete) {
		m.beginDelete.remove(req)
	}

	return resp, nil
}

func (m *MarketplaceGalleryImagesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if m.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\.AzureStackHCI/marketplaceGalleryImages/(?P<marketplaceGalleryImageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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
	}
	marketplaceGalleryImageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("marketplaceGalleryImageName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.Get(req.Context(), resourceGroupNameParam, marketplaceGalleryImageNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !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).MarketplaceGalleryImage, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *MarketplaceGalleryImagesServerTransport) dispatchNewListAllPager(req *http.Request) (*http.Response, error) {
	if m.srv.NewListAllPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListAllPager not implemented")}
	}
	newListAllPager := m.newListAllPager.get(req)
	if newListAllPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/marketplaceGalleryImages`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resp := m.srv.NewListAllPager(nil)
		newListAllPager = &resp
		m.newListAllPager.add(req, newListAllPager)
		server.PagerResponderInjectNextLinks(newListAllPager, req, func(page *armazurestackhcivm.MarketplaceGalleryImagesClientListAllResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListAllPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		m.newListAllPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListAllPager) {
		m.newListAllPager.remove(req)
	}
	return resp, nil
}

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

func (m *MarketplaceGalleryImagesServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if m.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := m.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/marketplaceGalleryImages/(?P<marketplaceGalleryImageName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armazurestackhcivm.MarketplaceGalleryImageTagsUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		marketplaceGalleryImageNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("marketplaceGalleryImageName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := m.srv.BeginUpdate(req.Context(), resourceGroupNameParam, marketplaceGalleryImageNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		m.beginUpdate.add(req, beginUpdate)
	}

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

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

	return resp, nil
}

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