package api

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/go-gost/x/config"
	parser "github.com/go-gost/x/config/parsing/limiter"
	"github.com/go-gost/x/registry"
)

// swagger:parameters getConnLimiterListRequest
type getConnLimiterListRequest struct {
}

// successful operation.
// swagger:response getConnLimiterListResponse
type getConnLimiterListResponse struct {
	// in: body
	Data connLimiterList
}

type connLimiterList struct {
	Count int                       `json:"count"`
	List  []*config.LimiterConfig `json:"list"`
}

func getConnLimiterList(ctx *gin.Context) {
	// swagger:route GET /config/climiters Limiter getConnLimiterListRequest
	//
	// Get conn limiter list.
	//
	//     Security:
	//       basicAuth: []
	//
	//     Responses:
	//       200: getConnLimiterListResponse

	var req getConnLimiterListRequest
	ctx.ShouldBindQuery(&req)

	list := config.Global().CLimiters

	var resp getConnLimiterListResponse
	resp.Data = connLimiterList{
		Count: len(list),
		List:  list,
	}

	ctx.JSON(http.StatusOK, Response{
		Data: resp.Data,
	})
}

// swagger:parameters getConnLimiterRequest
type getConnLimiterRequest struct {
	// in: path
	// required: true
	Limiter string `uri:"limiter" json:"limiter"`
}

// successful operation.
// swagger:response getConnLimiterResponse
type getConnLimiterResponse struct {
	// in: body
	Data *config.LimiterConfig
}

func getConnLimiter(ctx *gin.Context) {
	// swagger:route GET /config/climiters/{limiter} Limiter getConnLimiterRequest
	//
	// Get conn limiter.
	//
	//     Security:
	//       basicAuth: []
	//
	//     Responses:
	//       200: getConnLimiterResponse

	var req getConnLimiterRequest
	ctx.ShouldBindUri(&req)

	var resp getConnLimiterResponse

	for _, limiter := range config.Global().CLimiters{
		if limiter == nil {
			continue
		}
		if limiter.Name == req.Limiter {
			resp.Data = limiter
		}
	}

	ctx.JSON(http.StatusOK, Response{
		Data: resp.Data,
	})
}

// swagger:parameters createConnLimiterRequest
type createConnLimiterRequest struct {
	// in: body
	Data config.LimiterConfig `json:"data"`
}

// successful operation.
// swagger:response createConnLimiterResponse
type createConnLimiterResponse struct {
	Data Response
}

func createConnLimiter(ctx *gin.Context) {
	// swagger:route POST /config/climiters Limiter createConnLimiterRequest
	//
	// Create a new conn limiter, the name of limiter must be unique in limiter list.
	//
	//     Security:
	//       basicAuth: []
	//
	//     Responses:
	//       200: createConnLimiterResponse

	var req createConnLimiterRequest
	ctx.ShouldBindJSON(&req.Data)

	name := strings.TrimSpace(req.Data.Name)
	if name == "" {
		writeError(ctx, NewError(http.StatusBadRequest, ErrCodeInvalid, "limiter name is required"))
		return
	}
	req.Data.Name = name

	if registry.ConnLimiterRegistry().IsRegistered(name) {
		writeError(ctx, NewError(http.StatusBadRequest, ErrCodeDup, fmt.Sprintf("limiter %s already exists", name)))
		return
	}

	v := parser.ParseConnLimiter(&req.Data)
	if err := registry.ConnLimiterRegistry().Register(name, v); err != nil {
		writeError(ctx, NewError(http.StatusInternalServerError, ErrCodeFailed, fmt.Sprintf("create limmiter %s failed: %s", name, err.Error())))
		return
	}

	config.OnUpdate(func(c *config.Config) error {
		c.CLimiters = append(c.CLimiters, &req.Data)
		return nil
	})

	ctx.JSON(http.StatusOK, Response{
		Msg: "OK",
	})
}

// swagger:parameters updateConnLimiterRequest
type updateConnLimiterRequest struct {
	// in: path
	// required: true
	Limiter string `uri:"limiter" json:"limiter"`
	// in: body
	Data config.LimiterConfig `json:"data"`
}

// successful operation.
// swagger:response updateConnLimiterResponse
type updateConnLimiterResponse struct {
	Data Response
}

func updateConnLimiter(ctx *gin.Context) {
	// swagger:route PUT /config/climiters/{limiter} Limiter updateConnLimiterRequest
	//
	// Update conn limiter by name, the limiter must already exist.
	//
	//     Security:
	//       basicAuth: []
	//
	//     Responses:
	//       200: updateConnLimiterResponse

	var req updateConnLimiterRequest
	ctx.ShouldBindUri(&req)
	ctx.ShouldBindJSON(&req.Data)

	name := strings.TrimSpace(req.Limiter)

	if !registry.ConnLimiterRegistry().IsRegistered(name) {
		writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("limiter %s not found", name)))
		return
	}

	req.Data.Name = name

	v := parser.ParseConnLimiter(&req.Data)

	registry.ConnLimiterRegistry().Unregister(name)

	if err := registry.ConnLimiterRegistry().Register(name, v); err != nil {
		writeError(ctx, NewError(http.StatusBadRequest, ErrCodeDup, fmt.Sprintf("limiter %s already exists", name)))
		return
	}

	config.OnUpdate(func(c *config.Config) error {
		for i := range c.CLimiters {
			if c.CLimiters[i].Name == name {
				c.CLimiters[i] = &req.Data
				break
			}
		}
		return nil
	})

	ctx.JSON(http.StatusOK, Response{
		Msg: "OK",
	})
}

// swagger:parameters deleteConnLimiterRequest
type deleteConnLimiterRequest struct {
	// in: path
	// required: true
	Limiter string `uri:"limiter" json:"limiter"`
}

// successful operation.
// swagger:response deleteConnLimiterResponse
type deleteConnLimiterResponse struct {
	Data Response
}

func deleteConnLimiter(ctx *gin.Context) {
	// swagger:route DELETE /config/climiters/{limiter} Limiter deleteConnLimiterRequest
	//
	// Delete conn limiter by name.
	//
	//     Security:
	//       basicAuth: []
	//
	//     Responses:
	//       200: deleteConnLimiterResponse

	var req deleteConnLimiterRequest
	ctx.ShouldBindUri(&req)

	name := strings.TrimSpace(req.Limiter)

	if !registry.ConnLimiterRegistry().IsRegistered(name) {
		writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("limiter %s not found", name)))
		return
	}
	registry.ConnLimiterRegistry().Unregister(name)

	config.OnUpdate(func(c *config.Config) error {
		limiteres := c.CLimiters
		c.CLimiters = nil
		for _, s := range limiteres {
			if s.Name == name {
				continue
			}
			c.CLimiters = append(c.CLimiters, s)
		}
		return nil
	})

	ctx.JSON(http.StatusOK, Response{
		Msg: "OK",
	})
}
