// 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 armadvisor.ClientFactory type.
type ServerFactory struct {
	// AssessmentTypesServer contains the fakes for client AssessmentTypesClient
	AssessmentTypesServer AssessmentTypesServer

	// AssessmentsServer contains the fakes for client AssessmentsClient
	AssessmentsServer AssessmentsServer

	// ConfigurationsServer contains the fakes for client ConfigurationsClient
	ConfigurationsServer ConfigurationsServer

	// ManagementServer contains the fakes for client ManagementClient
	ManagementServer ManagementServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// RecommendationMetadataServer contains the fakes for client RecommendationMetadataClient
	RecommendationMetadataServer RecommendationMetadataServer

	// RecommendationsServer contains the fakes for client RecommendationsClient
	RecommendationsServer RecommendationsServer

	// ResiliencyReviewsServer contains the fakes for client ResiliencyReviewsClient
	ResiliencyReviewsServer ResiliencyReviewsServer

	// ScoresServer contains the fakes for client ScoresClient
	ScoresServer ScoresServer

	// SuppressionsServer contains the fakes for client SuppressionsClient
	SuppressionsServer SuppressionsServer

	// TriageRecommendationsServer contains the fakes for client TriageRecommendationsClient
	TriageRecommendationsServer TriageRecommendationsServer

	// TriageResourcesServer contains the fakes for client TriageResourcesClient
	TriageResourcesServer TriageResourcesServer

	// WorkloadsServer contains the fakes for client WorkloadsClient
	WorkloadsServer WorkloadsServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armadvisor.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 armadvisor.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                            *ServerFactory
	trMu                           sync.Mutex
	trAssessmentTypesServer        *AssessmentTypesServerTransport
	trAssessmentsServer            *AssessmentsServerTransport
	trConfigurationsServer         *ConfigurationsServerTransport
	trManagementServer             *ManagementServerTransport
	trOperationsServer             *OperationsServerTransport
	trRecommendationMetadataServer *RecommendationMetadataServerTransport
	trRecommendationsServer        *RecommendationsServerTransport
	trResiliencyReviewsServer      *ResiliencyReviewsServerTransport
	trScoresServer                 *ScoresServerTransport
	trSuppressionsServer           *SuppressionsServerTransport
	trTriageRecommendationsServer  *TriageRecommendationsServerTransport
	trTriageResourcesServer        *TriageResourcesServerTransport
	trWorkloadsServer              *WorkloadsServerTransport
}

// 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 "AssessmentTypesClient":
		initServer(&s.trMu, &s.trAssessmentTypesServer, func() *AssessmentTypesServerTransport {
			return NewAssessmentTypesServerTransport(&s.srv.AssessmentTypesServer)
		})
		resp, err = s.trAssessmentTypesServer.Do(req)
	case "AssessmentsClient":
		initServer(&s.trMu, &s.trAssessmentsServer, func() *AssessmentsServerTransport { return NewAssessmentsServerTransport(&s.srv.AssessmentsServer) })
		resp, err = s.trAssessmentsServer.Do(req)
	case "ConfigurationsClient":
		initServer(&s.trMu, &s.trConfigurationsServer, func() *ConfigurationsServerTransport {
			return NewConfigurationsServerTransport(&s.srv.ConfigurationsServer)
		})
		resp, err = s.trConfigurationsServer.Do(req)
	case "ManagementClient":
		initServer(&s.trMu, &s.trManagementServer, func() *ManagementServerTransport { return NewManagementServerTransport(&s.srv.ManagementServer) })
		resp, err = s.trManagementServer.Do(req)
	case "OperationsClient":
		initServer(&s.trMu, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "RecommendationMetadataClient":
		initServer(&s.trMu, &s.trRecommendationMetadataServer, func() *RecommendationMetadataServerTransport {
			return NewRecommendationMetadataServerTransport(&s.srv.RecommendationMetadataServer)
		})
		resp, err = s.trRecommendationMetadataServer.Do(req)
	case "RecommendationsClient":
		initServer(&s.trMu, &s.trRecommendationsServer, func() *RecommendationsServerTransport {
			return NewRecommendationsServerTransport(&s.srv.RecommendationsServer)
		})
		resp, err = s.trRecommendationsServer.Do(req)
	case "ResiliencyReviewsClient":
		initServer(&s.trMu, &s.trResiliencyReviewsServer, func() *ResiliencyReviewsServerTransport {
			return NewResiliencyReviewsServerTransport(&s.srv.ResiliencyReviewsServer)
		})
		resp, err = s.trResiliencyReviewsServer.Do(req)
	case "ScoresClient":
		initServer(&s.trMu, &s.trScoresServer, func() *ScoresServerTransport { return NewScoresServerTransport(&s.srv.ScoresServer) })
		resp, err = s.trScoresServer.Do(req)
	case "SuppressionsClient":
		initServer(&s.trMu, &s.trSuppressionsServer, func() *SuppressionsServerTransport { return NewSuppressionsServerTransport(&s.srv.SuppressionsServer) })
		resp, err = s.trSuppressionsServer.Do(req)
	case "TriageRecommendationsClient":
		initServer(&s.trMu, &s.trTriageRecommendationsServer, func() *TriageRecommendationsServerTransport {
			return NewTriageRecommendationsServerTransport(&s.srv.TriageRecommendationsServer)
		})
		resp, err = s.trTriageRecommendationsServer.Do(req)
	case "TriageResourcesClient":
		initServer(&s.trMu, &s.trTriageResourcesServer, func() *TriageResourcesServerTransport {
			return NewTriageResourcesServerTransport(&s.srv.TriageResourcesServer)
		})
		resp, err = s.trTriageResourcesServer.Do(req)
	case "WorkloadsClient":
		initServer(&s.trMu, &s.trWorkloadsServer, func() *WorkloadsServerTransport { return NewWorkloadsServerTransport(&s.srv.WorkloadsServer) })
		resp, err = s.trWorkloadsServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}
