package legacypacket

import (
	"github.com/akmalfairuz/legacy-version/legacyver/proto"
	"github.com/sandertv/gophertunnel/minecraft/protocol"
	"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)

// Disconnect may be sent by the server to disconnect the client using an optional message to send as the
// disconnect screen.
type Disconnect struct {
	// Reason is the reason for the disconnection. It seems as if this field has no use other than for
	// telemetry reasons as it does not affect the message that gets displayed on the disconnect screen.
	Reason int32
	// HideDisconnectionScreen specifies if the disconnection screen should be hidden when the client is
	// disconnected, meaning it will be sent directly to the main menu.
	HideDisconnectionScreen bool
	// Message is an optional message to show when disconnected. This message is only written if the
	// HideDisconnectionScreen field is set to true.
	Message string
	// FilteredMessage is a filtered version of Message with all the profanity removed. The client will use
	// this over Message if this field is not empty and they have the "Filter Profanity" setting enabled.
	FilteredMessage string
}

// ID ...
func (*Disconnect) ID() uint32 {
	return packet.IDDisconnect
}

func (pk *Disconnect) Marshal(io protocol.IO) {
	io.Varint32(&pk.Reason)
	io.Bool(&pk.HideDisconnectionScreen)
	if !pk.HideDisconnectionScreen {
		io.String(&pk.Message)
		if proto.IsProtoGTE(io, proto.ID712) {
			io.String(&pk.FilteredMessage)
		}
	}
}
