// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-06-25 20:33
package define

import (
	"sync"

	"git.zhangdeman.cn/zhangdeman/consts"
	"git.zhangdeman.cn/zhangdeman/wrapper/op_ternary"
	"github.com/gin-gonic/gin"
)

var (
	inputHttpHandleConfig = &HttpHandleConfig{}
)

// HttpHandleConfig 请求处理配置
type HttpHandleConfig struct {
	RecordRequestDataField  string
	RecordResponseDataField string
	RequestIDField          string
	TraceIDField            string
	ResponseCodeField       string
	ResponseMessageField    string
	HandleRequestCostField  string
	ResponseDataField       string
	ResponseTraceIDField    string
	StartRequestTimeField   string
	FinishRequestTimeField  string
	RequestIsSuccessField   string // 请求处理是否成功的标识
	ExtensionOutputField    string // 扩展信息对外输出字段
	EnableExtensionOutput   bool
	DisableDebugStackOutput bool // 禁用异常堆栈打印
}

// ConvertDefaultConfig 覆盖默认配置
func ConvertDefaultConfig(cfg *HttpHandleConfig) {
	inputHttpHandleConfig = cfg
}

// GetHttpHandleConfig 获取http配置
func GetHttpHandleConfig() *HttpHandleConfig {
	return &HttpHandleConfig{
		EnableExtensionOutput:   inputHttpHandleConfig.EnableExtensionOutput,
		DisableDebugStackOutput: inputHttpHandleConfig.DisableDebugStackOutput,
		RequestIDField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIDField == "",
			consts.GinRequestIDField,
			inputHttpHandleConfig.RequestIDField,
		),
		TraceIDField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.TraceIDField == "",
			consts.GinTraceIDField,
			inputHttpHandleConfig.TraceIDField,
		),
		ResponseCodeField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseCodeField == "",
			consts.GinResponseCodeField,
			inputHttpHandleConfig.ResponseCodeField,
		),
		ResponseMessageField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseMessageField == "",
			consts.GinResponseMessageField,
			inputHttpHandleConfig.ResponseMessageField,
		),
		HandleRequestCostField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.HandleRequestCostField == "",
			consts.GinHandleRequestCostField,
			inputHttpHandleConfig.HandleRequestCostField,
		),
		ResponseDataField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseDataField == "",
			consts.GinResponseDataField,
			inputHttpHandleConfig.ResponseDataField,
		),
		ResponseTraceIDField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseTraceIDField == "",
			consts.GinResponseTraceIDField,
			inputHttpHandleConfig.ResponseTraceIDField,
		),
		StartRequestTimeField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.StartRequestTimeField == "",
			consts.GinStartRequestTimeField,
			inputHttpHandleConfig.StartRequestTimeField,
		),
		FinishRequestTimeField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.FinishRequestTimeField == "",
			consts.GinFinishRequestTimeField,
			inputHttpHandleConfig.FinishRequestTimeField,
		),
		RecordRequestDataField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordRequestDataField == "",
			consts.GinRecordRequestDataField,
			inputHttpHandleConfig.RecordRequestDataField,
		),
		RecordResponseDataField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordResponseDataField == "",
			consts.GinRecordResponseDataField,
			inputHttpHandleConfig.RecordResponseDataField,
		),
		RequestIsSuccessField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIsSuccessField == "",
			consts.GinRequestIsSuccessField,
			inputHttpHandleConfig.RecordResponseDataField,
		),
		ExtensionOutputField: op_ternary.BaseType[string](
			nil == inputHttpHandleConfig || inputHttpHandleConfig.ExtensionOutputField == "",
			consts.GinResponseExtensionField,
			inputHttpHandleConfig.RecordResponseDataField,
		),
	}
}

type LogicAfterResponse struct {
	SuccessHookFuncList []func(ctx *gin.Context) `json:"-"` // 请求最后需要执行的成功 hook函数
	FailureHookFuncList []func(ctx *gin.Context) `json:"-"` // 请求最后需要执行的失败 hook函数
	Lock                *sync.RWMutex            `json:"-"` // 逻辑锁
}

func (logic *LogicAfterResponse) AddSuccessHook(f func(ctx *gin.Context)) {
	logic.Lock.Lock()
	defer logic.Lock.Unlock()
	logic.SuccessHookFuncList = append(logic.SuccessHookFuncList, f)
}
func (logic *LogicAfterResponse) AddFailureHook(f func(ctx *gin.Context)) {
	logic.Lock.Lock()
	defer logic.Lock.Unlock()
	logic.FailureHookFuncList = append(logic.FailureHookFuncList, f)
}

// ResponseOption 响应的可用选项
type ResponseOption struct {
	ContentType string         `json:"content_type"` // 响应的contentType
	Extension   map[string]any `json:"extension"`    // 扩展数据
	XmlName     string         `json:"xml_name"`     // 以xml文件格式响应数据时, Xml文件名(根节点)
}

type SseData struct {
	ID        string           `json:"id"`         // 响应数据 ID
	Object    string           `json:"object"`     // 响应数据对象类型
	Created   int64            `json:"created"`    // 创建时间, 秒级时间戳
	Choices   []map[string]any `json:"choices"`    // choice 列表
	EventType string           `json:"event_type"` // 事件类型
}

const SseMsgFormat = "id:%v\nevent:%v\ndata:%v\n\n"
