// 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 (
	"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 armdatamigration.ClientFactory type.
type ServerFactory struct {
	// DatabaseMigrationsMongoToCosmosDbRUMongoServer contains the fakes for client DatabaseMigrationsMongoToCosmosDbRUMongoClient
	DatabaseMigrationsMongoToCosmosDbRUMongoServer DatabaseMigrationsMongoToCosmosDbRUMongoServer

	// DatabaseMigrationsMongoToCosmosDbvCoreMongoServer contains the fakes for client DatabaseMigrationsMongoToCosmosDbvCoreMongoClient
	DatabaseMigrationsMongoToCosmosDbvCoreMongoServer DatabaseMigrationsMongoToCosmosDbvCoreMongoServer

	// DatabaseMigrationsSQLDbServer contains the fakes for client DatabaseMigrationsSQLDbClient
	DatabaseMigrationsSQLDbServer DatabaseMigrationsSQLDbServer

	// DatabaseMigrationsSQLMiServer contains the fakes for client DatabaseMigrationsSQLMiClient
	DatabaseMigrationsSQLMiServer DatabaseMigrationsSQLMiServer

	// DatabaseMigrationsSQLVMServer contains the fakes for client DatabaseMigrationsSQLVMClient
	DatabaseMigrationsSQLVMServer DatabaseMigrationsSQLVMServer

	// FilesServer contains the fakes for client FilesClient
	FilesServer FilesServer

	// MigrationServicesServer contains the fakes for client MigrationServicesClient
	MigrationServicesServer MigrationServicesServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// ProjectsServer contains the fakes for client ProjectsClient
	ProjectsServer ProjectsServer

	// ResourceSKUsServer contains the fakes for client ResourceSKUsClient
	ResourceSKUsServer ResourceSKUsServer

	// SQLMigrationServicesServer contains the fakes for client SQLMigrationServicesClient
	SQLMigrationServicesServer SQLMigrationServicesServer

	// ServiceTasksServer contains the fakes for client ServiceTasksClient
	ServiceTasksServer ServiceTasksServer

	// ServicesServer contains the fakes for client ServicesClient
	ServicesServer ServicesServer

	// TasksServer contains the fakes for client TasksClient
	TasksServer TasksServer

	// UsagesServer contains the fakes for client UsagesClient
	UsagesServer UsagesServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armdatamigration.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 armdatamigration.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                                                 *ServerFactory
	trMu                                                sync.Mutex
	trDatabaseMigrationsMongoToCosmosDbRUMongoServer    *DatabaseMigrationsMongoToCosmosDbRUMongoServerTransport
	trDatabaseMigrationsMongoToCosmosDbvCoreMongoServer *DatabaseMigrationsMongoToCosmosDbvCoreMongoServerTransport
	trDatabaseMigrationsSQLDbServer                     *DatabaseMigrationsSQLDbServerTransport
	trDatabaseMigrationsSQLMiServer                     *DatabaseMigrationsSQLMiServerTransport
	trDatabaseMigrationsSQLVMServer                     *DatabaseMigrationsSQLVMServerTransport
	trFilesServer                                       *FilesServerTransport
	trMigrationServicesServer                           *MigrationServicesServerTransport
	trOperationsServer                                  *OperationsServerTransport
	trProjectsServer                                    *ProjectsServerTransport
	trResourceSKUsServer                                *ResourceSKUsServerTransport
	trSQLMigrationServicesServer                        *SQLMigrationServicesServerTransport
	trServiceTasksServer                                *ServiceTasksServerTransport
	trServicesServer                                    *ServicesServerTransport
	trTasksServer                                       *TasksServerTransport
	trUsagesServer                                      *UsagesServerTransport
}

// 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 "DatabaseMigrationsMongoToCosmosDbRUMongoClient":
		initServer(s, &s.trDatabaseMigrationsMongoToCosmosDbRUMongoServer, func() *DatabaseMigrationsMongoToCosmosDbRUMongoServerTransport {
			return NewDatabaseMigrationsMongoToCosmosDbRUMongoServerTransport(&s.srv.DatabaseMigrationsMongoToCosmosDbRUMongoServer)
		})
		resp, err = s.trDatabaseMigrationsMongoToCosmosDbRUMongoServer.Do(req)
	case "DatabaseMigrationsMongoToCosmosDbvCoreMongoClient":
		initServer(s, &s.trDatabaseMigrationsMongoToCosmosDbvCoreMongoServer, func() *DatabaseMigrationsMongoToCosmosDbvCoreMongoServerTransport {
			return NewDatabaseMigrationsMongoToCosmosDbvCoreMongoServerTransport(&s.srv.DatabaseMigrationsMongoToCosmosDbvCoreMongoServer)
		})
		resp, err = s.trDatabaseMigrationsMongoToCosmosDbvCoreMongoServer.Do(req)
	case "DatabaseMigrationsSQLDbClient":
		initServer(s, &s.trDatabaseMigrationsSQLDbServer, func() *DatabaseMigrationsSQLDbServerTransport {
			return NewDatabaseMigrationsSQLDbServerTransport(&s.srv.DatabaseMigrationsSQLDbServer)
		})
		resp, err = s.trDatabaseMigrationsSQLDbServer.Do(req)
	case "DatabaseMigrationsSQLMiClient":
		initServer(s, &s.trDatabaseMigrationsSQLMiServer, func() *DatabaseMigrationsSQLMiServerTransport {
			return NewDatabaseMigrationsSQLMiServerTransport(&s.srv.DatabaseMigrationsSQLMiServer)
		})
		resp, err = s.trDatabaseMigrationsSQLMiServer.Do(req)
	case "DatabaseMigrationsSQLVMClient":
		initServer(s, &s.trDatabaseMigrationsSQLVMServer, func() *DatabaseMigrationsSQLVMServerTransport {
			return NewDatabaseMigrationsSQLVMServerTransport(&s.srv.DatabaseMigrationsSQLVMServer)
		})
		resp, err = s.trDatabaseMigrationsSQLVMServer.Do(req)
	case "FilesClient":
		initServer(s, &s.trFilesServer, func() *FilesServerTransport { return NewFilesServerTransport(&s.srv.FilesServer) })
		resp, err = s.trFilesServer.Do(req)
	case "MigrationServicesClient":
		initServer(s, &s.trMigrationServicesServer, func() *MigrationServicesServerTransport {
			return NewMigrationServicesServerTransport(&s.srv.MigrationServicesServer)
		})
		resp, err = s.trMigrationServicesServer.Do(req)
	case "OperationsClient":
		initServer(s, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "ProjectsClient":
		initServer(s, &s.trProjectsServer, func() *ProjectsServerTransport { return NewProjectsServerTransport(&s.srv.ProjectsServer) })
		resp, err = s.trProjectsServer.Do(req)
	case "ResourceSKUsClient":
		initServer(s, &s.trResourceSKUsServer, func() *ResourceSKUsServerTransport { return NewResourceSKUsServerTransport(&s.srv.ResourceSKUsServer) })
		resp, err = s.trResourceSKUsServer.Do(req)
	case "SQLMigrationServicesClient":
		initServer(s, &s.trSQLMigrationServicesServer, func() *SQLMigrationServicesServerTransport {
			return NewSQLMigrationServicesServerTransport(&s.srv.SQLMigrationServicesServer)
		})
		resp, err = s.trSQLMigrationServicesServer.Do(req)
	case "ServiceTasksClient":
		initServer(s, &s.trServiceTasksServer, func() *ServiceTasksServerTransport { return NewServiceTasksServerTransport(&s.srv.ServiceTasksServer) })
		resp, err = s.trServiceTasksServer.Do(req)
	case "ServicesClient":
		initServer(s, &s.trServicesServer, func() *ServicesServerTransport { return NewServicesServerTransport(&s.srv.ServicesServer) })
		resp, err = s.trServicesServer.Do(req)
	case "TasksClient":
		initServer(s, &s.trTasksServer, func() *TasksServerTransport { return NewTasksServerTransport(&s.srv.TasksServer) })
		resp, err = s.trTasksServer.Do(req)
	case "UsagesClient":
		initServer(s, &s.trUsagesServer, func() *UsagesServerTransport { return NewUsagesServerTransport(&s.srv.UsagesServer) })
		resp, err = s.trUsagesServer.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()
}
