package entitys

import (
	"ai_scheduler/internal/gateway"
	"encoding/json"

	"github.com/gofiber/websocket/v2"
)

type ResponseType string

const (
	ResponseJson       ResponseType = "json"
	ResponseLoading    ResponseType = "loading"
	ResponseEnd        ResponseType = "end"
	ResponseStream     ResponseType = "stream"
	ResponseText       ResponseType = "txt"
	ResponseImg        ResponseType = "img"
	ResponseFile       ResponseType = "file"
	ResponseErr        ResponseType = "error"
	ResponseLog        ResponseType = "log"
	ResponseAuth       ResponseType = "auth"
	ResponseMarkdown   ResponseType = "markdown"
	ResponseActionCard ResponseType = "actionCard"
)

func ResLog(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseLog,
	}
}

func ResStream(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseStream,
	}
}

func ResJson(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseJson,
	}
}

func ResEnd(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseEnd,
	}
}

func ResText(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseText,
	}
}

func ResLoading(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseLoading,
	}
}
func ResError(ch chan Response, index string, content string) {
	if ch == nil {
		return
	}
	ch <- Response{
		Index:   index,
		Content: content,
		Type:    ResponseErr,
	}
}

type ResponseData struct {
	Done    bool
	Content string
	Type    ResponseType
}

type Response struct {
	Content string
	Type    ResponseType
	Index   string
}

func MsgSet(msgType ResponseType, msg string, done bool) []byte {
	jsonByte, err := json.Marshal(ResponseData{
		Done:    done,
		Content: msg,

		Type: msgType,
	})
	if err != nil {
		return nil
	}
	return jsonByte
}

func MsgSend(client *gateway.Client, msg Response) error {
	// 检查上下文是否已取消
	if msg.Type == ResponseText {

	}
	jsonByte, _ := json.Marshal(msg)
	return client.SendFunc(jsonByte)
}

func MsgSendByte(c *websocket.Conn, msg []byte) {

	_ = c.WriteMessage(websocket.TextMessage, msg)
}
