// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

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/migrationassessment/armmigrationassessment"
	"net/http"
	"net/url"
	"regexp"
)

// SQLAssessmentV2SummaryOperationsServer is a fake server for instances of the armmigrationassessment.SQLAssessmentV2SummaryOperationsClient type.
type SQLAssessmentV2SummaryOperationsServer struct {
	// Get is the fake for method SQLAssessmentV2SummaryOperationsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, projectName string, groupName string, assessmentName string, summaryName string, options *armmigrationassessment.SQLAssessmentV2SummaryOperationsClientGetOptions) (resp azfake.Responder[armmigrationassessment.SQLAssessmentV2SummaryOperationsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListBySQLAssessmentV2Pager is the fake for method SQLAssessmentV2SummaryOperationsClient.NewListBySQLAssessmentV2Pager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBySQLAssessmentV2Pager func(resourceGroupName string, projectName string, groupName string, assessmentName string, options *armmigrationassessment.SQLAssessmentV2SummaryOperationsClientListBySQLAssessmentV2Options) (resp azfake.PagerResponder[armmigrationassessment.SQLAssessmentV2SummaryOperationsClientListBySQLAssessmentV2Response])
}

// NewSQLAssessmentV2SummaryOperationsServerTransport creates a new instance of SQLAssessmentV2SummaryOperationsServerTransport with the provided implementation.
// The returned SQLAssessmentV2SummaryOperationsServerTransport instance is connected to an instance of armmigrationassessment.SQLAssessmentV2SummaryOperationsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewSQLAssessmentV2SummaryOperationsServerTransport(srv *SQLAssessmentV2SummaryOperationsServer) *SQLAssessmentV2SummaryOperationsServerTransport {
	return &SQLAssessmentV2SummaryOperationsServerTransport{
		srv:                           srv,
		newListBySQLAssessmentV2Pager: newTracker[azfake.PagerResponder[armmigrationassessment.SQLAssessmentV2SummaryOperationsClientListBySQLAssessmentV2Response]](),
	}
}

// SQLAssessmentV2SummaryOperationsServerTransport connects instances of armmigrationassessment.SQLAssessmentV2SummaryOperationsClient to instances of SQLAssessmentV2SummaryOperationsServer.
// Don't use this type directly, use NewSQLAssessmentV2SummaryOperationsServerTransport instead.
type SQLAssessmentV2SummaryOperationsServerTransport struct {
	srv                           *SQLAssessmentV2SummaryOperationsServer
	newListBySQLAssessmentV2Pager *tracker[azfake.PagerResponder[armmigrationassessment.SQLAssessmentV2SummaryOperationsClientListBySQLAssessmentV2Response]]
}

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

func (s *SQLAssessmentV2SummaryOperationsServerTransport) 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 sqlAssessmentV2SummaryOperationsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = sqlAssessmentV2SummaryOperationsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "SQLAssessmentV2SummaryOperationsClient.Get":
				res.resp, res.err = s.dispatchGet(req)
			case "SQLAssessmentV2SummaryOperationsClient.NewListBySQLAssessmentV2Pager":
				res.resp, res.err = s.dispatchNewListBySQLAssessmentV2Pager(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 (s *SQLAssessmentV2SummaryOperationsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if s.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\.Migrate/assessmentProjects/(?P<projectName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/groups/(?P<groupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/sqlAssessments/(?P<assessmentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/summaries/(?P<summaryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 6 {
		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
	}
	projectNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("projectName")])
	if err != nil {
		return nil, err
	}
	groupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("groupName")])
	if err != nil {
		return nil, err
	}
	assessmentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("assessmentName")])
	if err != nil {
		return nil, err
	}
	summaryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("summaryName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.Get(req.Context(), resourceGroupNameParam, projectNameParam, groupNameParam, assessmentNameParam, summaryNameParam, 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).SQLAssessmentV2Summary, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (s *SQLAssessmentV2SummaryOperationsServerTransport) dispatchNewListBySQLAssessmentV2Pager(req *http.Request) (*http.Response, error) {
	if s.srv.NewListBySQLAssessmentV2Pager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBySQLAssessmentV2Pager not implemented")}
	}
	newListBySQLAssessmentV2Pager := s.newListBySQLAssessmentV2Pager.get(req)
	if newListBySQLAssessmentV2Pager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Migrate/assessmentProjects/(?P<projectName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/groups/(?P<groupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/sqlAssessments/(?P<assessmentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/summaries`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 5 {
			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
		}
		projectNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("projectName")])
		if err != nil {
			return nil, err
		}
		groupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("groupName")])
		if err != nil {
			return nil, err
		}
		assessmentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("assessmentName")])
		if err != nil {
			return nil, err
		}
		resp := s.srv.NewListBySQLAssessmentV2Pager(resourceGroupNameParam, projectNameParam, groupNameParam, assessmentNameParam, nil)
		newListBySQLAssessmentV2Pager = &resp
		s.newListBySQLAssessmentV2Pager.add(req, newListBySQLAssessmentV2Pager)
		server.PagerResponderInjectNextLinks(newListBySQLAssessmentV2Pager, req, func(page *armmigrationassessment.SQLAssessmentV2SummaryOperationsClientListBySQLAssessmentV2Response, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListBySQLAssessmentV2Pager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		s.newListBySQLAssessmentV2Pager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBySQLAssessmentV2Pager) {
		s.newListBySQLAssessmentV2Pager.remove(req)
	}
	return resp, nil
}

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