package com.njzscloud.common.localizer.jt808.message;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 服务器注册响应消息体
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
public class ServerRegisterReplyMessage implements ServerMessageBody {
    /**
     * 终端ID
     */
    private String terminalId;
    /**
     * 流水号
     */
    private int flowId;
    /**
     * 结果码，0 成功，其他失败
     */
    private int result;
    /**
     * 认证码
     */
    private String authCode;

    @Override
    public byte[] toBytes() {
        ByteBuf body = Unpooled.buffer();
        try {
            body.writeShort(flowId);
            body.writeByte(result);
            if (result == 0) {
                ByteBufUtil.writeAscii(body, authCode);
            }
            return ByteBufUtil.getBytes(body);
        } finally {
            body.release();
        }
    }
}
