//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/resourcemanager/botservice/armbotservice"
	"net/http"
	"net/url"
	"regexp"
)

// OperationResultsServer is a fake server for instances of the armbotservice.OperationResultsClient type.
type OperationResultsServer struct {
	// BeginGet is the fake for method OperationResultsClient.BeginGet
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginGet func(ctx context.Context, operationResultID string, options *armbotservice.OperationResultsClientBeginGetOptions) (resp azfake.PollerResponder[armbotservice.OperationResultsClientGetResponse], errResp azfake.ErrorResponder)
}

// NewOperationResultsServerTransport creates a new instance of OperationResultsServerTransport with the provided implementation.
// The returned OperationResultsServerTransport instance is connected to an instance of armbotservice.OperationResultsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewOperationResultsServerTransport(srv *OperationResultsServer) *OperationResultsServerTransport {
	return &OperationResultsServerTransport{
		srv:      srv,
		beginGet: newTracker[azfake.PollerResponder[armbotservice.OperationResultsClientGetResponse]](),
	}
}

// OperationResultsServerTransport connects instances of armbotservice.OperationResultsClient to instances of OperationResultsServer.
// Don't use this type directly, use NewOperationResultsServerTransport instead.
type OperationResultsServerTransport struct {
	srv      *OperationResultsServer
	beginGet *tracker[azfake.PollerResponder[armbotservice.OperationResultsClientGetResponse]]
}

// Do implements the policy.Transporter interface for OperationResultsServerTransport.
func (o *OperationResultsServerTransport) 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 "OperationResultsClient.BeginGet":
		resp, err = o.dispatchBeginGet(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (o *OperationResultsServerTransport) dispatchBeginGet(req *http.Request) (*http.Response, error) {
	if o.srv.BeginGet == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginGet not implemented")}
	}
	beginGet := o.beginGet.get(req)
	if beginGet == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BotService/operationresults/(?P<operationResultId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		operationResultIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("operationResultId")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := o.srv.BeginGet(req.Context(), operationResultIDParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginGet = &respr
		o.beginGet.add(req, beginGet)
	}

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

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		o.beginGet.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginGet) {
		o.beginGet.remove(req)
	}

	return resp, nil
}
