// 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/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/commvaultcontentstore/armcommvaultcontentstore"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// PlansServer is a fake server for instances of the armcommvaultcontentstore.PlansClient type.
type PlansServer struct {
	// BeginCreateOrupdate is the fake for method PlansClient.BeginCreateOrupdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrupdate func(ctx context.Context, resourceGroupName string, cloudAccountName string, planName string, resource armcommvaultcontentstore.CommvaultPlan, options *armcommvaultcontentstore.PlansClientBeginCreateOrupdateOptions) (resp azfake.PollerResponder[armcommvaultcontentstore.PlansClientCreateOrupdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method PlansClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, cloudAccountName string, planName string, options *armcommvaultcontentstore.PlansClientBeginDeleteOptions) (resp azfake.PollerResponder[armcommvaultcontentstore.PlansClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method PlansClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, cloudAccountName string, planName string, options *armcommvaultcontentstore.PlansClientGetOptions) (resp azfake.Responder[armcommvaultcontentstore.PlansClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByCloudAccountPager is the fake for method PlansClient.NewListByCloudAccountPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByCloudAccountPager func(resourceGroupName string, cloudAccountName string, options *armcommvaultcontentstore.PlansClientListByCloudAccountOptions) (resp azfake.PagerResponder[armcommvaultcontentstore.PlansClientListByCloudAccountResponse])
}

// NewPlansServerTransport creates a new instance of PlansServerTransport with the provided implementation.
// The returned PlansServerTransport instance is connected to an instance of armcommvaultcontentstore.PlansClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewPlansServerTransport(srv *PlansServer) *PlansServerTransport {
	return &PlansServerTransport{
		srv:                        srv,
		beginCreateOrupdate:        newTracker[azfake.PollerResponder[armcommvaultcontentstore.PlansClientCreateOrupdateResponse]](),
		beginDelete:                newTracker[azfake.PollerResponder[armcommvaultcontentstore.PlansClientDeleteResponse]](),
		newListByCloudAccountPager: newTracker[azfake.PagerResponder[armcommvaultcontentstore.PlansClientListByCloudAccountResponse]](),
	}
}

// PlansServerTransport connects instances of armcommvaultcontentstore.PlansClient to instances of PlansServer.
// Don't use this type directly, use NewPlansServerTransport instead.
type PlansServerTransport struct {
	srv                        *PlansServer
	beginCreateOrupdate        *tracker[azfake.PollerResponder[armcommvaultcontentstore.PlansClientCreateOrupdateResponse]]
	beginDelete                *tracker[azfake.PollerResponder[armcommvaultcontentstore.PlansClientDeleteResponse]]
	newListByCloudAccountPager *tracker[azfake.PagerResponder[armcommvaultcontentstore.PlansClientListByCloudAccountResponse]]
}

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

func (p *PlansServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if plansServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = plansServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "PlansClient.BeginCreateOrupdate":
				res.resp, res.err = p.dispatchBeginCreateOrupdate(req)
			case "PlansClient.BeginDelete":
				res.resp, res.err = p.dispatchBeginDelete(req)
			case "PlansClient.Get":
				res.resp, res.err = p.dispatchGet(req)
			case "PlansClient.NewListByCloudAccountPager":
				res.resp, res.err = p.dispatchNewListByCloudAccountPager(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 (p *PlansServerTransport) dispatchBeginCreateOrupdate(req *http.Request) (*http.Response, error) {
	if p.srv.BeginCreateOrupdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrupdate not implemented")}
	}
	beginCreateOrupdate := p.beginCreateOrupdate.get(req)
	if beginCreateOrupdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Commvault\.ContentStore/cloudAccounts/(?P<cloudAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/plans/(?P<planName>[!#&$-;=?-\[\]_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)
		}
		body, err := server.UnmarshalRequestAsJSON[armcommvaultcontentstore.CommvaultPlan](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		cloudAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("cloudAccountName")])
		if err != nil {
			return nil, err
		}
		planNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("planName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := p.srv.BeginCreateOrupdate(req.Context(), resourceGroupNameParam, cloudAccountNameParam, planNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrupdate = &respr
		p.beginCreateOrupdate.add(req, beginCreateOrupdate)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
		p.beginCreateOrupdate.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginCreateOrupdate) {
		p.beginCreateOrupdate.remove(req)
	}

	return resp, nil
}

func (p *PlansServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if p.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := p.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Commvault\.ContentStore/cloudAccounts/(?P<cloudAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/plans/(?P<planName>[!#&$-;=?-\[\]_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)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		cloudAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("cloudAccountName")])
		if err != nil {
			return nil, err
		}
		planNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("planName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := p.srv.BeginDelete(req.Context(), resourceGroupNameParam, cloudAccountNameParam, planNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		p.beginDelete.add(req, beginDelete)
	}

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

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

	return resp, nil
}

func (p *PlansServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if p.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/Commvault\.ContentStore/cloudAccounts/(?P<cloudAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/plans/(?P<planName>[!#&$-;=?-\[\]_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)
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	cloudAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("cloudAccountName")])
	if err != nil {
		return nil, err
	}
	planNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("planName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.Get(req.Context(), resourceGroupNameParam, cloudAccountNameParam, planNameParam, 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).CommvaultPlan, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PlansServerTransport) dispatchNewListByCloudAccountPager(req *http.Request) (*http.Response, error) {
	if p.srv.NewListByCloudAccountPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByCloudAccountPager not implemented")}
	}
	newListByCloudAccountPager := p.newListByCloudAccountPager.get(req)
	if newListByCloudAccountPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Commvault\.ContentStore/cloudAccounts/(?P<cloudAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/plans`
		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
		}
		cloudAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("cloudAccountName")])
		if err != nil {
			return nil, err
		}
		resp := p.srv.NewListByCloudAccountPager(resourceGroupNameParam, cloudAccountNameParam, nil)
		newListByCloudAccountPager = &resp
		p.newListByCloudAccountPager.add(req, newListByCloudAccountPager)
		server.PagerResponderInjectNextLinks(newListByCloudAccountPager, req, func(page *armcommvaultcontentstore.PlansClientListByCloudAccountResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByCloudAccountPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		p.newListByCloudAccountPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByCloudAccountPager) {
		p.newListByCloudAccountPager.remove(req)
	}
	return resp, nil
}

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