package gplus

import (
	"context"
	"github.com/gogf/gf/v2/net/ghttp"
	"strings"
	iContext "yx-dataset-server/app/context"
	"yx-dataset-server/app/errors"
	"yx-dataset-server/app/schema"
	"yx-dataset-server/library/logger"
	"yx-dataset-server/library/utils"
)

// 定义上下文中的键
const (
	prefix = "gao-xin-tong"
	// UserIDKey 存储上下文中的键(用户ID)
	UserIDKey = prefix + "/user-id"
	// UserTypeKey 存储上下文中的键(用户类型)
	UserTypeKey = prefix + "/user-type"
	// TraceIDKey 存储上下文中的键(跟踪ID)
	TraceIDKey = prefix + "/trace-id"
	// ResBodyKey 存储上下文中的键(响应Body数据)
	ResBodyKey = prefix + "/res-body"
)

// ParseJson 解析请求参数Json
func ParseJson(r *ghttp.Request, out interface{}) error {
	if err := r.Parse(out); err != nil {
		m := "解析请求参数发生错误"
		if utils.GetConfig("common.run_mode").String() == "debug" {
			m += "[" + err.Error() + "]"
		}
		return errors.Wrap400Response(err, m)
	}
	return nil
}

// GetPageIndex 获取当前页
func GetPageIndex(r *ghttp.Request) int {
	defaultVal := 1
	if v := r.GetQuery("current").Int(); v > 0 {
		return v
	}
	return defaultVal
}

// GetPageSize 获取分页的页大小(最大50)
func GetPageSize(r *ghttp.Request) int {
	defaultVal := 10
	if v := r.GetQuery("pageSize").Int(); v > 0 {
		if v > 50 {
			v = 50
		}
		return v
	}
	return defaultVal
}

func ResPage(r *ghttp.Request, data interface{}, pr *schema.PaginationResult) {
	result := schema.HTTPList{
		List: data,
		Pagination: &schema.HTTPPagination{
			Current:  GetPageIndex(r),
			PageSize: GetPageSize(r),
		},
	}
	if data != nil {
		result.Pagination.Total = pr.Total
	}
	r.Response.WriteJson(&schema.HttpResponse{
		Code:    0,
		Message: "success",
		Data:    result,
	})
	r.Exit()
}

func ResSuccess(r *ghttp.Request, result interface{}) {
	r.Response.WriteJson(&schema.HttpResponse{
		Code:    0,
		Message: "success",
		Data:    result,
	})
	r.Exit()
}
func ResError(r *ghttp.Request, err error) {
	r.Response.WriteJson(&schema.HttpResponse{
		Code:    -1,
		Message: err.Error(),
		Data:    nil,
	})
	r.Exit()
}

// ResOK 响应OK
func ResOK(c *ghttp.Request) {
	ResSuccess(c, schema.HTTPStatus{Status: "OK"})
}

// ResList 响应列表数据
func ResList(c *ghttp.Request, v interface{}) {
	ResSuccess(c, schema.HTTPList{List: v})
}

// ResJSON 响应JSON结果
func ResJSON(r *ghttp.Request, code int, v interface{}) {
	errors.JsonExit(r, code, v)
}

// SetUserId 上下文中设置用户Id
func SetUserId(r *ghttp.Request, userId string) {
	r.SetCtxVar(UserIDKey, userId)
}

// GetUserId 获取上下文中的用户Id
func GetUserId(r *ghttp.Request) string {
	return r.GetCtxVar(UserIDKey).String()
}

// GetToken 获取token
func GetToken(r *ghttp.Request) string {
	var token string
	auth := r.GetHeader("Authorization")
	prefix := "Bearer "
	if auth != "" && strings.HasPrefix(auth, prefix) {
		token = auth[len(prefix):]
	}
	return token
}

// NewContext 封装上下文入口
func NewContext(r *ghttp.Request) context.Context {
	parent := context.Background()

	traceId := GetTraceID(r)
	if traceId == "" {
		traceId = utils.NewTraceID()
	}
	parent = iContext.NewTraceID(parent, traceId)
	parent = logger.NewTraceIDContext(parent, traceId)

	if v := GetUserID(r); v != "" {
		parent = iContext.NewUserID(parent, v)
		parent = logger.NewUserIDContext(parent, v)
	}

	return parent
}

// GetTraceID 获取追踪ID
func GetTraceID(c *ghttp.Request) string {
	return c.GetCtxVar(TraceIDKey).String()
}

// GetUserID 获取用户ID
func GetUserID(c *ghttp.Request) string {
	return c.GetCtxVar(UserIDKey).String()
}

// GetUserType 获取用户类型
func GetUserType(c *ghttp.Request) string {
	return c.GetCtxVar(UserTypeKey).String()
}
