//go:build go1.18
// +build go1.18

// 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/machinelearning/armmachinelearning/v4"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
)

// FeaturesServer is a fake server for instances of the armmachinelearning.FeaturesClient type.
type FeaturesServer struct {
	// Get is the fake for method FeaturesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, workspaceName string, featuresetName string, featuresetVersion string, featureName string, options *armmachinelearning.FeaturesClientGetOptions) (resp azfake.Responder[armmachinelearning.FeaturesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method FeaturesClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceGroupName string, workspaceName string, featuresetName string, featuresetVersion string, options *armmachinelearning.FeaturesClientListOptions) (resp azfake.PagerResponder[armmachinelearning.FeaturesClientListResponse])
}

// NewFeaturesServerTransport creates a new instance of FeaturesServerTransport with the provided implementation.
// The returned FeaturesServerTransport instance is connected to an instance of armmachinelearning.FeaturesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewFeaturesServerTransport(srv *FeaturesServer) *FeaturesServerTransport {
	return &FeaturesServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armmachinelearning.FeaturesClientListResponse]](),
	}
}

// FeaturesServerTransport connects instances of armmachinelearning.FeaturesClient to instances of FeaturesServer.
// Don't use this type directly, use NewFeaturesServerTransport instead.
type FeaturesServerTransport struct {
	srv          *FeaturesServer
	newListPager *tracker[azfake.PagerResponder[armmachinelearning.FeaturesClientListResponse]]
}

// Do implements the policy.Transporter interface for FeaturesServerTransport.
func (f *FeaturesServerTransport) 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")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "FeaturesClient.Get":
		resp, err = f.dispatchGet(req)
	case "FeaturesClient.NewListPager":
		resp, err = f.dispatchNewListPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (f *FeaturesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if f.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\.MachineLearningServices/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/featuresets/(?P<featuresetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<featuresetVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/features/(?P<featureName>[!#&$-;=?-\[\]_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
	}
	workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
	if err != nil {
		return nil, err
	}
	featuresetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("featuresetName")])
	if err != nil {
		return nil, err
	}
	featuresetVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("featuresetVersion")])
	if err != nil {
		return nil, err
	}
	featureNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("featureName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := f.srv.Get(req.Context(), resourceGroupNameParam, workspaceNameParam, featuresetNameParam, featuresetVersionParam, featureNameParam, 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).Feature, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (f *FeaturesServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if f.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := f.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.MachineLearningServices/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/featuresets/(?P<featuresetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<featuresetVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/features`
		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)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
		if err != nil {
			return nil, err
		}
		featuresetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("featuresetName")])
		if err != nil {
			return nil, err
		}
		featuresetVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("featuresetVersion")])
		if err != nil {
			return nil, err
		}
		skipUnescaped, err := url.QueryUnescape(qp.Get("$skip"))
		if err != nil {
			return nil, err
		}
		skipParam := getOptional(skipUnescaped)
		tagsUnescaped, err := url.QueryUnescape(qp.Get("tags"))
		if err != nil {
			return nil, err
		}
		tagsParam := getOptional(tagsUnescaped)
		featureNameUnescaped, err := url.QueryUnescape(qp.Get("featureName"))
		if err != nil {
			return nil, err
		}
		featureNameParam := getOptional(featureNameUnescaped)
		descriptionUnescaped, err := url.QueryUnescape(qp.Get("description"))
		if err != nil {
			return nil, err
		}
		descriptionParam := getOptional(descriptionUnescaped)
		listViewTypeUnescaped, err := url.QueryUnescape(qp.Get("listViewType"))
		if err != nil {
			return nil, err
		}
		listViewTypeParam := getOptional(armmachinelearning.ListViewType(listViewTypeUnescaped))
		pageSizeUnescaped, err := url.QueryUnescape(qp.Get("pageSize"))
		if err != nil {
			return nil, err
		}
		pageSizeParam, err := parseOptional(pageSizeUnescaped, func(v string) (int32, error) {
			p, parseErr := strconv.ParseInt(v, 10, 32)
			if parseErr != nil {
				return 0, parseErr
			}
			return int32(p), nil
		})
		if err != nil {
			return nil, err
		}
		var options *armmachinelearning.FeaturesClientListOptions
		if skipParam != nil || tagsParam != nil || featureNameParam != nil || descriptionParam != nil || listViewTypeParam != nil || pageSizeParam != nil {
			options = &armmachinelearning.FeaturesClientListOptions{
				Skip:         skipParam,
				Tags:         tagsParam,
				FeatureName:  featureNameParam,
				Description:  descriptionParam,
				ListViewType: listViewTypeParam,
				PageSize:     pageSizeParam,
			}
		}
		resp := f.srv.NewListPager(resourceGroupNameParam, workspaceNameParam, featuresetNameParam, featuresetVersionParam, options)
		newListPager = &resp
		f.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armmachinelearning.FeaturesClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		f.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		f.newListPager.remove(req)
	}
	return resp, nil
}
