// 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/appcontainers/armappcontainers/v4"
	"net/http"
	"net/url"
	"regexp"
)

// ContainerAppsLabelHistoryServer is a fake server for instances of the armappcontainers.ContainerAppsLabelHistoryClient type.
type ContainerAppsLabelHistoryServer struct {
	// DeleteLabelHistory is the fake for method ContainerAppsLabelHistoryClient.DeleteLabelHistory
	// HTTP status codes to indicate success: http.StatusOK, http.StatusNoContent
	DeleteLabelHistory func(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, options *armappcontainers.ContainerAppsLabelHistoryClientDeleteLabelHistoryOptions) (resp azfake.Responder[armappcontainers.ContainerAppsLabelHistoryClientDeleteLabelHistoryResponse], errResp azfake.ErrorResponder)

	// GetLabelHistory is the fake for method ContainerAppsLabelHistoryClient.GetLabelHistory
	// HTTP status codes to indicate success: http.StatusOK
	GetLabelHistory func(ctx context.Context, resourceGroupName string, containerAppName string, labelName string, options *armappcontainers.ContainerAppsLabelHistoryClientGetLabelHistoryOptions) (resp azfake.Responder[armappcontainers.ContainerAppsLabelHistoryClientGetLabelHistoryResponse], errResp azfake.ErrorResponder)

	// NewListLabelHistoryPager is the fake for method ContainerAppsLabelHistoryClient.NewListLabelHistoryPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListLabelHistoryPager func(resourceGroupName string, containerAppName string, options *armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryOptions) (resp azfake.PagerResponder[armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryResponse])
}

// NewContainerAppsLabelHistoryServerTransport creates a new instance of ContainerAppsLabelHistoryServerTransport with the provided implementation.
// The returned ContainerAppsLabelHistoryServerTransport instance is connected to an instance of armappcontainers.ContainerAppsLabelHistoryClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewContainerAppsLabelHistoryServerTransport(srv *ContainerAppsLabelHistoryServer) *ContainerAppsLabelHistoryServerTransport {
	return &ContainerAppsLabelHistoryServerTransport{
		srv:                      srv,
		newListLabelHistoryPager: newTracker[azfake.PagerResponder[armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryResponse]](),
	}
}

// ContainerAppsLabelHistoryServerTransport connects instances of armappcontainers.ContainerAppsLabelHistoryClient to instances of ContainerAppsLabelHistoryServer.
// Don't use this type directly, use NewContainerAppsLabelHistoryServerTransport instead.
type ContainerAppsLabelHistoryServerTransport struct {
	srv                      *ContainerAppsLabelHistoryServer
	newListLabelHistoryPager *tracker[azfake.PagerResponder[armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryResponse]]
}

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

func (c *ContainerAppsLabelHistoryServerTransport) 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 containerAppsLabelHistoryServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = containerAppsLabelHistoryServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ContainerAppsLabelHistoryClient.DeleteLabelHistory":
				res.resp, res.err = c.dispatchDeleteLabelHistory(req)
			case "ContainerAppsLabelHistoryClient.GetLabelHistory":
				res.resp, res.err = c.dispatchGetLabelHistory(req)
			case "ContainerAppsLabelHistoryClient.NewListLabelHistoryPager":
				res.resp, res.err = c.dispatchNewListLabelHistoryPager(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 (c *ContainerAppsLabelHistoryServerTransport) dispatchDeleteLabelHistory(req *http.Request) (*http.Response, error) {
	if c.srv.DeleteLabelHistory == nil {
		return nil, &nonRetriableError{errors.New("fake for method DeleteLabelHistory not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.App/containerApps/(?P<containerAppName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/labelHistory/(?P<labelName>[!#&$-;=?-\[\]_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
	}
	containerAppNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("containerAppName")])
	if err != nil {
		return nil, err
	}
	labelNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("labelName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.DeleteLabelHistory(req.Context(), resourceGroupNameParam, containerAppNameParam, labelNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *ContainerAppsLabelHistoryServerTransport) dispatchGetLabelHistory(req *http.Request) (*http.Response, error) {
	if c.srv.GetLabelHistory == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetLabelHistory not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.App/containerApps/(?P<containerAppName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/labelHistory/(?P<labelName>[!#&$-;=?-\[\]_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
	}
	containerAppNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("containerAppName")])
	if err != nil {
		return nil, err
	}
	labelNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("labelName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.GetLabelHistory(req.Context(), resourceGroupNameParam, containerAppNameParam, labelNameParam, 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).LabelHistory, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *ContainerAppsLabelHistoryServerTransport) dispatchNewListLabelHistoryPager(req *http.Request) (*http.Response, error) {
	if c.srv.NewListLabelHistoryPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListLabelHistoryPager not implemented")}
	}
	newListLabelHistoryPager := c.newListLabelHistoryPager.get(req)
	if newListLabelHistoryPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.App/containerApps/(?P<containerAppName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/labelHistory`
		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)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		containerAppNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("containerAppName")])
		if err != nil {
			return nil, err
		}
		filterUnescaped, err := url.QueryUnescape(qp.Get("$filter"))
		if err != nil {
			return nil, err
		}
		filterParam := getOptional(filterUnescaped)
		var options *armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryOptions
		if filterParam != nil {
			options = &armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryOptions{
				Filter: filterParam,
			}
		}
		resp := c.srv.NewListLabelHistoryPager(resourceGroupNameParam, containerAppNameParam, options)
		newListLabelHistoryPager = &resp
		c.newListLabelHistoryPager.add(req, newListLabelHistoryPager)
		server.PagerResponderInjectNextLinks(newListLabelHistoryPager, req, func(page *armappcontainers.ContainerAppsLabelHistoryClientListLabelHistoryResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListLabelHistoryPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		c.newListLabelHistoryPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListLabelHistoryPager) {
		c.newListLabelHistoryPager.remove(req)
	}
	return resp, nil
}

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