// 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"
	"net/http"
	"net/url"
	"reflect"
	"regexp"

	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/resourcemanager/marketplace/armmarketplace/v2"
)

// PrivateStoreCollectionServer is a fake server for instances of the armmarketplace.PrivateStoreCollectionClient type.
type PrivateStoreCollectionServer struct {
	// ApproveAllItems is the fake for method PrivateStoreCollectionClient.ApproveAllItems
	// HTTP status codes to indicate success: http.StatusOK
	ApproveAllItems func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientApproveAllItemsOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientApproveAllItemsResponse], errResp azfake.ErrorResponder)

	// CreateOrUpdate is the fake for method PrivateStoreCollectionClient.CreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK
	CreateOrUpdate func(ctx context.Context, privateStoreID string, collectionID string, payload armmarketplace.Collection, options *armmarketplace.PrivateStoreCollectionClientCreateOrUpdateOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// Delete is the fake for method PrivateStoreCollectionClient.Delete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusNoContent
	Delete func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientDeleteOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientDeleteResponse], errResp azfake.ErrorResponder)

	// DisableApproveAllItems is the fake for method PrivateStoreCollectionClient.DisableApproveAllItems
	// HTTP status codes to indicate success: http.StatusOK
	DisableApproveAllItems func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientDisableApproveAllItemsOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientDisableApproveAllItemsResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method PrivateStoreCollectionClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientGetOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientGetResponse], errResp azfake.ErrorResponder)

	// List is the fake for method PrivateStoreCollectionClient.List
	// HTTP status codes to indicate success: http.StatusOK
	List func(ctx context.Context, privateStoreID string, options *armmarketplace.PrivateStoreCollectionClientListOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientListResponse], errResp azfake.ErrorResponder)

	// Post is the fake for method PrivateStoreCollectionClient.Post
	// HTTP status codes to indicate success: http.StatusOK
	Post func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientPostOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientPostResponse], errResp azfake.ErrorResponder)

	// TransferOffers is the fake for method PrivateStoreCollectionClient.TransferOffers
	// HTTP status codes to indicate success: http.StatusOK
	TransferOffers func(ctx context.Context, privateStoreID string, collectionID string, options *armmarketplace.PrivateStoreCollectionClientTransferOffersOptions) (resp azfake.Responder[armmarketplace.PrivateStoreCollectionClientTransferOffersResponse], errResp azfake.ErrorResponder)
}

// NewPrivateStoreCollectionServerTransport creates a new instance of PrivateStoreCollectionServerTransport with the provided implementation.
// The returned PrivateStoreCollectionServerTransport instance is connected to an instance of armmarketplace.PrivateStoreCollectionClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewPrivateStoreCollectionServerTransport(srv *PrivateStoreCollectionServer) *PrivateStoreCollectionServerTransport {
	return &PrivateStoreCollectionServerTransport{srv: srv}
}

// PrivateStoreCollectionServerTransport connects instances of armmarketplace.PrivateStoreCollectionClient to instances of PrivateStoreCollectionServer.
// Don't use this type directly, use NewPrivateStoreCollectionServerTransport instead.
type PrivateStoreCollectionServerTransport struct {
	srv *PrivateStoreCollectionServer
}

// Do implements the policy.Transporter interface for PrivateStoreCollectionServerTransport.
func (p *PrivateStoreCollectionServerTransport) 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 *PrivateStoreCollectionServerTransport) 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 privateStoreCollectionServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = privateStoreCollectionServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "PrivateStoreCollectionClient.ApproveAllItems":
				res.resp, res.err = p.dispatchApproveAllItems(req)
			case "PrivateStoreCollectionClient.CreateOrUpdate":
				res.resp, res.err = p.dispatchCreateOrUpdate(req)
			case "PrivateStoreCollectionClient.Delete":
				res.resp, res.err = p.dispatchDelete(req)
			case "PrivateStoreCollectionClient.DisableApproveAllItems":
				res.resp, res.err = p.dispatchDisableApproveAllItems(req)
			case "PrivateStoreCollectionClient.Get":
				res.resp, res.err = p.dispatchGet(req)
			case "PrivateStoreCollectionClient.List":
				res.resp, res.err = p.dispatchList(req)
			case "PrivateStoreCollectionClient.Post":
				res.resp, res.err = p.dispatchPost(req)
			case "PrivateStoreCollectionClient.TransferOffers":
				res.resp, res.err = p.dispatchTransferOffers(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 (p *PrivateStoreCollectionServerTransport) dispatchApproveAllItems(req *http.Request) (*http.Response, error) {
	if p.srv.ApproveAllItems == nil {
		return nil, &nonRetriableError{errors.New("fake for method ApproveAllItems not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/approveAllItems`
	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)
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.ApproveAllItems(req.Context(), privateStoreIDParam, collectionIDParam, 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).Collection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if p.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armmarketplace.Collection](req)
	if err != nil {
		return nil, err
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.CreateOrUpdate(req.Context(), privateStoreIDParam, collectionIDParam, body, 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).Collection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchDelete(req *http.Request) (*http.Response, error) {
	if p.srv.Delete == nil {
		return nil, &nonRetriableError{errors.New("fake for method Delete not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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)
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.Delete(req.Context(), privateStoreIDParam, collectionIDParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchDisableApproveAllItems(req *http.Request) (*http.Response, error) {
	if p.srv.DisableApproveAllItems == nil {
		return nil, &nonRetriableError{errors.New("fake for method DisableApproveAllItems not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/disableApproveAllItems`
	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)
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.DisableApproveAllItems(req.Context(), privateStoreIDParam, collectionIDParam, 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).Collection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if p.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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)
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.Get(req.Context(), privateStoreIDParam, collectionIDParam, 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).Collection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchList(req *http.Request) (*http.Response, error) {
	if p.srv.List == nil {
		return nil, &nonRetriableError{errors.New("fake for method List not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections`
	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)
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.List(req.Context(), privateStoreIDParam, 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).CollectionsList, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchPost(req *http.Request) (*http.Response, error) {
	if p.srv.Post == nil {
		return nil, &nonRetriableError{errors.New("fake for method Post not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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)
	}
	body, err := server.UnmarshalRequestAsText(req)
	if err != nil {
		return nil, err
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	var options *armmarketplace.PrivateStoreCollectionClientPostOptions
	if !reflect.ValueOf(body).IsZero() {
		op := armmarketplace.Operation(body)
		options = &armmarketplace.PrivateStoreCollectionClientPostOptions{
			Payload: &op,
		}
	}
	respr, errRespr := p.srv.Post(req.Context(), privateStoreIDParam, collectionIDParam, options)
	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.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PrivateStoreCollectionServerTransport) dispatchTransferOffers(req *http.Request) (*http.Response, error) {
	if p.srv.TransferOffers == nil {
		return nil, &nonRetriableError{errors.New("fake for method TransferOffers not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Marketplace/privateStores/(?P<privateStoreId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/collections/(?P<collectionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transferOffers`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armmarketplace.TransferOffersProperties](req)
	if err != nil {
		return nil, err
	}
	privateStoreIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("privateStoreId")])
	if err != nil {
		return nil, err
	}
	collectionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("collectionId")])
	if err != nil {
		return nil, err
	}
	var options *armmarketplace.PrivateStoreCollectionClientTransferOffersOptions
	if !reflect.ValueOf(body).IsZero() {
		options = &armmarketplace.PrivateStoreCollectionClientTransferOffersOptions{
			Payload: &body,
		}
	}
	respr, errRespr := p.srv.TransferOffers(req.Context(), privateStoreIDParam, collectionIDParam, options)
	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).TransferOffersResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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