// 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 armbillingbenefits.ClientFactory type.
type ServerFactory struct {
	// ApplicableMaccsServer contains the fakes for client ApplicableMaccsClient
	ApplicableMaccsServer ApplicableMaccsServer

	// ConditionalCreditContributorsServer contains the fakes for client ConditionalCreditContributorsClient
	ConditionalCreditContributorsServer ConditionalCreditContributorsServer

	// ConditionalCreditsServer contains the fakes for client ConditionalCreditsClient
	ConditionalCreditsServer ConditionalCreditsServer

	// ContributorsServer contains the fakes for client ContributorsClient
	ContributorsServer ContributorsServer

	// CreditsServer contains the fakes for client CreditsClient
	CreditsServer CreditsServer

	// DiscountServer contains the fakes for client DiscountClient
	DiscountServer DiscountServer

	// DiscountsServer contains the fakes for client DiscountsClient
	DiscountsServer DiscountsServer

	// FreeServicesServer contains the fakes for client FreeServicesClient
	FreeServicesServer FreeServicesServer

	// MaccsServer contains the fakes for client MaccsClient
	MaccsServer MaccsServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// RPServer contains the fakes for client RPClient
	RPServer RPServer

	// ReservationOrderAliasServer contains the fakes for client ReservationOrderAliasClient
	ReservationOrderAliasServer ReservationOrderAliasServer

	// SavingsPlanServer contains the fakes for client SavingsPlanClient
	SavingsPlanServer SavingsPlanServer

	// SavingsPlanOrderAliasServer contains the fakes for client SavingsPlanOrderAliasClient
	SavingsPlanOrderAliasServer SavingsPlanOrderAliasServer

	// SavingsPlanOrderServer contains the fakes for client SavingsPlanOrderClient
	SavingsPlanOrderServer SavingsPlanOrderServer

	// SellerResourceServer contains the fakes for client SellerResourceClient
	SellerResourceServer SellerResourceServer

	// SourcesServer contains the fakes for client SourcesClient
	SourcesServer SourcesServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armbillingbenefits.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 armbillingbenefits.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                                   *ServerFactory
	trMu                                  sync.Mutex
	trApplicableMaccsServer               *ApplicableMaccsServerTransport
	trConditionalCreditContributorsServer *ConditionalCreditContributorsServerTransport
	trConditionalCreditsServer            *ConditionalCreditsServerTransport
	trContributorsServer                  *ContributorsServerTransport
	trCreditsServer                       *CreditsServerTransport
	trDiscountServer                      *DiscountServerTransport
	trDiscountsServer                     *DiscountsServerTransport
	trFreeServicesServer                  *FreeServicesServerTransport
	trMaccsServer                         *MaccsServerTransport
	trOperationsServer                    *OperationsServerTransport
	trRPServer                            *RPServerTransport
	trReservationOrderAliasServer         *ReservationOrderAliasServerTransport
	trSavingsPlanServer                   *SavingsPlanServerTransport
	trSavingsPlanOrderAliasServer         *SavingsPlanOrderAliasServerTransport
	trSavingsPlanOrderServer              *SavingsPlanOrderServerTransport
	trSellerResourceServer                *SellerResourceServerTransport
	trSourcesServer                       *SourcesServerTransport
}

// 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 "ApplicableMaccsClient":
		initServer(&s.trMu, &s.trApplicableMaccsServer, func() *ApplicableMaccsServerTransport {
			return NewApplicableMaccsServerTransport(&s.srv.ApplicableMaccsServer)
		})
		resp, err = s.trApplicableMaccsServer.Do(req)
	case "ConditionalCreditContributorsClient":
		initServer(&s.trMu, &s.trConditionalCreditContributorsServer, func() *ConditionalCreditContributorsServerTransport {
			return NewConditionalCreditContributorsServerTransport(&s.srv.ConditionalCreditContributorsServer)
		})
		resp, err = s.trConditionalCreditContributorsServer.Do(req)
	case "ConditionalCreditsClient":
		initServer(&s.trMu, &s.trConditionalCreditsServer, func() *ConditionalCreditsServerTransport {
			return NewConditionalCreditsServerTransport(&s.srv.ConditionalCreditsServer)
		})
		resp, err = s.trConditionalCreditsServer.Do(req)
	case "ContributorsClient":
		initServer(&s.trMu, &s.trContributorsServer, func() *ContributorsServerTransport { return NewContributorsServerTransport(&s.srv.ContributorsServer) })
		resp, err = s.trContributorsServer.Do(req)
	case "CreditsClient":
		initServer(&s.trMu, &s.trCreditsServer, func() *CreditsServerTransport { return NewCreditsServerTransport(&s.srv.CreditsServer) })
		resp, err = s.trCreditsServer.Do(req)
	case "DiscountClient":
		initServer(&s.trMu, &s.trDiscountServer, func() *DiscountServerTransport { return NewDiscountServerTransport(&s.srv.DiscountServer) })
		resp, err = s.trDiscountServer.Do(req)
	case "DiscountsClient":
		initServer(&s.trMu, &s.trDiscountsServer, func() *DiscountsServerTransport { return NewDiscountsServerTransport(&s.srv.DiscountsServer) })
		resp, err = s.trDiscountsServer.Do(req)
	case "FreeServicesClient":
		initServer(&s.trMu, &s.trFreeServicesServer, func() *FreeServicesServerTransport { return NewFreeServicesServerTransport(&s.srv.FreeServicesServer) })
		resp, err = s.trFreeServicesServer.Do(req)
	case "MaccsClient":
		initServer(&s.trMu, &s.trMaccsServer, func() *MaccsServerTransport { return NewMaccsServerTransport(&s.srv.MaccsServer) })
		resp, err = s.trMaccsServer.Do(req)
	case "OperationsClient":
		initServer(&s.trMu, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "RPClient":
		initServer(&s.trMu, &s.trRPServer, func() *RPServerTransport { return NewRPServerTransport(&s.srv.RPServer) })
		resp, err = s.trRPServer.Do(req)
	case "ReservationOrderAliasClient":
		initServer(&s.trMu, &s.trReservationOrderAliasServer, func() *ReservationOrderAliasServerTransport {
			return NewReservationOrderAliasServerTransport(&s.srv.ReservationOrderAliasServer)
		})
		resp, err = s.trReservationOrderAliasServer.Do(req)
	case "SavingsPlanClient":
		initServer(&s.trMu, &s.trSavingsPlanServer, func() *SavingsPlanServerTransport { return NewSavingsPlanServerTransport(&s.srv.SavingsPlanServer) })
		resp, err = s.trSavingsPlanServer.Do(req)
	case "SavingsPlanOrderAliasClient":
		initServer(&s.trMu, &s.trSavingsPlanOrderAliasServer, func() *SavingsPlanOrderAliasServerTransport {
			return NewSavingsPlanOrderAliasServerTransport(&s.srv.SavingsPlanOrderAliasServer)
		})
		resp, err = s.trSavingsPlanOrderAliasServer.Do(req)
	case "SavingsPlanOrderClient":
		initServer(&s.trMu, &s.trSavingsPlanOrderServer, func() *SavingsPlanOrderServerTransport {
			return NewSavingsPlanOrderServerTransport(&s.srv.SavingsPlanOrderServer)
		})
		resp, err = s.trSavingsPlanOrderServer.Do(req)
	case "SellerResourceClient":
		initServer(&s.trMu, &s.trSellerResourceServer, func() *SellerResourceServerTransport {
			return NewSellerResourceServerTransport(&s.srv.SellerResourceServer)
		})
		resp, err = s.trSellerResourceServer.Do(req)
	case "SourcesClient":
		initServer(&s.trMu, &s.trSourcesServer, func() *SourcesServerTransport { return NewSourcesServerTransport(&s.srv.SourcesServer) })
		resp, err = s.trSourcesServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}
