package model

import (
	"strconv"
	"time"
)

// 服务器响应对象
type ServerResponseData struct {
	// Id
	Id int

	// 聊天频道
	Channel string

	// 聊天消息
	Message string

	// 语音信息
	Voice string

	// 语音文字
	VoiceText string

	// 发送人
	FromPlayer   string
	FromPlayerId string

	// 接收人
	ToPlayer   string `json:"ToPlayer,omitempty"`
	ToPlayerId string `json:"ToPlayerId,omitempty"`

	// 创建的时间戳
	TimeStamp int64

	//是否是离线消息
	IfOffline bool

	//是否已读消息
	IfRead bool
}

// 创建新的服务器响应对象
func NewServerResponseData(id int, channel, message, voice, voiceText string, from, to *Player, ifOffline, ifRead int32) *ServerResponseData {
	var fromPlayer, toPlayer string
	var fromPlayerId, toPlayerId int64
	if from != nil {
		fromPlayerId = from.Id
		fromPlayer = from.String()
	}

	if to != nil {
		toPlayerId = to.Id
		toPlayer = to.String()
	}

	return &ServerResponseData{
		Id:           id,
		Channel:      channel,
		Message:      message,
		Voice:        voice,
		VoiceText:    voiceText,
		FromPlayer:   fromPlayer,
		FromPlayerId: strconv.FormatInt(fromPlayerId, 10),
		ToPlayer:     toPlayer,
		ToPlayerId:   strconv.FormatInt(toPlayerId, 10),
		TimeStamp:    time.Now().Unix(),
		IfOffline:    ifOffline == 1,
		IfRead:       ifRead == 1,
	}
}

// 从其它类型转化为服务器响应对象
func ConvertToServerResponseData(id int, channel, message, voice, voiceText, fromPlayer string, fromPlayerId int64, toPlayer string, toPlayerId int64, timeStamp int64, ifOffline, ifRead bool) *ServerResponseData {
	return &ServerResponseData{
		Id:           id,
		Channel:      channel,
		Message:      message,
		Voice:        voice,
		VoiceText:    voiceText,
		FromPlayer:   fromPlayer,
		FromPlayerId: strconv.FormatInt(fromPlayerId, 10),
		ToPlayer:     toPlayer,
		ToPlayerId:   strconv.FormatInt(toPlayerId, 10),
		TimeStamp:    timeStamp,
		IfOffline:    ifOffline,
		IfRead:       ifRead,
	}
}
