package cn.orionsec.ops.constant.ws;

import cn.orionsec.kit.lang.utils.Exceptions;
import cn.orionsec.kit.lang.utils.Strings;
import cn.orionsec.kit.lang.utils.Valid;
import lombok.AllArgsConstructor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;


@AllArgsConstructor
public enum WsProtocol {

    /**
     * 正常返回
     */
    OK("0"),

    /**
     * 连接成功
     */
    CONNECTED("1"),

    /**
     * ping
     */
    PING("2"),

    /**
     * pong
     */
    PONG("3"),

    /**
     * 未知操作
     */
    ERROR("4"),

    ;

    private final String code;

    /**
     * 分隔符
     */
    public static final String SYMBOL = "|";

    public byte[] get() {
        return Strings.bytes(code);
    }

    public byte[] msg(String body) {
        Valid.notNull(body);
        return this.msg(Strings.bytes(body));
    }

    public byte[] msg(byte[] body) {
        return this.msg(body, 0, body.length);
    }

    public byte[] msg(byte[] body, int offset, int len) {
        Valid.notNull(body);
        try (ByteArrayOutputStream o = new ByteArrayOutputStream()) {
            o.write(Strings.bytes(code + SYMBOL));
            o.write(body, offset, len);
            return o.toByteArray();
        } catch (IOException e) {
            throw Exceptions.ioRuntime(e);
        }
    }

}
