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

package dashapi

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

	"github.com/gorilla/mux"
)

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

// TrackingApiOption for how the controller is set up.
type TrackingApiOption func(*TrackingApiController)

// WithTrackingApiErrorHandler inject ErrorHandler into controller
func WithTrackingApiErrorHandler(h ErrorHandler) TrackingApiOption {
	return func(c *TrackingApiController) {
		c.errorHandler = h
	}
}

// NewTrackingApiController creates a default api controller
func NewTrackingApiController(s TrackingApiServicer, opts ...TrackingApiOption) Router {
	controller := &TrackingApiController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the TrackingApiController
func (c *TrackingApiController) Routes() Routes {
	return Routes{ 
		{
			"GetTrackingCategories",
			strings.ToUpper("Get"),
			"/api/v1/tracking/categories",
			c.GetTrackingCategories,
		},
		{
			"GetTrackingEntryForDate",
			strings.ToUpper("Get"),
			"/api/v1/tracking/entry/{date}",
			c.GetTrackingEntryForDate,
		},
		{
			"WriteTrackingEntry",
			strings.ToUpper("Post"),
			"/api/v1/tracking/entry",
			c.WriteTrackingEntry,
		},
	}
}

// GetTrackingCategories - 
func (c *TrackingApiController) GetTrackingCategories(w http.ResponseWriter, r *http.Request) {
	result, err := c.service.GetTrackingCategories(r.Context())
	// 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)

}

// GetTrackingEntryForDate - 
func (c *TrackingApiController) GetTrackingEntryForDate(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	dateParam := params["date"]
	
	result, err := c.service.GetTrackingEntryForDate(r.Context(), dateParam)
	// 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)

}

// WriteTrackingEntry - 
func (c *TrackingApiController) WriteTrackingEntry(w http.ResponseWriter, r *http.Request) {
	trackingEntryParam := TrackingEntry{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&trackingEntryParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertTrackingEntryRequired(trackingEntryParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.WriteTrackingEntry(r.Context(), trackingEntryParam)
	// 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)

}
