// 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/portalservicescopilot/armportalservicescopilot"
	"net/http"
)

// CopilotSettingsServer is a fake server for instances of the armportalservicescopilot.CopilotSettingsClient type.
type CopilotSettingsServer struct {
	// CreateOrUpdate is the fake for method CopilotSettingsClient.CreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	CreateOrUpdate func(ctx context.Context, resource armportalservicescopilot.CopilotSettingsResource, options *armportalservicescopilot.CopilotSettingsClientCreateOrUpdateOptions) (resp azfake.Responder[armportalservicescopilot.CopilotSettingsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// Delete is the fake for method CopilotSettingsClient.Delete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusNoContent
	Delete func(ctx context.Context, options *armportalservicescopilot.CopilotSettingsClientDeleteOptions) (resp azfake.Responder[armportalservicescopilot.CopilotSettingsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method CopilotSettingsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, options *armportalservicescopilot.CopilotSettingsClientGetOptions) (resp azfake.Responder[armportalservicescopilot.CopilotSettingsClientGetResponse], errResp azfake.ErrorResponder)

	// Update is the fake for method CopilotSettingsClient.Update
	// HTTP status codes to indicate success: http.StatusOK
	Update func(ctx context.Context, properties armportalservicescopilot.CopilotSettingsResourceUpdate, options *armportalservicescopilot.CopilotSettingsClientUpdateOptions) (resp azfake.Responder[armportalservicescopilot.CopilotSettingsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewCopilotSettingsServerTransport creates a new instance of CopilotSettingsServerTransport with the provided implementation.
// The returned CopilotSettingsServerTransport instance is connected to an instance of armportalservicescopilot.CopilotSettingsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewCopilotSettingsServerTransport(srv *CopilotSettingsServer) *CopilotSettingsServerTransport {
	return &CopilotSettingsServerTransport{srv: srv}
}

// CopilotSettingsServerTransport connects instances of armportalservicescopilot.CopilotSettingsClient to instances of CopilotSettingsServer.
// Don't use this type directly, use NewCopilotSettingsServerTransport instead.
type CopilotSettingsServerTransport struct {
	srv *CopilotSettingsServer
}

// Do implements the policy.Transporter interface for CopilotSettingsServerTransport.
func (c *CopilotSettingsServerTransport) 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 *CopilotSettingsServerTransport) 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 copilotSettingsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = copilotSettingsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "CopilotSettingsClient.CreateOrUpdate":
				res.resp, res.err = c.dispatchCreateOrUpdate(req)
			case "CopilotSettingsClient.Delete":
				res.resp, res.err = c.dispatchDelete(req)
			case "CopilotSettingsClient.Get":
				res.resp, res.err = c.dispatchGet(req)
			case "CopilotSettingsClient.Update":
				res.resp, res.err = c.dispatchUpdate(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 *CopilotSettingsServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if c.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armportalservicescopilot.CopilotSettingsResource](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.CreateOrUpdate(req.Context(), body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).CopilotSettingsResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *CopilotSettingsServerTransport) dispatchDelete(req *http.Request) (*http.Response, error) {
	if c.srv.Delete == nil {
		return nil, &nonRetriableError{errors.New("fake for method Delete not implemented")}
	}
	respr, errRespr := c.srv.Delete(req.Context(), nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (c *CopilotSettingsServerTransport) dispatchUpdate(req *http.Request) (*http.Response, error) {
	if c.srv.Update == nil {
		return nil, &nonRetriableError{errors.New("fake for method Update not implemented")}
	}
	body, err := server.UnmarshalRequestAsJSON[armportalservicescopilot.CopilotSettingsResourceUpdate](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.Update(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).CopilotSettingsResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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