// 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/security/keyvault/azadmin/ekm"
	"net/http"
	"slices"
)

// KeyVaultServer is a fake server for instances of the ekm.KeyVaultClient type.
type KeyVaultServer struct {
	// CheckEkmConnection is the fake for method KeyVaultClient.CheckEkmConnection
	// HTTP status codes to indicate success: http.StatusOK
	CheckEkmConnection func(ctx context.Context, options *ekm.CheckEkmConnectionOptions) (resp azfake.Responder[ekm.CheckEkmConnectionResponse], errResp azfake.ErrorResponder)

	// CreateEkmConnection is the fake for method KeyVaultClient.CreateEkmConnection
	// HTTP status codes to indicate success: http.StatusOK
	CreateEkmConnection func(ctx context.Context, ekmConnection ekm.Connection, options *ekm.CreateEkmConnectionOptions) (resp azfake.Responder[ekm.CreateEkmConnectionResponse], errResp azfake.ErrorResponder)

	// DeleteEkmConnection is the fake for method KeyVaultClient.DeleteEkmConnection
	// HTTP status codes to indicate success: http.StatusOK
	DeleteEkmConnection func(ctx context.Context, options *ekm.DeleteEkmConnectionOptions) (resp azfake.Responder[ekm.DeleteEkmConnectionResponse], errResp azfake.ErrorResponder)

	// GetEkmCertificate is the fake for method KeyVaultClient.GetEkmCertificate
	// HTTP status codes to indicate success: http.StatusOK
	GetEkmCertificate func(ctx context.Context, options *ekm.GetEkmCertificateOptions) (resp azfake.Responder[ekm.GetEkmCertificateResponse], errResp azfake.ErrorResponder)

	// GetEkmConnection is the fake for method KeyVaultClient.GetEkmConnection
	// HTTP status codes to indicate success: http.StatusOK
	GetEkmConnection func(ctx context.Context, options *ekm.GetEkmConnectionOptions) (resp azfake.Responder[ekm.GetEkmConnectionResponse], errResp azfake.ErrorResponder)

	// UpdateEkmConnection is the fake for method KeyVaultClient.UpdateEkmConnection
	// HTTP status codes to indicate success: http.StatusOK
	UpdateEkmConnection func(ctx context.Context, ekmConnection ekm.Connection, options *ekm.UpdateEkmConnectionOptions) (resp azfake.Responder[ekm.UpdateEkmConnectionResponse], errResp azfake.ErrorResponder)
}

// NewKeyVaultServerTransport creates a new instance of KeyVaultServerTransport with the provided implementation.
// The returned KeyVaultServerTransport instance is connected to an instance of ekm.KeyVaultClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewKeyVaultServerTransport(srv *KeyVaultServer) *KeyVaultServerTransport {
	return &KeyVaultServerTransport{srv: srv}
}

// KeyVaultServerTransport connects instances of ekm.KeyVaultClient to instances of KeyVaultServer.
// Don't use this type directly, use NewKeyVaultServerTransport instead.
type KeyVaultServerTransport struct {
	srv *KeyVaultServer
}

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

func (k *KeyVaultServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if keyVaultServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = keyVaultServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "KeyVaultClient.CheckEkmConnection":
				res.resp, res.err = k.dispatchCheckEkmConnection(req)
			case "KeyVaultClient.CreateEkmConnection":
				res.resp, res.err = k.dispatchCreateEkmConnection(req)
			case "KeyVaultClient.DeleteEkmConnection":
				res.resp, res.err = k.dispatchDeleteEkmConnection(req)
			case "KeyVaultClient.GetEkmCertificate":
				res.resp, res.err = k.dispatchGetEkmCertificate(req)
			case "KeyVaultClient.GetEkmConnection":
				res.resp, res.err = k.dispatchGetEkmConnection(req)
			case "KeyVaultClient.UpdateEkmConnection":
				res.resp, res.err = k.dispatchUpdateEkmConnection(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 (k *KeyVaultServerTransport) dispatchCheckEkmConnection(req *http.Request) (*http.Response, error) {
	if k.srv.CheckEkmConnection == nil {
		return nil, &nonRetriableError{errors.New("fake for method CheckEkmConnection not implemented")}
	}
	respr, errRespr := k.srv.CheckEkmConnection(req.Context(), 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).ProxyInfo, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (k *KeyVaultServerTransport) dispatchCreateEkmConnection(req *http.Request) (*http.Response, error) {
	if k.srv.CreateEkmConnection == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateEkmConnection not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[ekm.Connection](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := k.srv.CreateEkmConnection(req.Context(), body, 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).Connection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (k *KeyVaultServerTransport) dispatchDeleteEkmConnection(req *http.Request) (*http.Response, error) {
	if k.srv.DeleteEkmConnection == nil {
		return nil, &nonRetriableError{errors.New("fake for method DeleteEkmConnection not implemented")}
	}
	respr, errRespr := k.srv.DeleteEkmConnection(req.Context(), 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).Connection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (k *KeyVaultServerTransport) dispatchGetEkmCertificate(req *http.Request) (*http.Response, error) {
	if k.srv.GetEkmCertificate == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetEkmCertificate not implemented")}
	}
	respr, errRespr := k.srv.GetEkmCertificate(req.Context(), 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).ProxyClientCertificateInfo, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (k *KeyVaultServerTransport) dispatchGetEkmConnection(req *http.Request) (*http.Response, error) {
	if k.srv.GetEkmConnection == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetEkmConnection not implemented")}
	}
	respr, errRespr := k.srv.GetEkmConnection(req.Context(), 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).Connection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (k *KeyVaultServerTransport) dispatchUpdateEkmConnection(req *http.Request) (*http.Response, error) {
	if k.srv.UpdateEkmConnection == nil {
		return nil, &nonRetriableError{errors.New("fake for method UpdateEkmConnection not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[ekm.Connection](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := k.srv.UpdateEkmConnection(req.Context(), body, 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).Connection, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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