package model

import (
	"compress/zlib"
	"encoding/json"
	"goutil/zlibUtil"
)

// ChatServer的响应对象
type ServerResponseObject struct {
	// 响应结果的状态值
	*ResultStatus

	// 响应结果的数据
	Data interface{} `json:"Data,omitempty"`

	// 响应结果对应的请求的方法名称
	MethodName string

	// 压缩后的字节
	DataByte []byte `json:"-"`
}

func (this *ServerResponseObject) SetResultStatus(rs *ResultStatus) *ServerResponseObject {
	this.ResultStatus = rs

	return this
}

func (this *ServerResponseObject) SetData(data interface{}) *ServerResponseObject {
	this.Data = data

	return this
}

func (this *ServerResponseObject) SetMethodName(methodName string) *ServerResponseObject {
	this.MethodName = methodName

	return this
}

func (this *ServerResponseObject) IsDisconnect() bool {
	return this.ResultStatus.Code == DisconnectStatus.Code
}

// Compress
//
//	@Description: 压缩数据
//	parameter:
//	                 receiver this
//	                 ifCompress
//	return:
func (this *ServerResponseObject) Compress(ifCompress bool) {
	// 序列化发送的数据
	contentObj, _ := json.Marshal(this)

	// 进行zlib压缩
	if ifCompress {
		contentObj, _ = zlibUtil.Compress(contentObj, zlib.DefaultCompression)
	}

	//赋值
	this.DataByte = contentObj
}

func NewServerResponseObject() *ServerResponseObject {
	return &ServerResponseObject{
		ResultStatus: Success,
		Data:         nil,
		MethodName:   "",
	}
}

func NewDisconnectServerResponseObject() *ServerResponseObject {
	return &ServerResponseObject{
		ResultStatus: DisconnectStatus,
		Data:         nil,
		MethodName:   "",
	}
}
