// Code generated by go-swagger; DO NOT EDIT.

package restapi

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/loads"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/middleware"
	"github.com/go-openapi/runtime/security"

	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/eventsengine/restapi/operations"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/eventsengine/restapi/operations/event_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/eventsengine/restapi/operations/status_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/eventsengine/restapi/operations/trigger_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/eventsengine/restapi/operations/usage_management"
)

type contextKey string

const AuthKey contextKey = "Auth"

//go:generate mockery -name EventManagementAPI -inpkg

/* EventManagementAPI  */
type EventManagementAPI interface {
	/* AddEvent Takes into the system the provided event */
	AddEvent(ctx context.Context, params event_management.AddEventParams) middleware.Responder

	/* GetHistory Provides the events for the id provided */
	GetHistory(ctx context.Context, params event_management.GetHistoryParams) middleware.Responder

	/* GetState Provides the events for the id provided */
	GetState(ctx context.Context, params event_management.GetStateParams) middleware.Responder

	/* ListStates Provides the list of states in not terminated state */
	ListStates(ctx context.Context, params event_management.ListStatesParams) middleware.Responder
}

//go:generate mockery -name StatusManagementAPI -inpkg

/* StatusManagementAPI  */
type StatusManagementAPI interface {
	/* GetStatus Basic status of the system */
	GetStatus(ctx context.Context, params status_management.GetStatusParams) middleware.Responder

	/* ShowStatus Basic status of the system */
	ShowStatus(ctx context.Context, params status_management.ShowStatusParams) middleware.Responder
}

//go:generate mockery -name TriggerManagementAPI -inpkg

/* TriggerManagementAPI  */
type TriggerManagementAPI interface {
	/* ExecSample Sample task trigger */
	ExecSample(ctx context.Context, params trigger_management.ExecSampleParams) middleware.Responder
}

//go:generate mockery -name UsageManagementAPI -inpkg

/* UsageManagementAPI  */
type UsageManagementAPI interface {
	/* GetSystemUsage Generates an aggregated response by account of the usage recorded in the system during the time-window specified */
	GetSystemUsage(ctx context.Context, params usage_management.GetSystemUsageParams) middleware.Responder

	/* GetUsage Generates an aggregated response of the usage recorded in the system during the time-window specified for the selected account */
	GetUsage(ctx context.Context, params usage_management.GetUsageParams) middleware.Responder
}

// Config is configuration for Handler
type Config struct {
	EventManagementAPI
	StatusManagementAPI
	TriggerManagementAPI
	UsageManagementAPI
	Logger func(string, ...interface{})
	// InnerMiddleware is for the handler executors. These do not apply to the swagger.json document.
	// The middleware executes after routing but before authentication, binding and validation
	InnerMiddleware func(http.Handler) http.Handler

	// Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions
	// and the principal was stored in the context in the "AuthKey" context value.
	Authorizer func(*http.Request) error

	// AuthAPIKeyHeader Applies when the "X-API-KEY" header is set
	AuthAPIKeyHeader func(token string) (interface{}, error)

	// AuthAPIKeyParam Applies when the "api_key" query is set
	AuthAPIKeyParam func(token string) (interface{}, error)

	// AuthKeycloak For OAuth2 authentication
	AuthKeycloak func(token string, scopes []string) (interface{}, error)
	// Authenticator to use for all APIKey authentication
	APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator
	// Authenticator to use for all Bearer authentication
	BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
	// Authenticator to use for all Basic authentication
	BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
}

// Handler returns an http.Handler given the handler configuration
// It mounts all the business logic implementers in the right routing.
func Handler(c Config) (http.Handler, error) {
	h, _, err := HandlerAPI(c)
	return h, err
}

// HandlerAPI returns an http.Handler given the handler configuration
// and the corresponding *EventEngineManagementAPI instance.
// It mounts all the business logic implementers in the right routing.
func HandlerAPI(c Config) (http.Handler, *operations.EventEngineManagementAPIAPI, error) {
	spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "")
	if err != nil {
		return nil, nil, fmt.Errorf("analyze swagger: %v", err)
	}
	api := operations.NewEventEngineManagementAPIAPI(spec)
	api.ServeError = errors.ServeError
	api.Logger = c.Logger

	if c.APIKeyAuthenticator != nil {
		api.APIKeyAuthenticator = c.APIKeyAuthenticator
	}
	if c.BasicAuthenticator != nil {
		api.BasicAuthenticator = c.BasicAuthenticator
	}
	if c.BearerAuthenticator != nil {
		api.BearerAuthenticator = c.BearerAuthenticator
	}

	api.JSONConsumer = runtime.JSONConsumer()
	api.JSONProducer = runtime.JSONProducer()
	api.APIKeyHeaderAuth = func(token string) (interface{}, error) {
		if c.AuthAPIKeyHeader == nil {
			return token, nil
		}
		return c.AuthAPIKeyHeader(token)
	}

	api.APIKeyParamAuth = func(token string) (interface{}, error) {
		if c.AuthAPIKeyParam == nil {
			return token, nil
		}
		return c.AuthAPIKeyParam(token)
	}

	api.KeycloakAuth = func(token string, scopes []string) (interface{}, error) {
		if c.AuthKeycloak == nil {
			return token, nil
		}
		return c.AuthKeycloak(token, scopes)
	}
	api.APIAuthorizer = authorizer(c.Authorizer)
	api.EventManagementAddEventHandler = event_management.AddEventHandlerFunc(func(params event_management.AddEventParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.EventManagementAPI.AddEvent(ctx, params)
	})
	api.TriggerManagementExecSampleHandler = trigger_management.ExecSampleHandlerFunc(func(params trigger_management.ExecSampleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.TriggerManagementAPI.ExecSample(ctx, params)
	})
	api.EventManagementGetHistoryHandler = event_management.GetHistoryHandlerFunc(func(params event_management.GetHistoryParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.EventManagementAPI.GetHistory(ctx, params)
	})
	api.EventManagementGetStateHandler = event_management.GetStateHandlerFunc(func(params event_management.GetStateParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.EventManagementAPI.GetState(ctx, params)
	})
	api.StatusManagementGetStatusHandler = status_management.GetStatusHandlerFunc(func(params status_management.GetStatusParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.StatusManagementAPI.GetStatus(ctx, params)
	})
	api.UsageManagementGetSystemUsageHandler = usage_management.GetSystemUsageHandlerFunc(func(params usage_management.GetSystemUsageParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.UsageManagementAPI.GetSystemUsage(ctx, params)
	})
	api.UsageManagementGetUsageHandler = usage_management.GetUsageHandlerFunc(func(params usage_management.GetUsageParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.UsageManagementAPI.GetUsage(ctx, params)
	})
	api.EventManagementListStatesHandler = event_management.ListStatesHandlerFunc(func(params event_management.ListStatesParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.EventManagementAPI.ListStates(ctx, params)
	})
	api.StatusManagementShowStatusHandler = status_management.ShowStatusHandlerFunc(func(params status_management.ShowStatusParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.StatusManagementAPI.ShowStatus(ctx, params)
	})
	api.ServerShutdown = func() {}
	return api.Serve(c.InnerMiddleware), api, nil
}

// swaggerCopy copies the swagger json to prevent data races in runtime
func swaggerCopy(orig json.RawMessage) json.RawMessage {
	c := make(json.RawMessage, len(orig))
	copy(c, orig)
	return c
}

// authorizer is a helper function to implement the runtime.Authorizer interface.
type authorizer func(*http.Request) error

func (a authorizer) Authorize(req *http.Request, principal interface{}) error {
	if a == nil {
		return nil
	}
	ctx := storeAuth(req.Context(), principal)
	return a(req.WithContext(ctx))
}

func storeAuth(ctx context.Context, principal interface{}) context.Context {
	return context.WithValue(ctx, AuthKey, principal)
}
