// 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 armhybridconnectivity.ClientFactory type.
type ServerFactory struct {
	// EndpointsServer contains the fakes for client EndpointsClient
	EndpointsServer EndpointsServer

	// GenerateAwsTemplateServer contains the fakes for client GenerateAwsTemplateClient
	GenerateAwsTemplateServer GenerateAwsTemplateServer

	// InventoryServer contains the fakes for client InventoryClient
	InventoryServer InventoryServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// PublicCloudConnectorsServer contains the fakes for client PublicCloudConnectorsClient
	PublicCloudConnectorsServer PublicCloudConnectorsServer

	// ServiceConfigurationsServer contains the fakes for client ServiceConfigurationsClient
	ServiceConfigurationsServer ServiceConfigurationsServer

	// SolutionConfigurationsServer contains the fakes for client SolutionConfigurationsClient
	SolutionConfigurationsServer SolutionConfigurationsServer

	// SolutionTypesServer contains the fakes for client SolutionTypesClient
	SolutionTypesServer SolutionTypesServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armhybridconnectivity.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 armhybridconnectivity.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                            *ServerFactory
	trMu                           sync.Mutex
	trEndpointsServer              *EndpointsServerTransport
	trGenerateAwsTemplateServer    *GenerateAwsTemplateServerTransport
	trInventoryServer              *InventoryServerTransport
	trOperationsServer             *OperationsServerTransport
	trPublicCloudConnectorsServer  *PublicCloudConnectorsServerTransport
	trServiceConfigurationsServer  *ServiceConfigurationsServerTransport
	trSolutionConfigurationsServer *SolutionConfigurationsServerTransport
	trSolutionTypesServer          *SolutionTypesServerTransport
}

// 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 "EndpointsClient":
		initServer(s, &s.trEndpointsServer, func() *EndpointsServerTransport { return NewEndpointsServerTransport(&s.srv.EndpointsServer) })
		resp, err = s.trEndpointsServer.Do(req)
	case "GenerateAwsTemplateClient":
		initServer(s, &s.trGenerateAwsTemplateServer, func() *GenerateAwsTemplateServerTransport {
			return NewGenerateAwsTemplateServerTransport(&s.srv.GenerateAwsTemplateServer)
		})
		resp, err = s.trGenerateAwsTemplateServer.Do(req)
	case "InventoryClient":
		initServer(s, &s.trInventoryServer, func() *InventoryServerTransport { return NewInventoryServerTransport(&s.srv.InventoryServer) })
		resp, err = s.trInventoryServer.Do(req)
	case "OperationsClient":
		initServer(s, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "PublicCloudConnectorsClient":
		initServer(s, &s.trPublicCloudConnectorsServer, func() *PublicCloudConnectorsServerTransport {
			return NewPublicCloudConnectorsServerTransport(&s.srv.PublicCloudConnectorsServer)
		})
		resp, err = s.trPublicCloudConnectorsServer.Do(req)
	case "ServiceConfigurationsClient":
		initServer(s, &s.trServiceConfigurationsServer, func() *ServiceConfigurationsServerTransport {
			return NewServiceConfigurationsServerTransport(&s.srv.ServiceConfigurationsServer)
		})
		resp, err = s.trServiceConfigurationsServer.Do(req)
	case "SolutionConfigurationsClient":
		initServer(s, &s.trSolutionConfigurationsServer, func() *SolutionConfigurationsServerTransport {
			return NewSolutionConfigurationsServerTransport(&s.srv.SolutionConfigurationsServer)
		})
		resp, err = s.trSolutionConfigurationsServer.Do(req)
	case "SolutionTypesClient":
		initServer(s, &s.trSolutionTypesServer, func() *SolutionTypesServerTransport {
			return NewSolutionTypesServerTransport(&s.srv.SolutionTypesServer)
		})
		resp, err = s.trSolutionTypesServer.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()
}
