// 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 (
	"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/applicationinsights/armapplicationinsights/v2"
	"net/http"
	"net/url"
	"regexp"
)

// DeletedWorkbooksServer is a fake server for instances of the armapplicationinsights.DeletedWorkbooksClient type.
type DeletedWorkbooksServer struct {
	// NewListBySubscriptionPager is the fake for method DeletedWorkbooksClient.NewListBySubscriptionPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBySubscriptionPager func(options *armapplicationinsights.DeletedWorkbooksClientListBySubscriptionOptions) (resp azfake.PagerResponder[armapplicationinsights.DeletedWorkbooksClientListBySubscriptionResponse])
}

// NewDeletedWorkbooksServerTransport creates a new instance of DeletedWorkbooksServerTransport with the provided implementation.
// The returned DeletedWorkbooksServerTransport instance is connected to an instance of armapplicationinsights.DeletedWorkbooksClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewDeletedWorkbooksServerTransport(srv *DeletedWorkbooksServer) *DeletedWorkbooksServerTransport {
	return &DeletedWorkbooksServerTransport{
		srv:                        srv,
		newListBySubscriptionPager: newTracker[azfake.PagerResponder[armapplicationinsights.DeletedWorkbooksClientListBySubscriptionResponse]](),
	}
}

// DeletedWorkbooksServerTransport connects instances of armapplicationinsights.DeletedWorkbooksClient to instances of DeletedWorkbooksServer.
// Don't use this type directly, use NewDeletedWorkbooksServerTransport instead.
type DeletedWorkbooksServerTransport struct {
	srv                        *DeletedWorkbooksServer
	newListBySubscriptionPager *tracker[azfake.PagerResponder[armapplicationinsights.DeletedWorkbooksClientListBySubscriptionResponse]]
}

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

func (d *DeletedWorkbooksServerTransport) 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 deletedWorkbooksServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = deletedWorkbooksServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "DeletedWorkbooksClient.NewListBySubscriptionPager":
				res.resp, res.err = d.dispatchNewListBySubscriptionPager(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 (d *DeletedWorkbooksServerTransport) dispatchNewListBySubscriptionPager(req *http.Request) (*http.Response, error) {
	if d.srv.NewListBySubscriptionPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBySubscriptionPager not implemented")}
	}
	newListBySubscriptionPager := d.newListBySubscriptionPager.get(req)
	if newListBySubscriptionPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Insights/deletedWorkbooks`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 1 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		qp := req.URL.Query()
		categoryUnescaped, err := url.QueryUnescape(qp.Get("category"))
		if err != nil {
			return nil, err
		}
		categoryParam := getOptional(armapplicationinsights.CategoryType(categoryUnescaped))
		tagsUnescaped, err := url.QueryUnescape(qp.Get("tags"))
		if err != nil {
			return nil, err
		}
		tagsParam := splitHelper(tagsUnescaped, ",")
		var options *armapplicationinsights.DeletedWorkbooksClientListBySubscriptionOptions
		if categoryParam != nil || len(tagsParam) > 0 {
			options = &armapplicationinsights.DeletedWorkbooksClientListBySubscriptionOptions{
				Category: categoryParam,
				Tags:     tagsParam,
			}
		}
		resp := d.srv.NewListBySubscriptionPager(options)
		newListBySubscriptionPager = &resp
		d.newListBySubscriptionPager.add(req, newListBySubscriptionPager)
		server.PagerResponderInjectNextLinks(newListBySubscriptionPager, req, func(page *armapplicationinsights.DeletedWorkbooksClientListBySubscriptionResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListBySubscriptionPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		d.newListBySubscriptionPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBySubscriptionPager) {
		d.newListBySubscriptionPager.remove(req)
	}
	return resp, nil
}

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