// Package ws -----------------------------
// @file      : hertzWSUpgrade.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2022/6/28 14:14
// -------------------------------------------
package ws

import (
	"encoding/json"
	"fonchain-fiee/pkg/e"
	"fonchain-fiee/pkg/serializer"
	"strings"
)

// 消息结构
type WSMessage struct {
	Type string `json:"type"`
	Data string `json:"data"`
}

// websocket消息内容
type WsInfo struct {
	Type    WsType      `json:"type"`    //消息类型
	Content interface{} `json:"content"` //消息内容
	From    string      `json:"from"`    //发送者 0为服务端,客户端填写clientId
	To      string      `json:"to"`      //接收者 接收消息的用户id
	Mark    string      `json:"mark"`
	//Conn    *websocket.Conn `json:"-"`       //客户端发送消息使用
}
type WsSessionInfo struct {
	Type WsType `json:"type"` //消息类型
	//SessionId string      `json:"sessionId"` //会话Id
	Content interface{} `json:"content"` //消息内容
}

// 身份认证消息
type AuthorizationInfo struct {
	Type    WsType   `json:"type"` //消息类型
	Content AuthInfo `json:"content"`
}

type AuthInfo struct {
	Auth   string `json:"auth"`
	Domain string `json:"domain"`
}

// 注册消息
type WsRegisterInfo struct {
	Type    WsType   `json:"type"`    //消息类型
	Content UserInfo `json:"content"` //消息内容
	From    string   `json:"from"`    //发送者 0为服务端,客户端填写clientId
	To      string   `json:"to"`      //接收者 接收消息的用户id
	//Conn    *websocket.Conn `json:"-"`       //客户端发送消息使用
}
type UserInfo struct {
	Uuid     string `json:"uuid"`               //画家uid
	UserId   int64  `json:"userId"`             //用户id
	ClientId string `json:"clientId,omitempty"` //服务端临时签发的客户端uid
}

type TempClientInfo struct {
	ClientId string `json:"clientId"`
}

func WsMessageRegisterCallback(clientId string) []byte {
	var errMsg = WsInfo{
		Type: RegisterType,
		Content: map[string]string{
			"clientId": clientId,
		},
		From: "0",
		To:   clientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}

func WsErrorMessage(wsType WsType, clientId string, code e.ErrorCodeType, err error) []byte {
	var ers string
	if err != nil {
		ers = err.Error()
	}
	var content = serializer.Response{
		Code: code,
		Err:  ers,
		Msg:  code.String(),
	}
	var errMsg = WsInfo{
		Type:    wsType,
		Content: content,
		From:    "0",
		To:      clientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}
func WsErrorPermissionDenied(wsType WsType, clientId string) []byte {
	var content = serializer.Response{
		Code: e.PermissionDenied,
		Err:  "Permission Denied",
		Msg:  "拒绝访问",
	}
	var errMsg = WsInfo{
		Type:    wsType,
		Content: content,
		From:    "0",
		To:      clientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}

func WsErrorInvalidDataFormat(clientId string) []byte {
	var content = serializer.Response{
		Status: e.Failed,
		Code:   e.Failed,
		Err:    "Invalid Data Format",
		Msg:    "发送失败",
	}
	var errMsg = WsInfo{
		Type:    ErrorType,
		Content: content,
		From:    "0",
		To:      clientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}

func WsErrorUnknownMessageType(clientId string) []byte {
	var errMsg = WsInfo{
		Type:    ErrorType,
		Content: "Unknown notice type",
		From:    "0",
		To:      clientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}

func WsErrorConnection(clientId string, err string, marks ...string) []byte {
	mark := ""
	if marks != nil {
		mark = strings.Join(marks, ";")
	}
	var errMsg = WsInfo{
		Type:    ErrorType,
		Content: "Connection error:" + err,
		From:    "0",
		To:      clientId,
		Mark:    mark,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}

func WsChatMessage(clientId string, targetClientId string, msg string) []byte {
	var errMsg = WsInfo{
		Type:    ChatType,
		Content: msg,
		From:    clientId,
		To:      targetClientId,
	}
	byteMsg, _ := json.Marshal(errMsg)
	return byteMsg
}