/*
 * OPEN OUTCRY API
 *
 * # Introduction This API is documented in **OpenAPI 3.0 format**.  This API the following operations: * Retrieve a list of available instruments * Retrieve a list of executed trades  # Basics * API calls have to be secured with HTTPS. * All data has to be submitted UTF-8 encoded. * The reply is sent JSON encoded.
 *
 * API version: 1.0.0
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package api

import (
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

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

// PublicAPIOption for how the controller is set up.
type PublicAPIOption func(*PublicAPIController)

// WithPublicAPIErrorHandler inject ErrorHandler into controller
func WithPublicAPIErrorHandler(h ErrorHandler) PublicAPIOption {
	return func(c *PublicAPIController) {
		c.errorHandler = h
	}
}

// NewPublicAPIController creates a default api controller
func NewPublicAPIController(s PublicAPIServicer, opts ...PublicAPIOption) Router {
	controller := &PublicAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the PublicAPIController
func (c *PublicAPIController) Routes() Routes {
	return Routes{
		"GetCurrencies": Route{
			strings.ToUpper("Get"),
			"/currencies",
			c.GetCurrencies,
		},
		"GetFxInstruments": Route{
			strings.ToUpper("Get"),
			"/fxinstruments",
			c.GetFxInstruments,
		},
		"GetInstruments": Route{
			strings.ToUpper("Get"),
			"/instruments",
			c.GetInstruments,
		},
		"GetOrderBook": Route{
			strings.ToUpper("Get"),
			"/order-books/{instrument_name}",
			c.GetOrderBook,
		},
	}
}

// GetCurrencies - Currencies list
func (c *PublicAPIController) GetCurrencies(w http.ResponseWriter, r *http.Request) {
	result, err := c.service.GetCurrencies(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)
}

// GetFxInstruments - Fx instrument list
func (c *PublicAPIController) GetFxInstruments(w http.ResponseWriter, r *http.Request) {
	result, err := c.service.GetFxInstruments(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)
}

// GetInstruments - Instrument list
func (c *PublicAPIController) GetInstruments(w http.ResponseWriter, r *http.Request) {
	result, err := c.service.GetInstruments(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)
}

// GetOrderBook - Get order book
func (c *PublicAPIController) GetOrderBook(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	instrumentNameParam := params["instrument_name"]
	if instrumentNameParam == "" {
		c.errorHandler(w, r, &RequiredError{"instrument_name"}, nil)
		return
	}
	result, err := c.service.GetOrderBook(r.Context(), instrumentNameParam)
	// 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)
}
