// 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 (
	"errors"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"strings"
	"sync"
)

// ServerFactory is a fake server for instances of the armrecoveryservicesdatareplication.ClientFactory type.
type ServerFactory struct {
	// CheckNameAvailabilityServer contains the fakes for client CheckNameAvailabilityClient
	CheckNameAvailabilityServer CheckNameAvailabilityServer

	// DeploymentPreflightServer contains the fakes for client DeploymentPreflightClient
	DeploymentPreflightServer DeploymentPreflightServer

	// EmailConfigurationServer contains the fakes for client EmailConfigurationClient
	EmailConfigurationServer EmailConfigurationServer

	// EventServer contains the fakes for client EventClient
	EventServer EventServer

	// FabricAgentServer contains the fakes for client FabricAgentClient
	FabricAgentServer FabricAgentServer

	// FabricServer contains the fakes for client FabricClient
	FabricServer FabricServer

	// JobServer contains the fakes for client JobClient
	JobServer JobServer

	// LocationBasedOperationResultsServer contains the fakes for client LocationBasedOperationResultsClient
	LocationBasedOperationResultsServer LocationBasedOperationResultsServer

	// OperationResultsServer contains the fakes for client OperationResultsClient
	OperationResultsServer OperationResultsServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// PolicyServer contains the fakes for client PolicyClient
	PolicyServer PolicyServer

	// PrivateEndpointConnectionProxiesServer contains the fakes for client PrivateEndpointConnectionProxiesClient
	PrivateEndpointConnectionProxiesServer PrivateEndpointConnectionProxiesServer

	// PrivateEndpointConnectionsServer contains the fakes for client PrivateEndpointConnectionsClient
	PrivateEndpointConnectionsServer PrivateEndpointConnectionsServer

	// PrivateLinkResourcesServer contains the fakes for client PrivateLinkResourcesClient
	PrivateLinkResourcesServer PrivateLinkResourcesServer

	// ProtectedItemServer contains the fakes for client ProtectedItemClient
	ProtectedItemServer ProtectedItemServer

	// RecoveryPointServer contains the fakes for client RecoveryPointClient
	RecoveryPointServer RecoveryPointServer

	// ReplicationExtensionServer contains the fakes for client ReplicationExtensionClient
	ReplicationExtensionServer ReplicationExtensionServer

	// VaultServer contains the fakes for client VaultClient
	VaultServer VaultServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armrecoveryservicesdatareplication.ClientFactory via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewServerFactoryTransport(srv *ServerFactory) *ServerFactoryTransport {
	return &ServerFactoryTransport{
		srv: srv,
	}
}

// ServerFactoryTransport connects instances of armrecoveryservicesdatareplication.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                                      *ServerFactory
	trMu                                     sync.Mutex
	trCheckNameAvailabilityServer            *CheckNameAvailabilityServerTransport
	trDeploymentPreflightServer              *DeploymentPreflightServerTransport
	trEmailConfigurationServer               *EmailConfigurationServerTransport
	trEventServer                            *EventServerTransport
	trFabricAgentServer                      *FabricAgentServerTransport
	trFabricServer                           *FabricServerTransport
	trJobServer                              *JobServerTransport
	trLocationBasedOperationResultsServer    *LocationBasedOperationResultsServerTransport
	trOperationResultsServer                 *OperationResultsServerTransport
	trOperationsServer                       *OperationsServerTransport
	trPolicyServer                           *PolicyServerTransport
	trPrivateEndpointConnectionProxiesServer *PrivateEndpointConnectionProxiesServerTransport
	trPrivateEndpointConnectionsServer       *PrivateEndpointConnectionsServerTransport
	trPrivateLinkResourcesServer             *PrivateLinkResourcesServerTransport
	trProtectedItemServer                    *ProtectedItemServerTransport
	trRecoveryPointServer                    *RecoveryPointServerTransport
	trReplicationExtensionServer             *ReplicationExtensionServerTransport
	trVaultServer                            *VaultServerTransport
}

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

	client := method[:strings.Index(method, ".")]
	var resp *http.Response
	var err error

	switch client {
	case "CheckNameAvailabilityClient":
		initServer(s, &s.trCheckNameAvailabilityServer, func() *CheckNameAvailabilityServerTransport {
			return NewCheckNameAvailabilityServerTransport(&s.srv.CheckNameAvailabilityServer)
		})
		resp, err = s.trCheckNameAvailabilityServer.Do(req)
	case "DeploymentPreflightClient":
		initServer(s, &s.trDeploymentPreflightServer, func() *DeploymentPreflightServerTransport {
			return NewDeploymentPreflightServerTransport(&s.srv.DeploymentPreflightServer)
		})
		resp, err = s.trDeploymentPreflightServer.Do(req)
	case "EmailConfigurationClient":
		initServer(s, &s.trEmailConfigurationServer, func() *EmailConfigurationServerTransport {
			return NewEmailConfigurationServerTransport(&s.srv.EmailConfigurationServer)
		})
		resp, err = s.trEmailConfigurationServer.Do(req)
	case "EventClient":
		initServer(s, &s.trEventServer, func() *EventServerTransport { return NewEventServerTransport(&s.srv.EventServer) })
		resp, err = s.trEventServer.Do(req)
	case "FabricAgentClient":
		initServer(s, &s.trFabricAgentServer, func() *FabricAgentServerTransport { return NewFabricAgentServerTransport(&s.srv.FabricAgentServer) })
		resp, err = s.trFabricAgentServer.Do(req)
	case "FabricClient":
		initServer(s, &s.trFabricServer, func() *FabricServerTransport { return NewFabricServerTransport(&s.srv.FabricServer) })
		resp, err = s.trFabricServer.Do(req)
	case "JobClient":
		initServer(s, &s.trJobServer, func() *JobServerTransport { return NewJobServerTransport(&s.srv.JobServer) })
		resp, err = s.trJobServer.Do(req)
	case "LocationBasedOperationResultsClient":
		initServer(s, &s.trLocationBasedOperationResultsServer, func() *LocationBasedOperationResultsServerTransport {
			return NewLocationBasedOperationResultsServerTransport(&s.srv.LocationBasedOperationResultsServer)
		})
		resp, err = s.trLocationBasedOperationResultsServer.Do(req)
	case "OperationResultsClient":
		initServer(s, &s.trOperationResultsServer, func() *OperationResultsServerTransport {
			return NewOperationResultsServerTransport(&s.srv.OperationResultsServer)
		})
		resp, err = s.trOperationResultsServer.Do(req)
	case "OperationsClient":
		initServer(s, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "PolicyClient":
		initServer(s, &s.trPolicyServer, func() *PolicyServerTransport { return NewPolicyServerTransport(&s.srv.PolicyServer) })
		resp, err = s.trPolicyServer.Do(req)
	case "PrivateEndpointConnectionProxiesClient":
		initServer(s, &s.trPrivateEndpointConnectionProxiesServer, func() *PrivateEndpointConnectionProxiesServerTransport {
			return NewPrivateEndpointConnectionProxiesServerTransport(&s.srv.PrivateEndpointConnectionProxiesServer)
		})
		resp, err = s.trPrivateEndpointConnectionProxiesServer.Do(req)
	case "PrivateEndpointConnectionsClient":
		initServer(s, &s.trPrivateEndpointConnectionsServer, func() *PrivateEndpointConnectionsServerTransport {
			return NewPrivateEndpointConnectionsServerTransport(&s.srv.PrivateEndpointConnectionsServer)
		})
		resp, err = s.trPrivateEndpointConnectionsServer.Do(req)
	case "PrivateLinkResourcesClient":
		initServer(s, &s.trPrivateLinkResourcesServer, func() *PrivateLinkResourcesServerTransport {
			return NewPrivateLinkResourcesServerTransport(&s.srv.PrivateLinkResourcesServer)
		})
		resp, err = s.trPrivateLinkResourcesServer.Do(req)
	case "ProtectedItemClient":
		initServer(s, &s.trProtectedItemServer, func() *ProtectedItemServerTransport {
			return NewProtectedItemServerTransport(&s.srv.ProtectedItemServer)
		})
		resp, err = s.trProtectedItemServer.Do(req)
	case "RecoveryPointClient":
		initServer(s, &s.trRecoveryPointServer, func() *RecoveryPointServerTransport {
			return NewRecoveryPointServerTransport(&s.srv.RecoveryPointServer)
		})
		resp, err = s.trRecoveryPointServer.Do(req)
	case "ReplicationExtensionClient":
		initServer(s, &s.trReplicationExtensionServer, func() *ReplicationExtensionServerTransport {
			return NewReplicationExtensionServerTransport(&s.srv.ReplicationExtensionServer)
		})
		resp, err = s.trReplicationExtensionServer.Do(req)
	case "VaultClient":
		initServer(s, &s.trVaultServer, func() *VaultServerTransport { return NewVaultServerTransport(&s.srv.VaultServer) })
		resp, err = s.trVaultServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func initServer[T any](s *ServerFactoryTransport, dst **T, src func() *T) {
	s.trMu.Lock()
	if *dst == nil {
		*dst = src()
	}
	s.trMu.Unlock()
}
