package middleware

import (
	"fmt"
	"github.com/gogf/gf/v2/net/ghttp"
	"strings"
)

// EmptyMiddleware 不执行业务处理的中间件
func EmptyMiddleware(r *ghttp.Request) {
	r.Middleware.Next()
}

type SkipperFunc func(request *ghttp.Request) bool

// AllowPathPrefixSkipper 检查请求路径是否包含指定的前缀，如果包含则跳过
func AllowPathPrefixSkipper(prefixes ...string) SkipperFunc {
	return func(request *ghttp.Request) bool {
		path := request.URL.Path
		pathLen := len(path)
		for _, p := range prefixes {
			if pl := len(p); pathLen >= pl && path[:pl] == p {
				return true
			}
		}
		return false
	}
}

// AllowMethodAndPathPrefixSkipper 检查请求方法和路径是否包含指定的前缀，如果不包含则跳过
func AllowMethodAndPathPrefixSkipper(prefixes ...string) SkipperFunc {
	return func(request *ghttp.Request) bool {
		path := JoinRouter(request.Method, request.URL.Path)
		pathLen := len(path)

		for _, p := range prefixes {
			if pl := len(p); pathLen >= pl && path[:pl] == p {
				return true
			}
		}
		return false
	}
}

// JoinRouter 拼接路由
func JoinRouter(method, path string) string {
	if len(path) > 0 && path[0] != '/' {
		path = "/" + path
	}
	return fmt.Sprintf("%s%s", strings.ToUpper(method), path)
}

// AllowPathPrefixNoSkipper 检查请求路径是否包含指定的前缀，如果包含则不跳过
func AllowPathPrefixNoSkipper(prefixes ...string) SkipperFunc {
	return func(request *ghttp.Request) bool {
		path := request.URL.Path
		pathLen := len(path)

		for _, p := range prefixes {
			if pl := len(p); pathLen >= pl && path[:pl] == p {
				return false
			}
		}
		return true
	}
}

// SkipHandler 统一处理跳过函数
func SkipHandler(r *ghttp.Request, skippers ...SkipperFunc) bool {
	for _, skipper := range skippers {
		if skipper(r) {
			return true
		}
	}
	return false
}
