//go:build go1.18
// +build go1.18

// 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 (
	"context"
	"errors"
	"fmt"
	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcomplianceautomation/armappcomplianceautomation"
	"net/http"
)

// ProviderActionsServer is a fake server for instances of the armappcomplianceautomation.ProviderActionsClient type.
type ProviderActionsServer struct {
	// CheckNameAvailability is the fake for method ProviderActionsClient.CheckNameAvailability
	// HTTP status codes to indicate success: http.StatusOK
	CheckNameAvailability func(ctx context.Context, body armappcomplianceautomation.CheckNameAvailabilityRequest, options *armappcomplianceautomation.ProviderActionsClientCheckNameAvailabilityOptions) (resp azfake.Responder[armappcomplianceautomation.ProviderActionsClientCheckNameAvailabilityResponse], errResp azfake.ErrorResponder)

	// GetCollectionCount is the fake for method ProviderActionsClient.GetCollectionCount
	// HTTP status codes to indicate success: http.StatusOK
	GetCollectionCount func(ctx context.Context, body armappcomplianceautomation.GetCollectionCountRequest, options *armappcomplianceautomation.ProviderActionsClientGetCollectionCountOptions) (resp azfake.Responder[armappcomplianceautomation.ProviderActionsClientGetCollectionCountResponse], errResp azfake.ErrorResponder)

	// GetOverviewStatus is the fake for method ProviderActionsClient.GetOverviewStatus
	// HTTP status codes to indicate success: http.StatusOK
	GetOverviewStatus func(ctx context.Context, body armappcomplianceautomation.GetOverviewStatusRequest, options *armappcomplianceautomation.ProviderActionsClientGetOverviewStatusOptions) (resp azfake.Responder[armappcomplianceautomation.ProviderActionsClientGetOverviewStatusResponse], errResp azfake.ErrorResponder)

	// ListInUseStorageAccounts is the fake for method ProviderActionsClient.ListInUseStorageAccounts
	// HTTP status codes to indicate success: http.StatusOK
	ListInUseStorageAccounts func(ctx context.Context, body armappcomplianceautomation.ListInUseStorageAccountsRequest, options *armappcomplianceautomation.ProviderActionsClientListInUseStorageAccountsOptions) (resp azfake.Responder[armappcomplianceautomation.ProviderActionsClientListInUseStorageAccountsResponse], errResp azfake.ErrorResponder)

	// BeginOnboard is the fake for method ProviderActionsClient.BeginOnboard
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginOnboard func(ctx context.Context, body armappcomplianceautomation.OnboardRequest, options *armappcomplianceautomation.ProviderActionsClientBeginOnboardOptions) (resp azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientOnboardResponse], errResp azfake.ErrorResponder)

	// BeginTriggerEvaluation is the fake for method ProviderActionsClient.BeginTriggerEvaluation
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginTriggerEvaluation func(ctx context.Context, body armappcomplianceautomation.TriggerEvaluationRequest, options *armappcomplianceautomation.ProviderActionsClientBeginTriggerEvaluationOptions) (resp azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientTriggerEvaluationResponse], errResp azfake.ErrorResponder)
}

// NewProviderActionsServerTransport creates a new instance of ProviderActionsServerTransport with the provided implementation.
// The returned ProviderActionsServerTransport instance is connected to an instance of armappcomplianceautomation.ProviderActionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewProviderActionsServerTransport(srv *ProviderActionsServer) *ProviderActionsServerTransport {
	return &ProviderActionsServerTransport{
		srv:                    srv,
		beginOnboard:           newTracker[azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientOnboardResponse]](),
		beginTriggerEvaluation: newTracker[azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientTriggerEvaluationResponse]](),
	}
}

// ProviderActionsServerTransport connects instances of armappcomplianceautomation.ProviderActionsClient to instances of ProviderActionsServer.
// Don't use this type directly, use NewProviderActionsServerTransport instead.
type ProviderActionsServerTransport struct {
	srv                    *ProviderActionsServer
	beginOnboard           *tracker[azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientOnboardResponse]]
	beginTriggerEvaluation *tracker[azfake.PollerResponder[armappcomplianceautomation.ProviderActionsClientTriggerEvaluationResponse]]
}

// Do implements the policy.Transporter interface for ProviderActionsServerTransport.
func (p *ProviderActionsServerTransport) 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")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "ProviderActionsClient.CheckNameAvailability":
		resp, err = p.dispatchCheckNameAvailability(req)
	case "ProviderActionsClient.GetCollectionCount":
		resp, err = p.dispatchGetCollectionCount(req)
	case "ProviderActionsClient.GetOverviewStatus":
		resp, err = p.dispatchGetOverviewStatus(req)
	case "ProviderActionsClient.ListInUseStorageAccounts":
		resp, err = p.dispatchListInUseStorageAccounts(req)
	case "ProviderActionsClient.BeginOnboard":
		resp, err = p.dispatchBeginOnboard(req)
	case "ProviderActionsClient.BeginTriggerEvaluation":
		resp, err = p.dispatchBeginTriggerEvaluation(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchCheckNameAvailability(req *http.Request) (*http.Response, error) {
	if p.srv.CheckNameAvailability == nil {
		return nil, &nonRetriableError{errors.New("fake for method CheckNameAvailability not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.CheckNameAvailabilityRequest](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.CheckNameAvailability(req.Context(), body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).CheckNameAvailabilityResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchGetCollectionCount(req *http.Request) (*http.Response, error) {
	if p.srv.GetCollectionCount == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetCollectionCount not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.GetCollectionCountRequest](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.GetCollectionCount(req.Context(), body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).GetCollectionCountResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchGetOverviewStatus(req *http.Request) (*http.Response, error) {
	if p.srv.GetOverviewStatus == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetOverviewStatus not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.GetOverviewStatusRequest](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.GetOverviewStatus(req.Context(), body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).GetOverviewStatusResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchListInUseStorageAccounts(req *http.Request) (*http.Response, error) {
	if p.srv.ListInUseStorageAccounts == nil {
		return nil, &nonRetriableError{errors.New("fake for method ListInUseStorageAccounts not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.ListInUseStorageAccountsRequest](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.ListInUseStorageAccounts(req.Context(), body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).ListInUseStorageAccountsResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchBeginOnboard(req *http.Request) (*http.Response, error) {
	if p.srv.BeginOnboard == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginOnboard not implemented")}
	}
	beginOnboard := p.beginOnboard.get(req)
	if beginOnboard == nil {
		body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.OnboardRequest](req)
		if err != nil {
			return nil, err
		}
		respr, errRespr := p.srv.BeginOnboard(req.Context(), body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginOnboard = &respr
		p.beginOnboard.add(req, beginOnboard)
	}

	resp, err := server.PollerResponderNext(beginOnboard, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		p.beginOnboard.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginOnboard) {
		p.beginOnboard.remove(req)
	}

	return resp, nil
}

func (p *ProviderActionsServerTransport) dispatchBeginTriggerEvaluation(req *http.Request) (*http.Response, error) {
	if p.srv.BeginTriggerEvaluation == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginTriggerEvaluation not implemented")}
	}
	beginTriggerEvaluation := p.beginTriggerEvaluation.get(req)
	if beginTriggerEvaluation == nil {
		body, err := server.UnmarshalRequestAsJSON[armappcomplianceautomation.TriggerEvaluationRequest](req)
		if err != nil {
			return nil, err
		}
		respr, errRespr := p.srv.BeginTriggerEvaluation(req.Context(), body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginTriggerEvaluation = &respr
		p.beginTriggerEvaluation.add(req, beginTriggerEvaluation)
	}

	resp, err := server.PollerResponderNext(beginTriggerEvaluation, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		p.beginTriggerEvaluation.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginTriggerEvaluation) {
		p.beginTriggerEvaluation.remove(req)
	}

	return resp, nil
}
