// 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/resourcemanager/selfhelp/armselfhelp/v2"
	"net/http"
	"net/url"
	"reflect"
	"regexp"
	"slices"
)

// TroubleshootersServer is a fake server for instances of the armselfhelp.TroubleshootersClient type.
type TroubleshootersServer struct {
	// Continue is the fake for method TroubleshootersClient.Continue
	// HTTP status codes to indicate success: http.StatusNoContent
	Continue func(ctx context.Context, scope string, troubleshooterName string, options *armselfhelp.TroubleshootersClientContinueOptions) (resp azfake.Responder[armselfhelp.TroubleshootersClientContinueResponse], errResp azfake.ErrorResponder)

	// Create is the fake for method TroubleshootersClient.Create
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	Create func(ctx context.Context, scope string, troubleshooterName string, createTroubleshooterRequestBody armselfhelp.TroubleshooterResource, options *armselfhelp.TroubleshootersClientCreateOptions) (resp azfake.Responder[armselfhelp.TroubleshootersClientCreateResponse], errResp azfake.ErrorResponder)

	// End is the fake for method TroubleshootersClient.End
	// HTTP status codes to indicate success: http.StatusNoContent
	End func(ctx context.Context, scope string, troubleshooterName string, options *armselfhelp.TroubleshootersClientEndOptions) (resp azfake.Responder[armselfhelp.TroubleshootersClientEndResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method TroubleshootersClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, scope string, troubleshooterName string, options *armselfhelp.TroubleshootersClientGetOptions) (resp azfake.Responder[armselfhelp.TroubleshootersClientGetResponse], errResp azfake.ErrorResponder)

	// Restart is the fake for method TroubleshootersClient.Restart
	// HTTP status codes to indicate success: http.StatusOK
	Restart func(ctx context.Context, scope string, troubleshooterName string, options *armselfhelp.TroubleshootersClientRestartOptions) (resp azfake.Responder[armselfhelp.TroubleshootersClientRestartResponse], errResp azfake.ErrorResponder)
}

// NewTroubleshootersServerTransport creates a new instance of TroubleshootersServerTransport with the provided implementation.
// The returned TroubleshootersServerTransport instance is connected to an instance of armselfhelp.TroubleshootersClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTroubleshootersServerTransport(srv *TroubleshootersServer) *TroubleshootersServerTransport {
	return &TroubleshootersServerTransport{srv: srv}
}

// TroubleshootersServerTransport connects instances of armselfhelp.TroubleshootersClient to instances of TroubleshootersServer.
// Don't use this type directly, use NewTroubleshootersServerTransport instead.
type TroubleshootersServerTransport struct {
	srv *TroubleshootersServer
}

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

func (t *TroubleshootersServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if troubleshootersServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = troubleshootersServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "TroubleshootersClient.Continue":
				res.resp, res.err = t.dispatchContinue(req)
			case "TroubleshootersClient.Create":
				res.resp, res.err = t.dispatchCreate(req)
			case "TroubleshootersClient.End":
				res.resp, res.err = t.dispatchEnd(req)
			case "TroubleshootersClient.Get":
				res.resp, res.err = t.dispatchGet(req)
			case "TroubleshootersClient.Restart":
				res.resp, res.err = t.dispatchRestart(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 (t *TroubleshootersServerTransport) dispatchContinue(req *http.Request) (*http.Response, error) {
	if t.srv.Continue == nil {
		return nil, &nonRetriableError{errors.New("fake for method Continue not implemented")}
	}
	const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Help/troubleshooters/(?P<troubleshooterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/continue`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armselfhelp.ContinueRequestBody](req)
	if err != nil {
		return nil, err
	}
	scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
	if err != nil {
		return nil, err
	}
	troubleshooterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("troubleshooterName")])
	if err != nil {
		return nil, err
	}
	var options *armselfhelp.TroubleshootersClientContinueOptions
	if !reflect.ValueOf(body).IsZero() {
		options = &armselfhelp.TroubleshootersClientContinueOptions{
			ContinueRequestBody: &body,
		}
	}
	respr, errRespr := t.srv.Continue(req.Context(), scopeParam, troubleshooterNameParam, options)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	if val := server.GetResponse(respr).Location; val != nil {
		resp.Header.Set("location", *val)
	}
	return resp, nil
}

func (t *TroubleshootersServerTransport) dispatchCreate(req *http.Request) (*http.Response, error) {
	if t.srv.Create == nil {
		return nil, &nonRetriableError{errors.New("fake for method Create not implemented")}
	}
	const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Help/troubleshooters/(?P<troubleshooterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armselfhelp.TroubleshooterResource](req)
	if err != nil {
		return nil, err
	}
	scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
	if err != nil {
		return nil, err
	}
	troubleshooterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("troubleshooterName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Create(req.Context(), scopeParam, troubleshooterNameParam, body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).TroubleshooterResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TroubleshootersServerTransport) dispatchEnd(req *http.Request) (*http.Response, error) {
	if t.srv.End == nil {
		return nil, &nonRetriableError{errors.New("fake for method End not implemented")}
	}
	const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Help/troubleshooters/(?P<troubleshooterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/end`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
	if err != nil {
		return nil, err
	}
	troubleshooterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("troubleshooterName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.End(req.Context(), scopeParam, troubleshooterNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	if val := server.GetResponse(respr).Location; val != nil {
		resp.Header.Set("location", *val)
	}
	return resp, nil
}

func (t *TroubleshootersServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if t.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Help/troubleshooters/(?P<troubleshooterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
	if err != nil {
		return nil, err
	}
	troubleshooterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("troubleshooterName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Get(req.Context(), scopeParam, troubleshooterNameParam, 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).TroubleshooterResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TroubleshootersServerTransport) dispatchRestart(req *http.Request) (*http.Response, error) {
	if t.srv.Restart == nil {
		return nil, &nonRetriableError{errors.New("fake for method Restart not implemented")}
	}
	const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Help/troubleshooters/(?P<troubleshooterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/restart`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
	if err != nil {
		return nil, err
	}
	troubleshooterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("troubleshooterName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Restart(req.Context(), scopeParam, troubleshooterNameParam, 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).RestartTroubleshooterResponse, req)
	if err != nil {
		return nil, err
	}
	if val := server.GetResponse(respr).Location; val != nil {
		resp.Header.Set("location", *val)
	}
	return resp, nil
}

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