// 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 (
	"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/carbonoptimization/armcarbonoptimization"
	"net/http"
)

// CarbonServiceServer is a fake server for instances of the armcarbonoptimization.CarbonServiceClient type.
type CarbonServiceServer struct {
	// QueryCarbonEmissionDataAvailableDateRange is the fake for method CarbonServiceClient.QueryCarbonEmissionDataAvailableDateRange
	// HTTP status codes to indicate success: http.StatusOK
	QueryCarbonEmissionDataAvailableDateRange func(ctx context.Context, options *armcarbonoptimization.CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeOptions) (resp azfake.Responder[armcarbonoptimization.CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse], errResp azfake.ErrorResponder)

	// QueryCarbonEmissionReports is the fake for method CarbonServiceClient.QueryCarbonEmissionReports
	// HTTP status codes to indicate success: http.StatusOK
	QueryCarbonEmissionReports func(ctx context.Context, queryParameters armcarbonoptimization.QueryFilterClassification, options *armcarbonoptimization.CarbonServiceClientQueryCarbonEmissionReportsOptions) (resp azfake.Responder[armcarbonoptimization.CarbonServiceClientQueryCarbonEmissionReportsResponse], errResp azfake.ErrorResponder)
}

// NewCarbonServiceServerTransport creates a new instance of CarbonServiceServerTransport with the provided implementation.
// The returned CarbonServiceServerTransport instance is connected to an instance of armcarbonoptimization.CarbonServiceClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewCarbonServiceServerTransport(srv *CarbonServiceServer) *CarbonServiceServerTransport {
	return &CarbonServiceServerTransport{srv: srv}
}

// CarbonServiceServerTransport connects instances of armcarbonoptimization.CarbonServiceClient to instances of CarbonServiceServer.
// Don't use this type directly, use NewCarbonServiceServerTransport instead.
type CarbonServiceServerTransport struct {
	srv *CarbonServiceServer
}

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

	return c.dispatchToMethodFake(req, method)
}

func (c *CarbonServiceServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if carbonServiceServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = carbonServiceServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "CarbonServiceClient.QueryCarbonEmissionDataAvailableDateRange":
				res.resp, res.err = c.dispatchQueryCarbonEmissionDataAvailableDateRange(req)
			case "CarbonServiceClient.QueryCarbonEmissionReports":
				res.resp, res.err = c.dispatchQueryCarbonEmissionReports(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (c *CarbonServiceServerTransport) dispatchQueryCarbonEmissionDataAvailableDateRange(req *http.Request) (*http.Response, error) {
	if c.srv.QueryCarbonEmissionDataAvailableDateRange == nil {
		return nil, &nonRetriableError{errors.New("fake for method QueryCarbonEmissionDataAvailableDateRange not implemented")}
	}
	respr, errRespr := c.srv.QueryCarbonEmissionDataAvailableDateRange(req.Context(), 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).CarbonEmissionDataAvailableDateRange, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *CarbonServiceServerTransport) dispatchQueryCarbonEmissionReports(req *http.Request) (*http.Response, error) {
	if c.srv.QueryCarbonEmissionReports == nil {
		return nil, &nonRetriableError{errors.New("fake for method QueryCarbonEmissionReports not implemented")}
	}
	raw, err := readRequestBody(req)
	if err != nil {
		return nil, err
	}
	body, err := unmarshalQueryFilterClassification(raw)
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.QueryCarbonEmissionReports(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).CarbonEmissionDataListResult, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// set this to conditionally intercept incoming requests to CarbonServiceServerTransport
var carbonServiceServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
