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

import cn.hutool.core.util.CharsetUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

@Getter
@Setter
@ToString
@Slf4j
@Accessors(chain = true)
public class ServerTxtMessage implements ServerMessageBody {
    private byte flag;
    private String txt;

    public ServerTxtMessage(String txt) {
        this.flag = 0x01;
        this.txt = txt;
    }

    public byte[] toBytes() {
        byte[] messageBody = new byte[txt.length() + 1];
        messageBody[0] = flag;
        try {
            System.arraycopy(txt.getBytes(CharsetUtil.GBK), 0, messageBody, 1, txt.length());
        } catch (UnsupportedEncodingException e) {
            log.error("文本消息构建失败，原文本：{}", txt, e);
        }
        return messageBody;
    }
}
