// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

/*
 * (title)
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * API version: 0.0.0
 */

package openapi

import (
	"encoding/json"
	"net/http"
	"strings"
)

// DefaultAPIController binds http requests to an api service and writes the service results to the http response
type DefaultAPIController struct {
	service DefaultAPIServicer
	errorHandler ErrorHandler
}

// DefaultAPIOption for how the controller is set up.
type DefaultAPIOption func(*DefaultAPIController)

// WithDefaultAPIErrorHandler inject ErrorHandler into controller
func WithDefaultAPIErrorHandler(h ErrorHandler) DefaultAPIOption {
	return func(c *DefaultAPIController) {
		c.errorHandler = h
	}
}

// NewDefaultAPIController creates a default api controller
func NewDefaultAPIController(s DefaultAPIServicer, opts ...DefaultAPIOption) *DefaultAPIController {
	controller := &DefaultAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

	for _, opt := range opts {
		opt(controller)
	}

	return controller
}

// Routes returns all the api routes for the DefaultAPIController
func (c *DefaultAPIController) Routes() Routes {
	return Routes{
		"OnMqttClientConnected": Route{
			strings.ToUpper("Post"),
			"/MqttConnected",
			c.OnMqttClientConnected,
		},
		"OnMqttClientDisconnected": Route{
			strings.ToUpper("Post"),
			"/MqttDisconnected",
			c.OnMqttClientDisconnected,
		},
		"OnMqttConnect": Route{
			strings.ToUpper("Post"),
			"/MqttConnect",
			c.OnMqttConnect,
		},
	}
}

// OnMqttClientConnected -
func (c *DefaultAPIController) OnMqttClientConnected(w http.ResponseWriter, r *http.Request) {
	cePhysicalConnectionIdParam := r.Header.Get("ce-physicalConnectionId")
	ceHubParam := r.Header.Get("ce-hub")
	ceConnectionIdParam := r.Header.Get("ce-connectionId")
	ceEventNameParam := r.Header.Get("ce-eventName")
	bodyParam := map[string]interface{}{}
	d := json.NewDecoder(r.Body)

	// You SHOULD NOT use d.DisallowUnknownFields() for Web PubSub CloudEvents because it's expected to have new properties in the future.
	//d.DisallowUnknownFields()

	if err := d.Decode(&bodyParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	ceUserIdParam := r.Header.Get("ce-userId")
	ceSubprotocolParam := r.Header.Get("ce-subprotocol")
	ceConnectionStateParam := r.Header.Get("ce-connectionState")
	ceSignatureParam := r.Header.Get("ce-signature")
	result, err := c.service.OnMqttClientConnected(r.Context(), cePhysicalConnectionIdParam, ceHubParam, ceConnectionIdParam, ceEventNameParam, bodyParam, ceUserIdParam, ceSubprotocolParam, ceConnectionStateParam, ceSignatureParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	_ = EncodeJSONResponse(result.Body, &result.Code, w)
}

// OnMqttClientDisconnected -
func (c *DefaultAPIController) OnMqttClientDisconnected(w http.ResponseWriter, r *http.Request) {
	cePhysicalConnectionIdParam := r.Header.Get("ce-physicalConnectionId")
	ceHubParam := r.Header.Get("ce-hub")
	ceConnectionIdParam := r.Header.Get("ce-connectionId")
	ceEventNameParam := r.Header.Get("ce-eventName")
	mqttDisconnectedEventRequestParam := MqttDisconnectedEventRequest{}
	d := json.NewDecoder(r.Body)

	// You SHOULD NOT use d.DisallowUnknownFields() for Web PubSub CloudEvents because it's expected to have new properties in the future.
	//d.DisallowUnknownFields()

	if err := d.Decode(&mqttDisconnectedEventRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertMqttDisconnectedEventRequestRequired(mqttDisconnectedEventRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertMqttDisconnectedEventRequestConstraints(mqttDisconnectedEventRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	ceUserIdParam := r.Header.Get("ce-userId")
	ceSubprotocolParam := r.Header.Get("ce-subprotocol")
	ceConnectionStateParam := r.Header.Get("ce-connectionState")
	ceSignatureParam := r.Header.Get("ce-signature")
	result, err := c.service.OnMqttClientDisconnected(r.Context(), cePhysicalConnectionIdParam, ceHubParam, ceConnectionIdParam, ceEventNameParam, mqttDisconnectedEventRequestParam, ceUserIdParam, ceSubprotocolParam, ceConnectionStateParam, ceSignatureParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	_ = EncodeJSONResponse(result.Body, &result.Code, w)
}

// OnMqttConnect -
func (c *DefaultAPIController) OnMqttConnect(w http.ResponseWriter, r *http.Request) {
	cePhysicalConnectionIdParam := r.Header.Get("ce-physicalConnectionId")
	ceHubParam := r.Header.Get("ce-hub")
	ceConnectionIdParam := r.Header.Get("ce-connectionId")
	ceEventNameParam := r.Header.Get("ce-eventName")
	mqttConnectEventRequestParam := MqttConnectEventRequest{}
	d := json.NewDecoder(r.Body)

	// You SHOULD NOT use d.DisallowUnknownFields() for Web PubSub CloudEvents because it's expected to have new properties in the future.
	//d.DisallowUnknownFields()

	if err := d.Decode(&mqttConnectEventRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertMqttConnectEventRequestRequired(mqttConnectEventRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertMqttConnectEventRequestConstraints(mqttConnectEventRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	ceUserIdParam := r.Header.Get("ce-userId")
	ceSubprotocolParam := r.Header.Get("ce-subprotocol")
	ceConnectionStateParam := r.Header.Get("ce-connectionState")
	ceSignatureParam := r.Header.Get("ce-signature")
	result, err := c.service.OnMqttConnect(r.Context(), cePhysicalConnectionIdParam, ceHubParam, ceConnectionIdParam, ceEventNameParam, mqttConnectEventRequestParam, ceUserIdParam, ceSubprotocolParam, ceConnectionStateParam, ceSignatureParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	_ = EncodeJSONResponse(result.Body, &result.Code, w)
}
