// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

/*
 * Flame REST 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"

	"github.com/gorilla/mux"

	"github.com/cisco-open/flame/pkg/openapi/constants"
)

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

// DesignsApiOption for how the controller is set up.
type DesignsApiOption func(*DesignsApiController)

// WithDesignsApiErrorHandler inject ErrorHandler into controller
func WithDesignsApiErrorHandler(h ErrorHandler) DesignsApiOption {
	return func(c *DesignsApiController) {
		c.errorHandler = h
	}
}

// NewDesignsApiController creates a default api controller
func NewDesignsApiController(s DesignsApiServicer, opts ...DesignsApiOption) Router {
	controller := &DesignsApiController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the DesignsApiController
func (c *DesignsApiController) Routes() Routes {
	return Routes{
		{
			"CreateDesign",
			strings.ToUpper("Post"),
			"/users/{user}/designs",
			c.CreateDesign,
		},
		{
			"DeleteDesign",
			strings.ToUpper("Delete"),
			"/users/{user}/designs/{designId}",
			c.DeleteDesign,
		},
		{
			"GetDesign",
			strings.ToUpper("Get"),
			"/users/{user}/designs/{designId}",
			c.GetDesign,
		},
		{
			"GetDesigns",
			strings.ToUpper("Get"),
			"/users/{user}/designs",
			c.GetDesigns,
		},
		{
			"UpdateDesign",
			strings.ToUpper("Put"),
			"/users/{user}/designs/{designId}",
			c.UpdateDesign,
		},
	}
}

// CreateDesign - Create a new design template.
func (c *DesignsApiController) CreateDesign(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	userParam := params[constants.ParamUser]

	designInfoParam := DesignInfo{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&designInfoParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertDesignInfoRequired(designInfoParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.CreateDesign(r.Context(), userParam, designInfoParam)
	// 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)
}

// DeleteDesign - Delete design template
func (c *DesignsApiController) DeleteDesign(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	query := r.URL.Query()
	forceDeleteParam, err := parseBoolParameter(query.Get(constants.ParamForceDelete), false)
	if err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}

	userParam := params[constants.ParamUser]

	designIdParam := params[constants.ParamDesignID]

	result, err := c.service.DeleteDesign(r.Context(), userParam, designIdParam, forceDeleteParam)
	// 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)
}

// GetDesign - Get design template information
func (c *DesignsApiController) GetDesign(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	userParam := params[constants.ParamUser]

	designIdParam := params[constants.ParamDesignID]

	result, err := c.service.GetDesign(r.Context(), userParam, designIdParam)
	// 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)
}

// GetDesigns - Get list of all the designs created by the user.
func (c *DesignsApiController) GetDesigns(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	query := r.URL.Query()
	userParam := params[constants.ParamUser]

	limitParam, err := parseInt32Parameter(query.Get(constants.ParamLimit), false)
	if err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	result, err := c.service.GetDesigns(r.Context(), userParam, limitParam)
	// 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)
}

// UpdateDesign - Update design
func (c *DesignsApiController) UpdateDesign(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	userParam := params[constants.ParamUser]

	designIdParam := params["designId"]

	designInfoParam := DesignInfo{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&designInfoParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertDesignInfoRequired(designInfoParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.UpdateDesign(r.Context(), userParam, designIdParam, designInfoParam)
	// 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)
}
