/*
 * Solidarity Mobility API
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * API version: 1.0.0
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package openapi

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

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

// DriverAvailabilityAndScheduleAPIOption for how the controller is set up.
type DriverAvailabilityAndScheduleAPIOption func(*DriverAvailabilityAndScheduleAPIController)

// WithDriverAvailabilityAndScheduleAPIErrorHandler inject ErrorHandler into controller
func WithDriverAvailabilityAndScheduleAPIErrorHandler(h ErrorHandler) DriverAvailabilityAndScheduleAPIOption {
	return func(c *DriverAvailabilityAndScheduleAPIController) {
		c.errorHandler = h
	}
}

// NewDriverAvailabilityAndScheduleAPIController creates a default api controller
func NewDriverAvailabilityAndScheduleAPIController(s DriverAvailabilityAndScheduleAPIServicer, opts ...DriverAvailabilityAndScheduleAPIOption) Router {
	controller := &DriverAvailabilityAndScheduleAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the DriverAvailabilityAndScheduleAPIController
func (c *DriverAvailabilityAndScheduleAPIController) Routes() Routes {
	return Routes{
		"DriverPunctualAvailabilitiesPost": Route{
			strings.ToUpper("Post"),
			"/driver_punctual_availabilities",
			c.DriverPunctualAvailabilitiesPost,
		},
		"DriverRegularAvailabilitiesPost": Route{
			strings.ToUpper("Post"),
			"/driver_regular_availabilities",
			c.DriverRegularAvailabilitiesPost,
		},
	}
}

// DriverPunctualAvailabilitiesPost - Set Punctual Driver Availabilities
func (c *DriverAvailabilityAndScheduleAPIController) DriverPunctualAvailabilitiesPost(w http.ResponseWriter, r *http.Request) {
	authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
	if !authenticated {
		response := ImplResponse{
			Code: 401,
			Body: "Unauthorized request. Check your operator / API Key.",
		}
		EncodeJSONResponse(response.Body, &response.Code, w)
		return
	}
	driverPunctualAvailabilitiesRequestParam := DriverPunctualAvailabilitiesRequest{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&driverPunctualAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertDriverPunctualAvailabilitiesRequestRequired(driverPunctualAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertDriverPunctualAvailabilitiesRequestConstraints(driverPunctualAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.DriverPunctualAvailabilitiesPost(r.Context(), driverPunctualAvailabilitiesRequestParam)
	// 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)
}

// DriverRegularAvailabilitiesPost - Set Regular Driver Availabilities
func (c *DriverAvailabilityAndScheduleAPIController) DriverRegularAvailabilitiesPost(w http.ResponseWriter, r *http.Request) {
	authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
	if !authenticated {
		response := ImplResponse{
			Code: 401,
			Body: "Unauthorized request. Check your operator / API Key.",
		}
		EncodeJSONResponse(response.Body, &response.Code, w)
		return
	}
	driverRegularAvailabilitiesRequestParam := DriverRegularAvailabilitiesRequest{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&driverRegularAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertDriverRegularAvailabilitiesRequestRequired(driverRegularAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertDriverRegularAvailabilitiesRequestConstraints(driverRegularAvailabilitiesRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.DriverRegularAvailabilitiesPost(r.Context(), driverRegularAvailabilitiesRequestParam)
	// 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)
}