// 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/cdn/armcdn/v3"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// ManagementServer is a fake server for instances of the armcdn.ManagementClient type.
type ManagementServer struct {
	// CheckEndpointNameAvailability is the fake for method ManagementClient.CheckEndpointNameAvailability
	// HTTP status codes to indicate success: http.StatusOK
	CheckEndpointNameAvailability func(ctx context.Context, resourceGroupName string, checkEndpointNameAvailabilityInput armcdn.CheckEndpointNameAvailabilityInput, options *armcdn.ManagementClientCheckEndpointNameAvailabilityOptions) (resp azfake.Responder[armcdn.ManagementClientCheckEndpointNameAvailabilityResponse], errResp azfake.ErrorResponder)

	// CheckNameAvailability is the fake for method ManagementClient.CheckNameAvailability
	// HTTP status codes to indicate success: http.StatusOK
	CheckNameAvailability func(ctx context.Context, checkNameAvailabilityInput armcdn.CheckNameAvailabilityInput, options *armcdn.ManagementClientCheckNameAvailabilityOptions) (resp azfake.Responder[armcdn.ManagementClientCheckNameAvailabilityResponse], errResp azfake.ErrorResponder)

	// CheckNameAvailabilityWithSubscription is the fake for method ManagementClient.CheckNameAvailabilityWithSubscription
	// HTTP status codes to indicate success: http.StatusOK
	CheckNameAvailabilityWithSubscription func(ctx context.Context, checkNameAvailabilityInput armcdn.CheckNameAvailabilityInput, options *armcdn.ManagementClientCheckNameAvailabilityWithSubscriptionOptions) (resp azfake.Responder[armcdn.ManagementClientCheckNameAvailabilityWithSubscriptionResponse], errResp azfake.ErrorResponder)

	// ValidateProbe is the fake for method ManagementClient.ValidateProbe
	// HTTP status codes to indicate success: http.StatusOK
	ValidateProbe func(ctx context.Context, validateProbeInput armcdn.ValidateProbeInput, options *armcdn.ManagementClientValidateProbeOptions) (resp azfake.Responder[armcdn.ManagementClientValidateProbeResponse], errResp azfake.ErrorResponder)
}

// NewManagementServerTransport creates a new instance of ManagementServerTransport with the provided implementation.
// The returned ManagementServerTransport instance is connected to an instance of armcdn.ManagementClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewManagementServerTransport(srv *ManagementServer) *ManagementServerTransport {
	return &ManagementServerTransport{srv: srv}
}

// ManagementServerTransport connects instances of armcdn.ManagementClient to instances of ManagementServer.
// Don't use this type directly, use NewManagementServerTransport instead.
type ManagementServerTransport struct {
	srv *ManagementServer
}

// Do implements the policy.Transporter interface for ManagementServerTransport.
func (m *ManagementServerTransport) 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 *ManagementServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if managementServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = managementServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ManagementClient.CheckEndpointNameAvailability":
				res.resp, res.err = m.dispatchCheckEndpointNameAvailability(req)
			case "ManagementClient.CheckNameAvailability":
				res.resp, res.err = m.dispatchCheckNameAvailability(req)
			case "ManagementClient.CheckNameAvailabilityWithSubscription":
				res.resp, res.err = m.dispatchCheckNameAvailabilityWithSubscription(req)
			case "ManagementClient.ValidateProbe":
				res.resp, res.err = m.dispatchValidateProbe(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 (m *ManagementServerTransport) dispatchCheckEndpointNameAvailability(req *http.Request) (*http.Response, error) {
	if m.srv.CheckEndpointNameAvailability == nil {
		return nil, &nonRetriableError{errors.New("fake for method CheckEndpointNameAvailability not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Cdn/checkEndpointNameAvailability`
	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[armcdn.CheckEndpointNameAvailabilityInput](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.CheckEndpointNameAvailability(req.Context(), resourceGroupNameParam, body, 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).CheckEndpointNameAvailabilityOutput, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *ManagementServerTransport) dispatchCheckNameAvailability(req *http.Request) (*http.Response, error) {
	if m.srv.CheckNameAvailability == nil {
		return nil, &nonRetriableError{errors.New("fake for method CheckNameAvailability not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armcdn.CheckNameAvailabilityInput](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.CheckNameAvailability(req.Context(), body, 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).CheckNameAvailabilityOutput, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *ManagementServerTransport) dispatchCheckNameAvailabilityWithSubscription(req *http.Request) (*http.Response, error) {
	if m.srv.CheckNameAvailabilityWithSubscription == nil {
		return nil, &nonRetriableError{errors.New("fake for method CheckNameAvailabilityWithSubscription not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Cdn/checkNameAvailability`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armcdn.CheckNameAvailabilityInput](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.CheckNameAvailabilityWithSubscription(req.Context(), body, 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).CheckNameAvailabilityOutput, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *ManagementServerTransport) dispatchValidateProbe(req *http.Request) (*http.Response, error) {
	if m.srv.ValidateProbe == nil {
		return nil, &nonRetriableError{errors.New("fake for method ValidateProbe not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Cdn/validateProbe`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armcdn.ValidateProbeInput](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.ValidateProbe(req.Context(), body, 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).ValidateProbeOutput, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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