// 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 armnginx.ClientFactory type.
type ServerFactory struct {
	// APIKeysServer contains the fakes for client APIKeysClient
	APIKeysServer APIKeysServer

	// CertificatesServer contains the fakes for client CertificatesClient
	CertificatesServer CertificatesServer

	// ConfigurationsServer contains the fakes for client ConfigurationsClient
	ConfigurationsServer ConfigurationsServer

	// DefaultWafPolicyServer contains the fakes for client DefaultWafPolicyClient
	DefaultWafPolicyServer DefaultWafPolicyServer

	// DeploymentWafPoliciesServer contains the fakes for client DeploymentWafPoliciesClient
	DeploymentWafPoliciesServer DeploymentWafPoliciesServer

	// DeploymentsServer contains the fakes for client DeploymentsClient
	DeploymentsServer DeploymentsServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// WafPolicyServer contains the fakes for client WafPolicyClient
	WafPolicyServer WafPolicyServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armnginx.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 armnginx.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                           *ServerFactory
	trMu                          sync.Mutex
	trAPIKeysServer               *APIKeysServerTransport
	trCertificatesServer          *CertificatesServerTransport
	trConfigurationsServer        *ConfigurationsServerTransport
	trDefaultWafPolicyServer      *DefaultWafPolicyServerTransport
	trDeploymentWafPoliciesServer *DeploymentWafPoliciesServerTransport
	trDeploymentsServer           *DeploymentsServerTransport
	trOperationsServer            *OperationsServerTransport
	trWafPolicyServer             *WafPolicyServerTransport
}

// 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 "APIKeysClient":
		initServer(&s.trMu, &s.trAPIKeysServer, func() *APIKeysServerTransport { return NewAPIKeysServerTransport(&s.srv.APIKeysServer) })
		resp, err = s.trAPIKeysServer.Do(req)
	case "CertificatesClient":
		initServer(&s.trMu, &s.trCertificatesServer, func() *CertificatesServerTransport { return NewCertificatesServerTransport(&s.srv.CertificatesServer) })
		resp, err = s.trCertificatesServer.Do(req)
	case "ConfigurationsClient":
		initServer(&s.trMu, &s.trConfigurationsServer, func() *ConfigurationsServerTransport {
			return NewConfigurationsServerTransport(&s.srv.ConfigurationsServer)
		})
		resp, err = s.trConfigurationsServer.Do(req)
	case "DefaultWafPolicyClient":
		initServer(&s.trMu, &s.trDefaultWafPolicyServer, func() *DefaultWafPolicyServerTransport {
			return NewDefaultWafPolicyServerTransport(&s.srv.DefaultWafPolicyServer)
		})
		resp, err = s.trDefaultWafPolicyServer.Do(req)
	case "DeploymentWafPoliciesClient":
		initServer(&s.trMu, &s.trDeploymentWafPoliciesServer, func() *DeploymentWafPoliciesServerTransport {
			return NewDeploymentWafPoliciesServerTransport(&s.srv.DeploymentWafPoliciesServer)
		})
		resp, err = s.trDeploymentWafPoliciesServer.Do(req)
	case "DeploymentsClient":
		initServer(&s.trMu, &s.trDeploymentsServer, func() *DeploymentsServerTransport { return NewDeploymentsServerTransport(&s.srv.DeploymentsServer) })
		resp, err = s.trDeploymentsServer.Do(req)
	case "OperationsClient":
		initServer(&s.trMu, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "WafPolicyClient":
		initServer(&s.trMu, &s.trWafPolicyServer, func() *WafPolicyServerTransport { return NewWafPolicyServerTransport(&s.srv.WafPolicyServer) })
		resp, err = s.trWafPolicyServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}
