// 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 armpostgresqlflexibleservers.ClientFactory type.
type ServerFactory struct {
	// AdministratorsMicrosoftEntraServer contains the fakes for client AdministratorsMicrosoftEntraClient
	AdministratorsMicrosoftEntraServer AdministratorsMicrosoftEntraServer

	// AdvancedThreatProtectionSettingsServer contains the fakes for client AdvancedThreatProtectionSettingsClient
	AdvancedThreatProtectionSettingsServer AdvancedThreatProtectionSettingsServer

	// BackupsAutomaticAndOnDemandServer contains the fakes for client BackupsAutomaticAndOnDemandClient
	BackupsAutomaticAndOnDemandServer BackupsAutomaticAndOnDemandServer

	// BackupsLongTermRetentionServer contains the fakes for client BackupsLongTermRetentionClient
	BackupsLongTermRetentionServer BackupsLongTermRetentionServer

	// CapabilitiesByLocationServer contains the fakes for client CapabilitiesByLocationClient
	CapabilitiesByLocationServer CapabilitiesByLocationServer

	// CapabilitiesByServerServer contains the fakes for client CapabilitiesByServerClient
	CapabilitiesByServerServer CapabilitiesByServerServer

	// CapturedLogsServer contains the fakes for client CapturedLogsClient
	CapturedLogsServer CapturedLogsServer

	// ConfigurationsServer contains the fakes for client ConfigurationsClient
	ConfigurationsServer ConfigurationsServer

	// DatabasesServer contains the fakes for client DatabasesClient
	DatabasesServer DatabasesServer

	// FirewallRulesServer contains the fakes for client FirewallRulesClient
	FirewallRulesServer FirewallRulesServer

	// MaintenanceEventsServer contains the fakes for client MaintenanceEventsClient
	MaintenanceEventsServer MaintenanceEventsServer

	// MajorVersionUpgradePrecheckServer contains the fakes for client MajorVersionUpgradePrecheckClient
	MajorVersionUpgradePrecheckServer MajorVersionUpgradePrecheckServer

	// MigrationsServer contains the fakes for client MigrationsClient
	MigrationsServer MigrationsServer

	// NameAvailabilityServer contains the fakes for client NameAvailabilityClient
	NameAvailabilityServer NameAvailabilityServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// PrivateDNSZoneSuffixServer contains the fakes for client PrivateDNSZoneSuffixClient
	PrivateDNSZoneSuffixServer PrivateDNSZoneSuffixServer

	// PrivateEndpointConnectionsServer contains the fakes for client PrivateEndpointConnectionsClient
	PrivateEndpointConnectionsServer PrivateEndpointConnectionsServer

	// PrivateLinkResourcesServer contains the fakes for client PrivateLinkResourcesClient
	PrivateLinkResourcesServer PrivateLinkResourcesServer

	// QuotaUsagesServer contains the fakes for client QuotaUsagesClient
	QuotaUsagesServer QuotaUsagesServer

	// ReplicasServer contains the fakes for client ReplicasClient
	ReplicasServer ReplicasServer

	// ServerThreatProtectionSettingsServer contains the fakes for client ServerThreatProtectionSettingsClient
	ServerThreatProtectionSettingsServer ServerThreatProtectionSettingsServer

	// ServersServer contains the fakes for client ServersClient
	ServersServer ServersServer

	// TuningOptionsServer contains the fakes for client TuningOptionsClient
	TuningOptionsServer TuningOptionsServer

	// VirtualEndpointsServer contains the fakes for client VirtualEndpointsClient
	VirtualEndpointsServer VirtualEndpointsServer

	// VirtualNetworkSubnetUsageServer contains the fakes for client VirtualNetworkSubnetUsageClient
	VirtualNetworkSubnetUsageServer VirtualNetworkSubnetUsageServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armpostgresqlflexibleservers.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 armpostgresqlflexibleservers.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                                      *ServerFactory
	trMu                                     sync.Mutex
	trAdministratorsMicrosoftEntraServer     *AdministratorsMicrosoftEntraServerTransport
	trAdvancedThreatProtectionSettingsServer *AdvancedThreatProtectionSettingsServerTransport
	trBackupsAutomaticAndOnDemandServer      *BackupsAutomaticAndOnDemandServerTransport
	trBackupsLongTermRetentionServer         *BackupsLongTermRetentionServerTransport
	trCapabilitiesByLocationServer           *CapabilitiesByLocationServerTransport
	trCapabilitiesByServerServer             *CapabilitiesByServerServerTransport
	trCapturedLogsServer                     *CapturedLogsServerTransport
	trConfigurationsServer                   *ConfigurationsServerTransport
	trDatabasesServer                        *DatabasesServerTransport
	trFirewallRulesServer                    *FirewallRulesServerTransport
	trMaintenanceEventsServer                *MaintenanceEventsServerTransport
	trMajorVersionUpgradePrecheckServer      *MajorVersionUpgradePrecheckServerTransport
	trMigrationsServer                       *MigrationsServerTransport
	trNameAvailabilityServer                 *NameAvailabilityServerTransport
	trOperationsServer                       *OperationsServerTransport
	trPrivateDNSZoneSuffixServer             *PrivateDNSZoneSuffixServerTransport
	trPrivateEndpointConnectionsServer       *PrivateEndpointConnectionsServerTransport
	trPrivateLinkResourcesServer             *PrivateLinkResourcesServerTransport
	trQuotaUsagesServer                      *QuotaUsagesServerTransport
	trReplicasServer                         *ReplicasServerTransport
	trServerThreatProtectionSettingsServer   *ServerThreatProtectionSettingsServerTransport
	trServersServer                          *ServersServerTransport
	trTuningOptionsServer                    *TuningOptionsServerTransport
	trVirtualEndpointsServer                 *VirtualEndpointsServerTransport
	trVirtualNetworkSubnetUsageServer        *VirtualNetworkSubnetUsageServerTransport
}

// 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 "AdministratorsMicrosoftEntraClient":
		initServer(&s.trMu, &s.trAdministratorsMicrosoftEntraServer, func() *AdministratorsMicrosoftEntraServerTransport {
			return NewAdministratorsMicrosoftEntraServerTransport(&s.srv.AdministratorsMicrosoftEntraServer)
		})
		resp, err = s.trAdministratorsMicrosoftEntraServer.Do(req)
	case "AdvancedThreatProtectionSettingsClient":
		initServer(&s.trMu, &s.trAdvancedThreatProtectionSettingsServer, func() *AdvancedThreatProtectionSettingsServerTransport {
			return NewAdvancedThreatProtectionSettingsServerTransport(&s.srv.AdvancedThreatProtectionSettingsServer)
		})
		resp, err = s.trAdvancedThreatProtectionSettingsServer.Do(req)
	case "BackupsAutomaticAndOnDemandClient":
		initServer(&s.trMu, &s.trBackupsAutomaticAndOnDemandServer, func() *BackupsAutomaticAndOnDemandServerTransport {
			return NewBackupsAutomaticAndOnDemandServerTransport(&s.srv.BackupsAutomaticAndOnDemandServer)
		})
		resp, err = s.trBackupsAutomaticAndOnDemandServer.Do(req)
	case "BackupsLongTermRetentionClient":
		initServer(&s.trMu, &s.trBackupsLongTermRetentionServer, func() *BackupsLongTermRetentionServerTransport {
			return NewBackupsLongTermRetentionServerTransport(&s.srv.BackupsLongTermRetentionServer)
		})
		resp, err = s.trBackupsLongTermRetentionServer.Do(req)
	case "CapabilitiesByLocationClient":
		initServer(&s.trMu, &s.trCapabilitiesByLocationServer, func() *CapabilitiesByLocationServerTransport {
			return NewCapabilitiesByLocationServerTransport(&s.srv.CapabilitiesByLocationServer)
		})
		resp, err = s.trCapabilitiesByLocationServer.Do(req)
	case "CapabilitiesByServerClient":
		initServer(&s.trMu, &s.trCapabilitiesByServerServer, func() *CapabilitiesByServerServerTransport {
			return NewCapabilitiesByServerServerTransport(&s.srv.CapabilitiesByServerServer)
		})
		resp, err = s.trCapabilitiesByServerServer.Do(req)
	case "CapturedLogsClient":
		initServer(&s.trMu, &s.trCapturedLogsServer, func() *CapturedLogsServerTransport { return NewCapturedLogsServerTransport(&s.srv.CapturedLogsServer) })
		resp, err = s.trCapturedLogsServer.Do(req)
	case "ConfigurationsClient":
		initServer(&s.trMu, &s.trConfigurationsServer, func() *ConfigurationsServerTransport {
			return NewConfigurationsServerTransport(&s.srv.ConfigurationsServer)
		})
		resp, err = s.trConfigurationsServer.Do(req)
	case "DatabasesClient":
		initServer(&s.trMu, &s.trDatabasesServer, func() *DatabasesServerTransport { return NewDatabasesServerTransport(&s.srv.DatabasesServer) })
		resp, err = s.trDatabasesServer.Do(req)
	case "FirewallRulesClient":
		initServer(&s.trMu, &s.trFirewallRulesServer, func() *FirewallRulesServerTransport {
			return NewFirewallRulesServerTransport(&s.srv.FirewallRulesServer)
		})
		resp, err = s.trFirewallRulesServer.Do(req)
	case "MaintenanceEventsClient":
		initServer(&s.trMu, &s.trMaintenanceEventsServer, func() *MaintenanceEventsServerTransport {
			return NewMaintenanceEventsServerTransport(&s.srv.MaintenanceEventsServer)
		})
		resp, err = s.trMaintenanceEventsServer.Do(req)
	case "MajorVersionUpgradePrecheckClient":
		initServer(&s.trMu, &s.trMajorVersionUpgradePrecheckServer, func() *MajorVersionUpgradePrecheckServerTransport {
			return NewMajorVersionUpgradePrecheckServerTransport(&s.srv.MajorVersionUpgradePrecheckServer)
		})
		resp, err = s.trMajorVersionUpgradePrecheckServer.Do(req)
	case "MigrationsClient":
		initServer(&s.trMu, &s.trMigrationsServer, func() *MigrationsServerTransport { return NewMigrationsServerTransport(&s.srv.MigrationsServer) })
		resp, err = s.trMigrationsServer.Do(req)
	case "NameAvailabilityClient":
		initServer(&s.trMu, &s.trNameAvailabilityServer, func() *NameAvailabilityServerTransport {
			return NewNameAvailabilityServerTransport(&s.srv.NameAvailabilityServer)
		})
		resp, err = s.trNameAvailabilityServer.Do(req)
	case "OperationsClient":
		initServer(&s.trMu, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "PrivateDNSZoneSuffixClient":
		initServer(&s.trMu, &s.trPrivateDNSZoneSuffixServer, func() *PrivateDNSZoneSuffixServerTransport {
			return NewPrivateDNSZoneSuffixServerTransport(&s.srv.PrivateDNSZoneSuffixServer)
		})
		resp, err = s.trPrivateDNSZoneSuffixServer.Do(req)
	case "PrivateEndpointConnectionsClient":
		initServer(&s.trMu, &s.trPrivateEndpointConnectionsServer, func() *PrivateEndpointConnectionsServerTransport {
			return NewPrivateEndpointConnectionsServerTransport(&s.srv.PrivateEndpointConnectionsServer)
		})
		resp, err = s.trPrivateEndpointConnectionsServer.Do(req)
	case "PrivateLinkResourcesClient":
		initServer(&s.trMu, &s.trPrivateLinkResourcesServer, func() *PrivateLinkResourcesServerTransport {
			return NewPrivateLinkResourcesServerTransport(&s.srv.PrivateLinkResourcesServer)
		})
		resp, err = s.trPrivateLinkResourcesServer.Do(req)
	case "QuotaUsagesClient":
		initServer(&s.trMu, &s.trQuotaUsagesServer, func() *QuotaUsagesServerTransport { return NewQuotaUsagesServerTransport(&s.srv.QuotaUsagesServer) })
		resp, err = s.trQuotaUsagesServer.Do(req)
	case "ReplicasClient":
		initServer(&s.trMu, &s.trReplicasServer, func() *ReplicasServerTransport { return NewReplicasServerTransport(&s.srv.ReplicasServer) })
		resp, err = s.trReplicasServer.Do(req)
	case "ServerThreatProtectionSettingsClient":
		initServer(&s.trMu, &s.trServerThreatProtectionSettingsServer, func() *ServerThreatProtectionSettingsServerTransport {
			return NewServerThreatProtectionSettingsServerTransport(&s.srv.ServerThreatProtectionSettingsServer)
		})
		resp, err = s.trServerThreatProtectionSettingsServer.Do(req)
	case "ServersClient":
		initServer(&s.trMu, &s.trServersServer, func() *ServersServerTransport { return NewServersServerTransport(&s.srv.ServersServer) })
		resp, err = s.trServersServer.Do(req)
	case "TuningOptionsClient":
		initServer(&s.trMu, &s.trTuningOptionsServer, func() *TuningOptionsServerTransport {
			return NewTuningOptionsServerTransport(&s.srv.TuningOptionsServer)
		})
		resp, err = s.trTuningOptionsServer.Do(req)
	case "VirtualEndpointsClient":
		initServer(&s.trMu, &s.trVirtualEndpointsServer, func() *VirtualEndpointsServerTransport {
			return NewVirtualEndpointsServerTransport(&s.srv.VirtualEndpointsServer)
		})
		resp, err = s.trVirtualEndpointsServer.Do(req)
	case "VirtualNetworkSubnetUsageClient":
		initServer(&s.trMu, &s.trVirtualNetworkSubnetUsageServer, func() *VirtualNetworkSubnetUsageServerTransport {
			return NewVirtualNetworkSubnetUsageServerTransport(&s.srv.VirtualNetworkSubnetUsageServer)
		})
		resp, err = s.trVirtualNetworkSubnetUsageServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}
