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

// UpdateSummariesOperationGroupServer is a fake server for instances of the armazurestackhci.UpdateSummariesOperationGroupClient type.
type UpdateSummariesOperationGroupServer struct {
	// BeginCheckHealth is the fake for method UpdateSummariesOperationGroupClient.BeginCheckHealth
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginCheckHealth func(ctx context.Context, resourceGroupName string, clusterName string, options *armazurestackhci.UpdateSummariesOperationGroupClientBeginCheckHealthOptions) (resp azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckHealthResponse], errResp azfake.ErrorResponder)

	// BeginCheckUpdates is the fake for method UpdateSummariesOperationGroupClient.BeginCheckUpdates
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginCheckUpdates func(ctx context.Context, resourceGroupName string, clusterName string, body armazurestackhci.CheckUpdatesRequest, options *armazurestackhci.UpdateSummariesOperationGroupClientBeginCheckUpdatesOptions) (resp azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckUpdatesResponse], errResp azfake.ErrorResponder)
}

// NewUpdateSummariesOperationGroupServerTransport creates a new instance of UpdateSummariesOperationGroupServerTransport with the provided implementation.
// The returned UpdateSummariesOperationGroupServerTransport instance is connected to an instance of armazurestackhci.UpdateSummariesOperationGroupClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewUpdateSummariesOperationGroupServerTransport(srv *UpdateSummariesOperationGroupServer) *UpdateSummariesOperationGroupServerTransport {
	return &UpdateSummariesOperationGroupServerTransport{
		srv:               srv,
		beginCheckHealth:  newTracker[azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckHealthResponse]](),
		beginCheckUpdates: newTracker[azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckUpdatesResponse]](),
	}
}

// UpdateSummariesOperationGroupServerTransport connects instances of armazurestackhci.UpdateSummariesOperationGroupClient to instances of UpdateSummariesOperationGroupServer.
// Don't use this type directly, use NewUpdateSummariesOperationGroupServerTransport instead.
type UpdateSummariesOperationGroupServerTransport struct {
	srv               *UpdateSummariesOperationGroupServer
	beginCheckHealth  *tracker[azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckHealthResponse]]
	beginCheckUpdates *tracker[azfake.PollerResponder[armazurestackhci.UpdateSummariesOperationGroupClientCheckUpdatesResponse]]
}

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

func (u *UpdateSummariesOperationGroupServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if updateSummariesOperationGroupServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = updateSummariesOperationGroupServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "UpdateSummariesOperationGroupClient.BeginCheckHealth":
				res.resp, res.err = u.dispatchBeginCheckHealth(req)
			case "UpdateSummariesOperationGroupClient.BeginCheckUpdates":
				res.resp, res.err = u.dispatchBeginCheckUpdates(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 (u *UpdateSummariesOperationGroupServerTransport) dispatchBeginCheckHealth(req *http.Request) (*http.Response, error) {
	if u.srv.BeginCheckHealth == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCheckHealth not implemented")}
	}
	beginCheckHealth := u.beginCheckHealth.get(req)
	if beginCheckHealth == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/clusters/(?P<clusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/updateSummaries/default/checkHealth`
		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
		}
		clusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := u.srv.BeginCheckHealth(req.Context(), resourceGroupNameParam, clusterNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCheckHealth = &respr
		u.beginCheckHealth.add(req, beginCheckHealth)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		u.beginCheckHealth.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(beginCheckHealth) {
		u.beginCheckHealth.remove(req)
	}

	return resp, nil
}

func (u *UpdateSummariesOperationGroupServerTransport) dispatchBeginCheckUpdates(req *http.Request) (*http.Response, error) {
	if u.srv.BeginCheckUpdates == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCheckUpdates not implemented")}
	}
	beginCheckUpdates := u.beginCheckUpdates.get(req)
	if beginCheckUpdates == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/clusters/(?P<clusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/updateSummaries/default/checkUpdates`
		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[armazurestackhci.CheckUpdatesRequest](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		clusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("clusterName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := u.srv.BeginCheckUpdates(req.Context(), resourceGroupNameParam, clusterNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCheckUpdates = &respr
		u.beginCheckUpdates.add(req, beginCheckUpdates)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		u.beginCheckUpdates.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(beginCheckUpdates) {
		u.beginCheckUpdates.remove(req)
	}

	return resp, nil
}

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