// 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/resourcemanager/servicefabricmanagedclusters/armservicefabricmanagedclusters"
	"net/http"
	"net/url"
	"regexp"
)

// ManagedClusterVersionServer is a fake server for instances of the armservicefabricmanagedclusters.ManagedClusterVersionClient type.
type ManagedClusterVersionServer struct {
	// Get is the fake for method ManagedClusterVersionClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, location string, clusterVersion string, options *armservicefabricmanagedclusters.ManagedClusterVersionClientGetOptions) (resp azfake.Responder[armservicefabricmanagedclusters.ManagedClusterVersionClientGetResponse], errResp azfake.ErrorResponder)

	// GetByEnvironment is the fake for method ManagedClusterVersionClient.GetByEnvironment
	// HTTP status codes to indicate success: http.StatusOK
	GetByEnvironment func(ctx context.Context, location string, environment armservicefabricmanagedclusters.ManagedClusterVersionEnvironment, clusterVersion string, options *armservicefabricmanagedclusters.ManagedClusterVersionClientGetByEnvironmentOptions) (resp azfake.Responder[armservicefabricmanagedclusters.ManagedClusterVersionClientGetByEnvironmentResponse], errResp azfake.ErrorResponder)

	// List is the fake for method ManagedClusterVersionClient.List
	// HTTP status codes to indicate success: http.StatusOK
	List func(ctx context.Context, location string, options *armservicefabricmanagedclusters.ManagedClusterVersionClientListOptions) (resp azfake.Responder[armservicefabricmanagedclusters.ManagedClusterVersionClientListResponse], errResp azfake.ErrorResponder)

	// ListByEnvironment is the fake for method ManagedClusterVersionClient.ListByEnvironment
	// HTTP status codes to indicate success: http.StatusOK
	ListByEnvironment func(ctx context.Context, location string, environment armservicefabricmanagedclusters.ManagedClusterVersionEnvironment, options *armservicefabricmanagedclusters.ManagedClusterVersionClientListByEnvironmentOptions) (resp azfake.Responder[armservicefabricmanagedclusters.ManagedClusterVersionClientListByEnvironmentResponse], errResp azfake.ErrorResponder)
}

// NewManagedClusterVersionServerTransport creates a new instance of ManagedClusterVersionServerTransport with the provided implementation.
// The returned ManagedClusterVersionServerTransport instance is connected to an instance of armservicefabricmanagedclusters.ManagedClusterVersionClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewManagedClusterVersionServerTransport(srv *ManagedClusterVersionServer) *ManagedClusterVersionServerTransport {
	return &ManagedClusterVersionServerTransport{srv: srv}
}

// ManagedClusterVersionServerTransport connects instances of armservicefabricmanagedclusters.ManagedClusterVersionClient to instances of ManagedClusterVersionServer.
// Don't use this type directly, use NewManagedClusterVersionServerTransport instead.
type ManagedClusterVersionServerTransport struct {
	srv *ManagedClusterVersionServer
}

// Do implements the policy.Transporter interface for ManagedClusterVersionServerTransport.
func (m *ManagedClusterVersionServerTransport) 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 *ManagedClusterVersionServerTransport) 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 managedClusterVersionServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = managedClusterVersionServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ManagedClusterVersionClient.Get":
				res.resp, res.err = m.dispatchGet(req)
			case "ManagedClusterVersionClient.GetByEnvironment":
				res.resp, res.err = m.dispatchGetByEnvironment(req)
			case "ManagedClusterVersionClient.List":
				res.resp, res.err = m.dispatchList(req)
			case "ManagedClusterVersionClient.ListByEnvironment":
				res.resp, res.err = m.dispatchListByEnvironment(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 *ManagedClusterVersionServerTransport) 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~%@]+)/providers/Microsoft\.ServiceFabric/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedClusterVersions/(?P<clusterVersion>[!#&$-;=?-\[\]_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)
	}
	locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
	if err != nil {
		return nil, err
	}
	clusterVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.Get(req.Context(), locationParam, clusterVersionParam, 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).ManagedClusterCodeVersionResult, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *ManagedClusterVersionServerTransport) dispatchGetByEnvironment(req *http.Request) (*http.Response, error) {
	if m.srv.GetByEnvironment == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetByEnvironment not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ServiceFabric/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/environments/(?P<environment>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedClusterVersions/(?P<clusterVersion>[!#&$-;=?-\[\]_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)
	}
	locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
	if err != nil {
		return nil, err
	}
	environmentParam, err := parseWithCast(matches[regex.SubexpIndex("environment")], func(v string) (armservicefabricmanagedclusters.ManagedClusterVersionEnvironment, error) {
		p, unescapeErr := url.PathUnescape(v)
		if unescapeErr != nil {
			return "", unescapeErr
		}
		return armservicefabricmanagedclusters.ManagedClusterVersionEnvironment(p), nil
	})
	if err != nil {
		return nil, err
	}
	clusterVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.GetByEnvironment(req.Context(), locationParam, environmentParam, clusterVersionParam, 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).ManagedClusterCodeVersionResult, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (m *ManagedClusterVersionServerTransport) dispatchListByEnvironment(req *http.Request) (*http.Response, error) {
	if m.srv.ListByEnvironment == nil {
		return nil, &nonRetriableError{errors.New("fake for method ListByEnvironment not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ServiceFabric/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/environments/(?P<environment>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedClusterVersions`
	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)
	}
	locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
	if err != nil {
		return nil, err
	}
	environmentParam, err := parseWithCast(matches[regex.SubexpIndex("environment")], func(v string) (armservicefabricmanagedclusters.ManagedClusterVersionEnvironment, error) {
		p, unescapeErr := url.PathUnescape(v)
		if unescapeErr != nil {
			return "", unescapeErr
		}
		return armservicefabricmanagedclusters.ManagedClusterVersionEnvironment(p), nil
	})
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.ListByEnvironment(req.Context(), locationParam, environmentParam, 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).ManagedClusterCodeVersionResultArray, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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